Is there a console command that takes an IP address as an input and shows its geographical information like city, country, ISP, etc.?
5 Answers
This trick is even nicer and doesn't require any external packages:
curl ipinfo.io/23.66.166.151
-
3In case ipinfo.io gets offline someday, ifconfig.me provides a similar service (plus a REST-like API). But ifonfo.io seems much faster. – cedbeu Mar 05 '15 at 15:48
-
Meh, doesn't allow hostnames... – m3nda Mar 11 '16 at 04:47
-
1$ host yourdomain.com | cut -d ' ' -f 4 | curl ipinfo.io/$1 – zmonteca Mar 22 '16 at 15:57
-
3ipinfo.io is free for 1000 requests/day (see their [pricing documentation](http://ipinfo.io/pricing)). – Matthieu Nov 14 '16 at 15:03
-
5Another alternative: `curl ipinfo.io/$(dig +short yourdomain.com)` – John Red Dec 07 '16 at 17:00
-
@Matthieu I haven't hit 1000 limit but I'm planning to script it to check some IP addresses periodically during the day. Have you hit 1000 and blocked from doing more requests? – Jan 07 '17 at 10:08
-
@sdkks I'm not using it in a script, only from time to time when I need it. I took the limit from their documentation. – Matthieu Jan 07 '17 at 11:47
-
3If someone ever hits the ceiling on the ipinfo.io API (lucky you?), there is also https://freegeoip.net/ which has a mind-blowing 15,000 requests per hour limit. (Which under the hood seems to be running the geoiplookup from the other answers) – samthecodingman Mar 09 '17 at 13:52
-
10.000 requests/mo now for freegeoip.net – Imaskar Sep 20 '18 at 08:53
-
If you have 'jq' here's a fun one-liner :: `curl -qs http://ipinfo.io/$(curl -qs http://ipinfo.io/ip) | jq -r '. | "\(.city), \(.country)"';` Produces "Reykjavik, IS" for me. – Martin Bramwell Mar 29 '23 at 16:50
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 Install geoip-bin.package. But it only shows country information, and uses a static (hence out-of-date) database. This tool can also query the MaxMind GeoIP database, if you have a subscription there.
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.
- 807,993
- 194
- 1,674
- 2,175
-
1
-
-
@StephenRauch Thanks but please use the official replacement for the broken bit.ly link: https://hostmar.co/software-small – Gilles 'SO- stop being evil' Apr 03 '17 at 00:23
-
1@StephenRauch The apt link only works with a browser plug-in that's part of the default Ubuntu installation but rarely installed otherwise. – Gilles 'SO- stop being evil' Apr 03 '17 at 00:29
-
-
https://apple.stackexchange.com/questions/269787/homebrew-installed-geoiplookup-shows-no-output-howto-install-geoip-data – Pysis Nov 25 '18 at 17:12
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
-
[`geoipupdate`](https://dev.maxmind.com/geoip/geoipupdate/) is a MaxMind tool, you need a subscription and the licence key on `/etc/GeoIP.conf`. I couldn't use the [free alternative](https://dev.maxmind.com/geoip/geoipupdate/#For_Free_GeoLite2_Databases) (_Received an unexpected HTTP status code of 401_). – Pablo A Mar 09 '18 at 22:47
Here is my little command line tool called "gip":
https://github.com/softhub-software-development/gip
# gip -h usage: gip [-C] [-c] [-g] [-h] [-tT] [ip-address-or-domain ...] gip invoked with no arguments starts the gip server -C print country -c print city -g print geo coordinates only -d print domain info if possible -h print this help information -t trace route omitting duplicates -T trace route
In server-mode it renders a map with pin heads indicating the origin of requests.
- 103
- 1
- 11
- 1
-
1
-
It's using https://lite.ip2location.com/ip2location-lite (DB11·LITE to be precise) but includes only a few ip ranges for testing in the repository. You need to download the .csv yourself and generate the dataset which takes about 10 minutes on a raspberry pi. – chris Jul 18 '21 at 18:05