Skip to content
/ GeoIP2 Public

Convert maxmind GeoIP2 CSV to Postgres + CLI

Notifications You must be signed in to change notification settings

Tux/GeoIP2

Repository files navigation

GeoIP2

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.

Use

 $ 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

PREREQUISITES

For use of the --dist option, two additional modules are required. This functionality is optional, geoip will work perfectly fine without these.

INSTALLATION

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

TODO

  • 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.

SEE ALSO

This tool uses the following modules from CORE and CPAN:

Modules on CPAN with similar and/or related functionality:

Check CPAN for more

THANKS

Thanks to cavac for the inspiration

AUTHOR

H.Merijn Brand hmbrand@cpan.org

COPYRIGHT AND LICENSE

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-2024 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).

  1. https://creativecommons.org/licenses/by-sa/4.0/
  2. https://www.maxmind.com/en/geolite2/eula
  3. https://opensource.org/licenses/Artistic-2.0

About

Convert maxmind GeoIP2 CSV to Postgres + CLI

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages