How to convert IP to Country location using Perl and Geo::IPfree
Details
admin
Uncategorised
This config will provide you with a simple perl script which can be used to lookup country name from IP address. First we need to install libgeo-ipfree-perl perl library:
Our ip2location.pl script accepts a single command line argument and that is the IP address we would like to convert/lookup to country name. For example we execute the script with an argument 213.213.65.125:
./ip2location.pl 213.213.65.125
Country: Italy
Country Code: IT
The above script can be used as a simple command line tool to convert IP address to a Country location or it can be also use for example to analyze Apache access.log and convert all IP addresses within to a country location:
$ for i in $( awk '{ print $1} ' access.log | sort | uniq ); do perl ip2location.pl $i; done
Furthermore, perl's Geo::IPfree library can also be used to lookup hostnames:
$ ./ip2location.pl gnu.org
Country: United States
Contry Code: US