IP Country Geolocation

Contents

  1. Input
  2. Output
  3. Usage
  4. Download

This small piece of software does IP country geolocation using the free database provided here (look in the lower-right of that page for the download link). This program is designed for use on lists of thousands to a few million IPs. It's not very effective for quickly looking up one or two IPs because it takes a couple of seconds to read the database into memory.

This is designed to produce aggregated stats, and was written with analyzing web traffic in mind. Thus, it does not tell you which country any particular IP belongs to, but will, for example, tell you what percentage of the IPs are from each country.

Please see the bottom of the page for the download link.

Input

Provide it with a list of IP addresses on standard input, one per line:

200.25.194.154
218.194.80.230
141.20.70.200
61.110.143.2
219.159.67.187
218.194.80.228
218.194.80.230
62.231.245.5
62.231.245.5
72.55.188.250
218.194.80.228
218.194.80.230

Output

The program prints a total count for each country, the count as a percentage of the total, and a list of the frequencies for each distinct IP address from that country, sorted most to least. Everything is space-delimited so it's easy to import into a spreadsheet program.

For example, this output (generated from the above input), says that six of the input IPs were from China. There are only three distinct IPs in those six inputs. One appeared three times, one twice, and one once.

Reading database... ready.
12 IPs read.
1 8.33% CANADA 1
6 50.00% CHINA 3 2 1
1 8.33% ECUADOR 1
1 8.33% EUROPEAN_UNION 1
1 8.33% KOREA_REPUBLIC_OF 1
2 16.67% OMAN 2

If you want the output sorted by country, just pipe it through sort -n.

Usage

This is a command-line app. You need to provide it with one argument, which is the location of the CSV file available at the site mentioned above (here it is again). You can then enter your input, one IP per line, either by manually typing it, pasting it, redirecting/piping it, etc. For example:

$ java -jar ipcountry.jar IpToCountry.csv < list-of-ips.txt

If you use Apache, for example, piping the IPs of visitors is simple since the IP is just the first thing in the file:

 $ cut -f1 -d' ' access_log | java -jar ipcountry.jar IpToCountry.csv

Download

This software is available under GPLv3. This JAR file contains binaries as well as the source, in case you want to hack.

Comments, questions, and feedback are welcome. Please use the contact link at the top right corner of this page.