IP to Country console command
This trick is even nicer and doesn't require any external packages:
curl ipinfo.io/23.66.166.151
The command is the easy part, the difficult part is having access to a database.
For example, Ubuntu has a free database with a command line query tool (geoiplookup
) in the geoip-bin
There are various GeoIP databases that you can look up. They're generally meant to be viewed through a web browser, but you can look for a scraping script. For example, here's a ruby script to retrieve data from the MaxMind database. Note that scraping may be against the database's terms of service.
Here's another great option. Instructions here: http://kbeezie.com/geoiplookup-command-line/
For example, on Centos:
$ sudo yum install GeoIP GeoIP-data
$ geoiplookup 8.8.4.4
Works perfectly.
And of course, you can always set this up as a cron:
$ /usr/bin/geoipupdate