faqts : Computers : Programming : Languages : Perl

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

55 of 102 people (54%) answered Yes
Recently 1 of 10 people (10%) answered Yes

Entry

How can I find a clients geographical location from their IP address?

May 16th, 2002 17:40
TJ Mather, Per M Knutsen, peter kelly,


There is no simple one-to-one correspondence between IP addresses and 
the physical location of the host. If the IP address translates to a 
domain name, you can derive the host's TLD (country code) with Perl's 
gethostbyaddr function. This simple script will print out the host name 
of a user supplied address:
#!/usr/bin/perl
use Socket;
$ipaddr = inet_aton(@ARGV);
print gethostbyaddr ($ipaddr, AF_INET)."\n";
Run as ./ip2host.pl xxx.xxx.xxx.xxx
Geo::IP is a Perl module that looks up the country based on IP
address.  To download, go to
http://kobesearch.cpan.org/search?dist=Geo-IP