Geo::IPfree - Look up country of IP Address. This module make this off-line and the DB of IPs is free & small.
use Geo::IPfree; my ($country,$country_name) = Geo::IPfree::LookUp("192.168.0.1") ; ... or ... use Geo::IPfree qw(LookUp) ; my ($country,$country_name) = LookUp("18.104.22.168") ; ... or ... use Geo::IPfree; my $GeoIP = Geo::IPfree->new('/GeoIPfree/ipscountry.dat') ; $GeoIP->Faster ; ## Enable the faster option. my ($country,$country_name,$ip) = $GeoIP->LookUp("www.cnn.com") ; ## Getting by Hostname. $GeoIP->LoadDB('/GeoIPfree/ips.dat') ; my ($country,$country_name,$ip) = $GeoIP->LookUp("www.sf.net") ; ## Getting by Hostname. ... or ... use Geo::IPfree; my $GeoIP = Geo::IPfree::new() ; ## Using the default DB! my ($country,$country_name) = $GeoIP->LookUp("22.214.171.124") ;
This package comes with it's own database to look up the IP's country, and is totally free. Take a look in CPAN for updates...
Load the database to use to LookUp the IPs.
Returns the ISO 3166 country (XX) code for an IP address or Hostname.
**If you send a Hostname you will need to be connected to the internet to resolve the host IP.
Clean the memory used by the cache.
Make the LookUp() faster, good for big amount of LookUp()s. This will load all the DB in the memory (200Kb) and read from there, not from HD (good way for slow HD or network disks), but use more memory. The module "Memoize" will be enabled for some internal functions too.
Note that if you make a big amount of querys to LookUp(), in the end the amount of memory can be big, than is better to use more memory from the begin and make all faster.
The database file in use.
The database file handler.
The database file path.
Set/tell if the cache of LookUp() is on. If it's on it will cache the last 1000 querys. Default: 1
The cache is good when you are parsing a list of IPs, generally a web log. If in the log you have many lines with the same IP, GEO::IPfree don't need to make a full search for each query, it will cache the last 1000 different IPs. After each 1000 IPs the cache is cleaned to restart it.
Note that the Lookup make the query without the last IP number (xxx.xxx.xxx.0), then the cache for the IP 192.168.0.1 will be the same for 192.168.0.2 (they are the same query, 192.168.0.0).
the DB has a list of IP ranges & countrys, for example, from 126.96.36.199 to 188.8.131.52 the IPs are from BR. To make a fast access to the DB the format try to use less bytes per input (block). The file was in ASCII and in blocks of 7 bytes: XXnnnnn
XX -> the country code (BR,US...) nnnnn -> the IP range using a base of 85 digits (not in dec or hex to get space).
See CPAN for updates of the DB...
The file ipscountry.dat is made only for Geo::IPfree and has their own format. To convert it see the tool "ipct2txt.pl" in the same path of Geo/IPfree.pm.
0.2 - Sat Mar 22 18:10 2003
- Change sysread() to read() for better portability. - Speed improvement for multiples LookUp(). 4 times faster!
0.01.1 - Nov 6 14:05:03 2002 (not released on CPAN)
- Fix seek bug for Perl 5.6.0 on multiples LookUp().
Graciliano M. P. <firstname.lastname@example.org>.
Thanks to Laurent Destailleur (author of AWStats) that tested it on many OS and fixed bugs for them, like the not portable sysread, and asked for some speed improvement.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.