Find an Exact GeoLocation of an IP Address

Hi everyone, after long time, today we’ll learn how we can find an exact GeoLocation from an IP Address or a URL. Here, in this article, we’ll use a MAxMind Perl API module to retrieve the exact location of the device connected with that IP Address. Here, we’ll gonna use a perl module called GeoIP Perl Module to retrive the information using a database called GeoIP Lite City. These datasets allows us to easily retrieve all the necessary information required to find the exact Geo Location of a place using IP Address. It also has latitude, longitude, and time zone data as an extra point.

Here are some simple and easy steps to find an exact Geo Location from an IP Address with the help of GeoIP perl module.

 1. Downloading GeoIP Packages

First of all, we’ll need to download the required packages ie GeoIP and GeoLiteCity database. This GeoIP is a perl module with will utilize the GeoLiteCity Database. To download the both package, we’ll need to run the following command.

$ cd ~/; mkdir GeoIP; cd GeoIP
$ wget http://geolite.maxmind.com/download/geoip/api/perl/Geo-IP-1.38.tar.gz

–2015-07-04 02:00:10– http://geolite.maxmind.com/download/geoip/api/perl/Geo-IP-1.38.tar.gz
Resolving geolite.maxmind.com (geolite.maxmind.com)… 141.101.115.190, 141.101.114.190, 2400:cb00:2048:1::8d65:72be, …
Connecting to geolite.maxmind.com (geolite.maxmind.com)|141.101.115.190|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 63418 (62K) [application/octet-stream]
Saving to: ‘Geo-IP-1.38.tar.gz’

100%[====================================>] 63,418 54.1KB/s in 1.1s

2015-07-04 02:00:12 (54.1 KB/s) – ‘Geo-IP-1.38.tar.gz’ saved [63418/63418]

Then, we’ll gonna download the dataset of the location ie GeoLiteCity using wget command.

$ wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

-2015-07-04 02:01:42– http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
Resolving geolite.maxmind.com (geolite.maxmind.com)… 141.101.114.190, 141.101.115.190, 2400:cb00:2048:1::8d65:73be, …
Connecting to geolite.maxmind.com (geolite.maxmind.com)|141.101.114.190|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 10223409 (9.7M) [application/octet-stream]
Saving to: ‘GeoLiteCity.dat.gz’
100%[====================================>] 10,223,409 27.4KB/s in 3m 14s
2015-07-04 02:04:57 (51.4 KB/s) – ‘GeoLiteCity.dat.gz’ saved [10223409/10223409]

2. Extracting Packages

Now, after we have successfully downloaded the package we’ll need to extract both packages as shown below.

$ tar xzf Geo-IP-1.38.tar.gz
$ gunzip GeoLiteCity.dat.gz

3. Creating a Perl Script

As perl is pre-installed in most the distributions of Linux, we’ll consider its installed in our machine. So, now we’ll gonna create a file called IPtoLocation.pl in our GeoIP directory using a text editor as shown below.

$ nano IPtoLocation.pl

Then, we’ll need to add the following lines into it.

#!/usr/bin/perl
use lib “Geo-IP-1.38/lib/”;
use Geo::IP;
my $gi = Geo::IP->open( “GeoLiteCity.dat”, GEOIP_STANDARD );
my $r = $gi->record_by_name($ARGV[0]);
if ($r) {
print join( “\n”,
$r->country_code, $r->country_name, $r->city,
$r->region, $r->region_name, $r->postal_code,
$r->latitude, $r->longitude, $r->metro_code,
$r->area_code )
. “\n”;
}
else {
print “Location of this IP Address is NOT defined !\n”;
}

Then, we’ll wanna save the file and exit the text editor.

Making the file executable

Now, we’ll need to make the file IPtoLocation.pl as executable so that we can execute it and run our required arguments.

$ chmod +x IPtoLocation.pl

Running the Script

Finally, after everything is done, we’ll now gonna run our simple Perl script that we just wrote. This perl script only accepts a single argument and that is an IP address or a server’s domain name we wish to convert to a Geographical location. To do so, we’ll need to run the following command in a terminal or a shell.

$ ./IPtoLocation.pl fosslovers.com

GB
United Kingdom
51.5000
-0.1300

$ ./IPtoLocation.pl 8.8.8.8

US
United States
Mountain View
CA
California
94040
37.3860
-122.0838
807
650

geoip-location-track
geoip-location-track

Conclusion

Locating an exact location of a device or a person is pretty easy and simple with the help of this Perl module named GeoIP. GeoIP utilizes perl and a dataset which helps it to recognize and locate the exact location with the longitude, latitude of the place where the public IP address is assigned to. As the Geographical exact place is defined by longitude and latitude, this small perl module is highly effective if we want to track someone’s location with the IP Address. This little perl script is very clever and helpful indeed. If you have any questions, suggestions, feedback please write them in the comment box below so that we can improve or update our contents. Thank you ! Enjoy 🙂

1 thought on “Find an Exact GeoLocation of an IP Address”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.