IPアドレスから、どの国からアクセスしてきたかを調べたいことが多い。 いくつか実現方法があるのでメモをしておく。
IPinfo
IPinfoは会員登録不要で、無料で5万リクエスト/月まで出来る。
$ curl ipinfo.io/<IPアドレス>
e.g.)
$ curl ipinfo.io/8.8.8.8{"ip": "8.8.8.8", "hostname": "dns.google", "anycast": true, "city": "Mountain View", "region": "California", "country": "US", "loc": "37.4056,-122.0775", "org": "AS15169 Google LLC", "postal": "94043", "timezone": "America/Los_Angeles", "readme": "https://ipinfo.io/missingauth"}
ip-api
ip-apiは会員登録不要で、45リクエスト/分間まで出来る。
$ curl ip-api.com/json/<IPアドレス>
$ curl ip-api.com/json/8.8.8.8 | jq {"status": "success", "country": "United States", "countryCode": "US", "region": "VA", "regionName": "Virginia", "city": "Ashburn", "zip": "20149", "lat": 39.03, "lon": -77.5, "timezone": "America/New_York", "isp": "Google LLC", "org": "Google Public DNS", "as": "AS15169 Google LLC", "query": "8.8.8.8"}
ipstack
ipstackは会員登録は必要になるが、無料プランも提供されている。
$ curl http://api.ipstack.com/<IPアドレス>?access_key=<YOUR_ACCESS_KEY>
whoisコマンド
$ whois <IPアドレス> | grep-i country | head -1 | awk '{print $2}'
e.g.)
$ whois 8.8.8.8 | grep-i country | head -1 | awk '{print $2}' US