LoginSignup
312
250

More than 5 years have passed since last update.

グローバルIPをcurlで確認

Last updated at Posted at 2014-11-09

概要

サーバのグローバル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に捨てちゃうといい感じに見やすくなります。

参考リンク

312
250
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
312
250