Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

IP address geolocation from the command line

1,228 views
Skip to first unread message

Mark Cassidy

unread,
Jan 8, 2016, 1:22:43 AM1/8/16
to
Is there a way to get IP address geolocation from the command line
(http://whatismyipaddress.com)?

whatismyipaddress.com LOCATION information from a command line?
Let's say I want to know the geolocation of an IP address such as
100.9.53.236 which, according to firefox http://whatismyipaddress.com,
geolocates to
a. ISP: Verizon FiOS
b. City: Victorville
c: Region: California
d. Country: USA

Or, 104.208.39.105, which, according to the same web site geolocates to
a. ISP: Microsoft Corporation
b. City: Redmond
c. Region: Washington
d. Country: USA

Or, 104.53.124.192, which geolocates to
a. ISP: AT&T U-verse
b. City: Missouri City
c. Region: Texas
d. Country:USA

How can I get that geolocation information from the Linux command line?

I tried whois and curl, but both failed.

$ whois 100.9.53.236 | more
$ curl http://100.9.53.236; echo

Is there a way to query the geolocation providers from the command line?
http://whatismyipaddress.com/geolocation-providers
to get IP address geolocation information such as city, region, & country?

Eef Hartman

unread,
Jan 8, 2016, 1:52:09 AM1/8/16
to
Mark Cassidy <mcas...@dot.invalid> wrote:
> Is there a way to get IP address geolocation from the command line
> (http://whatismyipaddress.com)?

You would at least need something like the GeoIP database:
http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
and a program to search that (I don't know of any).
I myself use that GeoIP data with the Vuze CountryLocator plugin but that
will give you only the country the IP is in. The Vuze LocationProvider
one returns about the same info (and uses the same type of databases).
But they both are only usable FROM the Vuze UI and do not permit free
queries, just info about the peers connected TO Vuze.

maxmind does have a City database too, like:
http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz
but even that isn't too accurate, for instance MY provider covers the
whole country (Netherlands), you cannot determine from the IP _where_
a certain customer is located (deliberately).

PS: the GeoLite2 databases are also available in CVS format, so that
you can load them into a spreadsheet (like Excel) and search it that
way, but that still is a graphical interface, not a commandline one.

Mark Cassidy

unread,
Jan 8, 2016, 1:56:10 AM1/8/16
to
On Fri, 08 Jan 2016 06:22:41 +0000, Mark Cassidy wrote:

> Is there a way to get IP address geolocation from the command line

Nevermind.
I figured out a way to get IP address city and state from the linux CLI.

$ ls -l /usr/share/GeoIP/
$ sudo apt-get install geoip-bin
$ geoiplookup -v 100.9.53.236
$ geoiplookup 100.9.53.236
$ geoiplookup -f /usr/share/GeoIP/GeoLiteCity.dat 100.9.53.236
$ cd /tmp
$ wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
$ gunzip GeoIP.dat.gz
$ sudo cp GeoIP.dat /usr/share/GeoIP/
$ geoiplookup 100.9.53.236
$ wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
$ gunzip GeoLiteCity.dat.gz
$ sudo cp GeoLiteCity.dat /usr/share/GeoIP/
$ geoiplookup -f /usr/share/GeoIP/GeoLiteCity.dat 100.9.53.236

This output the country for a given list of IP addresses.
$ echo "100.9.53.236 104.208.39.105 104.53.124.192" > /tmp/list-of-ips.txt
$ output=/tmp/outputfile.csv; echo "ip,country" > $output; for i in $( cat /tmp/list-of-ips.txt ); do echo "$i,\"$( geoiplookup -f /usr/share/GeoIP/GeoIP.dat $i | cut -d' ' -f4-99 )\"" >> $output; done

This output the city for a given list of IP addresses..
$ echo "100.9.53.236 104.208.39.105 104.53.124.192" > /tmp/list-of-ips.txt
$ output=/tmp/outputfile.csv; echo "ip,country" > $output; for i in $( cat /tmp/list-of-ips.txt ); do echo "$i,\"$( geoiplookup -f /usr/share/GeoIP/GeoLiteCity.dat $i | cut -d' ' -f4-99 )\"" >> $output; done

I got the idea from here.
https://www.maketecheasier.com/ip-address-geolocation-lookups-linux/
https://pleasefeedthegeek.wordpress.com/2012/05/09/geolocation-lookups-in-linux-ubuntu/

Mark Cassidy

unread,
Jan 8, 2016, 2:00:27 AM1/8/16
to
On Fri, 08 Jan 2016 06:51:26 +0000, Eef Hartman wrote:

> You would at least need something like the GeoIP database:
> http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
> and a program to search that (I don't know of any).

Thank you for your kind response.

I found exactly the same answer, and just posted it a few minutes after you
did here. I was working in it so I didn't see your response but I do appreciate
that your suggestion seems to be the right direction to use the geoiplookup
command as

$ geoiplookup -f /usr/share/GeoIP/GeoLiteCity.dat 100.9.53.236

What took me a while was I was trying to get these http methods to work instead.
http://ipinfodb.com/linux_script.php
http://netgeo.caida.org/perl/netgeo.cgi?target=192.168.0.1

But I finally gave up on those in favor of geoip.



0 new messages