Linux環境で、curlコマンドを使用してグローバルIPアドレスを取得する方法を解説します。
はじめに
Linux環境で「curl <接続先>」コマンドを実行することで、グローバルIPアドレスを取得することが可能です。
また、WindowsなどのGUI環境でブラウザに「http://<接続先>」と入力することで、グローバルIPアドレスの他、ユーザーエージェントなどの情報も取得することが可能です。
下記のように表示された場合は、外部ネットワークに接続されていないので、設定を見直してみて下さい。
接続に失敗した場合のエラー
curl: (6) Could not resolve host: <接続先>
inet-ip.info
[root@localhost ~]# curl inet-ip.info ***.***.***.***
下記のコマンドでも取得することが出来ます。
[root@localhost ~]# curl https://inet-ip.info ***.***.***.***
[/json]を付けて実行することで、グローバルIPアドレス以外にも様々な情報を取得することが可能です。下記は取得例です。
[root@localhost ~]# curl inet-ip.info/json
"Subdivisions": [
{
"GeoNameID": 9999999,
"IsoCode": "99",
"Names": {
"en": "Tokyo",
"fr": "Prefecture de Tokyo",
"ja": "東京都"
}
}
]
globalip.me
[root@localhost ~]# curl globalip.me ***.***.***.***
httpbin.org
[root@localhost ~]# curl http://httpbin.org/ip
{
"origin": "***.***.***.***"
}
リンク
ifconfig.me
[root@localhost ~]# curl ifconfig.me ***.***.***.***
上記の場合、環境によっては「IPv6」で取得されます。
下記のコマンドを実行することで「IPv4」で取得することが可能です。
[root@localhost ~]# curl -4 ifconfig.me ***.***.***.***
下記のコマンドでも取得することが出来ます。
[root@localhost ~]# curl https://ifconfig.me ***.***.***.***
ifconfig.co
[root@localhost ~]# curl -4 ifconfig.co ***.***.***.***
上記以外のコマンドでも取得することが出来ます。
[root@localhost ~]# curl https://ifconfig.co ***.***.***.***
上記の場合、環境によっては「IPv6」で取得されます。
下記のコマンドを実行することで「IPv4」で取得することが可能です。
[root@localhost ~]# curl -4 https://ifconfig.co ***.***.***.***
リンク
ifconfig.io
[root@localhost ~]# curl ifconfig.io ***.***.***.***
上記コマンドの場合、環境により「IPv4」か「IPv6」のどちらかが取得されます。
「IPv4」を確実に取得したい場合は以下のコマンドを実行して下さい。
[root@localhost ~]# curl -4 ifconfig.io ***.***.***.***
[/all.json]を付けて実行することで、グローバルIPアドレス以外にも様々な情報を取得することが可能です。下記は取得例です。
[root@localhost ~]# curl ifconfig.io/all.json
{
"country_code":"JP",
"encoding":"gzip",
"forwarded":"***.***.***.***",
"host":"***.***.***.***",
"ifconfig_hostname":"ifconfig.io",
"ip":"***.***.***.***",
"lang":"",
"method":"GET",
"mime":"*/*",
"port":9999,
"referer":"",
"ua":"curl/7.51.1"
}
ipinfo.io
[root@localhost ~]# curl ipinfo.io
{
"ip": "***.***.***.***",
"hostname": "***.***.***",
"city": "Tokyo",
"region": "Tokyo",
"country": "JP",
"loc": "***.***.***.***",
"org": "***.***.***.***",
"postal": "***-****",
"timezone": "Asia/Tokyo",
"readme": "https://ipinfo.io/missingauth"
ip-api.com
「ip-api.com」では取得結果が色分けされるので、他より少し見やすくなっています。
[root@localhost ~]# curl ip-api.com
{
"status" : "success",
"continent" : "Asia",
"continentCode": "AS",
"country" : "Japan",
"countryCode" : "JP",
"region" : "99",
"regionName" : "Tokyo",
"city" : "Tokyo",
"district" : "",
"zip" : "***-****",
"lat" : ***.***.***.***,
"lon" : ***.***.***.***,
"timezone" : "Asia/Tokyo",
"offset" : 99999,
"currency" : "JPY",
"isp" : "***.***.***.***",
"org" : "***.***.***.***",
"as" : "***.***.***.***",
"asname" : "***.***.***.***",
"mobile" : false,
"proxy" : false,
"hosting" : false,
"query" : "***.***.***.***"
}





コメント