Based on PerlMonks posts 1226112 and 1226223 by cavac, this is my attempt in opening the GeoIP2 data from MAXMIND available here
The download section has three CSV databases available:
If you download all three, and create a postgres database geoip
, then
geoip will convert all CSV data to database tables.
$ perl geoip
$ perl geoip 66.39.54.27 209.197.123.153 216.92.34.251
$ perl geoip perlmonks.org
Once the database is filled, the last call might return something like
GeoIP data for 66.39.54.27 - www.perlmonks.com:
CIDR : 66.39.0.0/16
IP range : 66.39.0.0 - 66.39.255.255
Provider : pair Networks
City : Pittsburgh, 508, 15203
Country : US United States
Continent : North America
Location : 40.4254 / -79.9799 (1000) 40°25'31.44" / -79°58'47.64"
https://www.openstreetmap.org/#map=10/40.4254/-79.9799
https://www.google.com/maps/place/@40.4254,-79.9799,10z
Timezone : America/New_York
EU member : No
Satellite : No
Anon Proxy: No
and, when you also use the --dist
option
Using GeoIP to determine own location
GeoIP data for 66.39.54.27 - www.perlmonks.com:
CIDR : 66.39.0.0/16
IP range : 66.39.0.0 - 66.39.255.255
Provider : pair Networks
City : Pittsburgh, 508, 15203
Country : US United States
Continent : North America
Location : 40.4254 / -79.9799 (1000) 40°25'31.44" / -79°58'47.64"
https://www.openstreetmap.org/#map=10/40.4254/-79.9799
https://www.google.com/maps/place/@40.4254,-79.9799,10z
Location : 52.3824 / 4.8995 52°22'56.64" / 4°53'58.20"
Distance : ± 6258.19km
Timezone : America/New_York
EU member : No
Satellite : No
Anon Proxy: No
- perl-5.14.0
- Socket (CORE since per-5.000)
- Archive::Zip
- Text::CSV_XS-1.35
- Net::CIDR
For use of the --dist
option, two additional modules are required.
This functionality is optional, geoip
will work perfectly fine
without these.
Using PostgreSQL:
$ echo "create database geoip;" | psql -f -
$ perl ./geoip --fetch
$ ln geoip ~/bin/
Using SQLite (database will be close to 500 Mb):
$ perl ./geoip --fetch --DB=dbi:SQLite:dbname=geoip.db
or
$ export GEOIP_DBI_DSN=dbi:SQLite:dbname=/my/databases/geoip.db
$ perl ./geoip --fetch
Depending on the amount of memory you have, this might take a while.
You can also fetch the files yourself
$ wget -m -L -nd -np -nH \
http://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN-CSV.zip \
http://geolite.maxmind.com/download/geoip/database/GeoLite2-City-CSV.zip \
http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country-CSV.zip
- IPv6. The current version only implements the IPv4 part. The CSV files however also the IPv6 data, so it should not be too hard to add.
This tool uses the following modules from CORE and CPAN:
- Archive::Zip
- Data::Dumper (core)
- Data::Peek (optional)
- Getopt::Long (core)
- GIS::Distance (optional)
- HTML::TreeBuilder (optional)
- LWP::Simple
- LWP::UserAgent (optional)
- Math::Trig (core)
- Net::CIDR
- Socket (core)
- Text::CSV_XS
Modules on CPAN with similar and/or related functionality:
- Geo::Coder::HostIP
- Geo::IP - from maxmind
- Geo::IP2Location
- Geo::IP2Proxy
- Geo::IP6
- Geo::IPfree - using software77 database
- Geo::IP::RU::IpGeoBase
- IP::Country
- IP::Country::DB_File
- IP::Country::DNSBL
- IP::Info
- IP::Location
- IP::QQWry
- IP::World
- Metabrik::Lookup::Iplocation
- Pcore::GeoIP
Check CPAN for more
Thanks to cavac for the inspiration
H.Merijn Brand h.m.brand@xs4all.nl
The GeoLite2 end-user license agreement, which incorporates components of the Creative Commons Attribution-ShareAlike 4.0 International License 1) can be found here 2). The attribution requirement may be met by including the following in all advertising and documentation mentioning features of or use of this database.
This tool uses, but does not include, the GeoLite2 data created by MaxMind, available from http://www.maxmind.com.
Copyright (C) 2018-2020 H.Merijn Brand. All rights reserved.
This library is free software; you can redistribute and/or modify it under the same terms as Perl itself. See here 3).