RSS Subscription
Linux Howtos & Tutorials

Enter your email:

Delivered by


NOTE:New tutorials are from LinuxCareer.com

Poll

Do you own or wish to have iPhone?
 


Linux eBooks FREE Download
A guide to programming Linux kernel modules
Introduction to Linux - A Hands on Guide
A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

Linux: The Hacking Solution (v.3.0)

SQLite 3 with PHP Essential Training – Free Video Training Tutorials

This guide will introduce you to the world of GNU/Linux

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)

Advanced Bash-Scripting Guide

Set up, maintain, and secure a small office email server

Partner Linux Sites:
How-To.LinuxCareer.com
Jobs.LinuxCareer.com
TuxMachines
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
All For Linux

Find by IP - Perl IP to location Example

In the next few lines we are going to describe a process of how to retrieve and geographical information from an IP address . For this we will use a MAxMind Perl API module. This company also provides a data file GeoLiteCity which is free but is less accurate than the paid version. As for a preparation part first we need to download a GeoIP perl module and data file:

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

While in the GeoIP directory the next step is to extract both packages:

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

It should be pointed out that this article assumes that your are running some distribution of Linux with PERL interpreter.

 

At this stage create a file called IPtoLocation.pl in your GeoIP directory with a following code:

 

#!/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"; 
   }

Make IPtoLocation.pl executable:

$ chmod +x IPtoLocation.pl

This simple Perl script accepts a single argument and that is an IP address we wish to convert to a Geographical location. In other words execute the script as below:

./IPtoLocation.pl 8.8.8.8

OUTPUT:

$ ./IPtoLocation.pl 8.8.8.8
US
United States
Mountain View
CA
California
94043
37.4192
-122.0574
807
650

Using a perl CGI this script can be easily converted to something like this: http://linuxconfig.org/cgi-bin/findbyip/geoip.cgi

Share this linux post:

Submit Find by IP - Perl IP to location Example in Delicious Submit Find by IP - Perl IP to location Example in Digg Submit Find by IP - Perl IP to location Example in FaceBook Submit Find by IP - Perl IP to location Example in Google Bookmarks Submit Find by IP - Perl IP to location Example in Stumbleupon Submit Find by IP - Perl IP to location Example in Technorati Submit Find by IP - Perl IP to location Example in Twitter
 
Comments for this page are closed !!!
Please visit our new Linux Forum for additional help or discussion.


Linux eBooks FREE Download