back to index

IP-to-location

Purpose

ip2loc: Determining geographical position from an IP address. Designed for easy use in scripts.
ipdiffmask: Converting between 192.168.1.0-192.168.1.255 and 192.168.1.0/8 style notations.

Mechanism of work

Program uses a file with a table of IP ranges vs the countries.
Inspired by mod_geo, from where I "borrowed" the location and country code files.

Usage

Usage: ip2loc [-i] [-c] IP ...
Shows countries or country codes for IP addresses.

Options:
   -i          show IP
   -c          show country as code instead as name
   -C          do not show country
   -r          show IP range
   -m          show all matching ranges
   -n          do not show trailing \n
   -L <code>   show country code <code>
   -R <code>   show IP ranges for country <code>
   -G <file>   use <file> as geolocation file instead of /usr/share/ip-geo.ids

When IP is '-', the addresses are read from stdin.

When the IPs are read from stdin, the rest of the line after the IP address is ignored, making this tool suitable for processing scripts.

Examples:

ip2loc 1.2.3.4   Determine country of 1.2.3.4
ip2loc -r -C 1.2.3.4   Show only IP range 1.2.3.4 belongs to
ip2loc -c 1.2.3.4 2.3.4.5   Determine country of 1.2.3.4 and 2.3.4.5, show as code
ip2loc -m -r 1.2.3.4   Show ranges for all matching entries, not only the closest match
ip2loc -L CZ   Show the country with code CZ
ip2loc -R CZ   List all the known IP ranges of CZ

You came from , which is Usage: /bin/shad/ip2loc Queries http://phpweby.com/services/iplocation and parses the output. IP address locator is using Maxmind GeoLite's database for finding the location of the IP address. and belongs to segment IP address: .

ipdiffmask

Part of the project is a very simple utility for transcoding network IP ranges between the IP/mask and IP..IP notations.

Examples:

ipdiffmask 192.168.1.0/8   Shows 192.168.1.0-192.168.1.255
ipdiffmask 192.168.32.0-192.168.36.255   Shows 192.168.32.0/21

Caution

The file is out of date. Accuracy is not always guaranteed.

TODO

Get the IP ranges list from http://sunshine.globalone.no/ipindex/ and convert them to the format suitable for ip2loc.

Add databases from http://www.maxmind.com/download/geoip/database/

Files

ip2loc.c - the program source itself
ip-geo.ids - the IP-to-country map file. Goes to /usr/share/ip-geo.ids
country-codes.txt - the country-code-to-name file. Goes to /usr/share/country-codes.txt

ipdiffmask.c - source of ipdiffmask utility


If you have any comments or questions about the topic, please let me know here:
Your name:
Your email:
Spambait
Leave this empty!
Only spambots enter stuff here.
Feedback: