Help us understand the problem. What is going on with this article?

グローバルIPをcurlで確認

More than 3 years have passed since last update.

概要

サーバのグローバルIPをcurlコマンドで確認する方法について

curlコマンド

以下URLどれでもOK

  • httpbin.org/ip
$ curl httpbin.org/ip
{
  "origin": "121.102.14.91"
}
  • inet-ip.info
$ curl inet-ip.info
121.102.14.91
  • ifconfig.me
$ curl ifconfig.me   #実行に時間かかる
121.102.14.91

curlコマンドでサーバとのやり取りを表示したい時

-v, --verboseオプションを付ける

$ curl -v httpbin.org/ip
*   Trying 23.23.223.197...
* Connected to httpbin.org (23.23.223.197) port 80 (#0)
> GET /ip HTTP/1.1
> Host: httpbin.org
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Connection: keep-alive
< Server: gunicorn/19.7.1
< Date: Fri, 21 Apr 2017 05:38:49 GMT
< Content-Type: application/json
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Credentials: true
< Content-Length: 32
< Via: 1.1 vegur
<
{
  "origin": "121.102.14.91"
}
* Connection #0 to host httpbin.org left intact

cf) curlの使い方

curlがサーバにどういうリクエストを送信して、サーバからどういうレスポンスが返ってきているかを見たいことがあります。この場合、「 -v 」オプションを付けておくと、このやり取りを表示することができます。
$ curl -v http://www.hoge.com/

curlコマンドでサーバとのやり取りのヘッダー情報だけ表示したい時

-v, --verboseオプションを付けて、標準出力を/dev/nullに捨てる

$ curl -v httpbin.org/ip > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 54.243.85.55...
* Connected to httpbin.org (54.243.85.55) port 80 (#0)
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0> GET /ip HTTP/1.1
> Host: httpbin.org
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Connection: keep-alive
< Server: gunicorn/19.7.1
< Date: Fri, 21 Apr 2017 05:40:49 GMT
< Content-Type: application/json
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Credentials: true
< Content-Length: 32
< Via: 1.1 vegur
<
{ [32 bytes data]
100    32  100    32    0     0     79      0 --:--:-- --:--:-- --:--:--    79
* Connection #0 to host httpbin.org left intact

cf) curlでヘッダを見る方法いろいろ

--verboseオプションをつけるとリクエストヘッダ、レスポンスヘッダ、httpsならTLS handshakeの様子等が出力されるようになります。なので、--verboseをつけるとヘッダが見れます。
また、この"verboseな"情報は標準エラー出力に出力されるので、ヘッダだけ見たい、と言う時は標準出力は/dev/nullに捨てちゃうといい感じに見やすくなります。

参考リンク

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした