Skip to main content

A simple utility to lookup IP addresses by MaxMind's GeoIP databases (CLI & WEB).

Project description

ipref

Build and Release Tests

ipref-py is a simple utility to look up IP addresses in MaxMind's GeoIP downloadable databases by command-line and web.

I often look up geolocation data of a number of IP addresses in my research, but existing tools do not satisfy this need. So, I wrote ipref-py.

Note that the web interface must be used for internal use only such as your own environment and your lab. If you make it public, it can be abused for attackers and may also violate terms of use by MaxMind.

Main features:

  • Look up IP addresses by MaxMind's GeoIP databases.
  • Look up hostnames of IP addresses by DNS reverse lookups.
  • A command line and a simple web interface.
  • Configuration by YAML files.
  • Flexible outputs (JSON, JSON-Lines, CSV, TSV).

Requirements

  • Python 3.7-
  • Linux or macOS (It may work in Windows but I have not checked yet.)

Installation

(Optional, but recommended) Create a virtualenv environment for Python.

# /path/to/dir/ depends on your environment. e.g. /opt/ipref/
$ cd /path/to/dir/

# Create a virtualenv
$ python3 -m venv env

# Activate the virtualenv
$ source env/bin/activate

You can install ipref-py via pypi (plus, the wheel file is available from GitHub release page).

# if you want CLI only
(env) $ pip install ipref

# if you want CLI and web interface
(env) $ pip install ipref[web]

Configuration

Make a configuration file. The template files for configurations are available.

Configuration files are loaded from the following paths:

  • ~/.config/ipref.yaml
  • ~/.config/ipref.yml
  • ~/.ipref.yaml
  • ~/.ipref.yml
  • (environment variable IPREF_CONF)
  • (command line argument [CLI only])

The latter file overwrites the former one.

# Copy the configuration template above and edit it.
$ vim ~/.config/ipref.yaml
...(edit)...

CLI Examples

Usage

$ ipref --help

usage: ipref [-h] [-v] [-d] [-c CONFIG] [-I {ip,file}]
             [-O {tsv,json,jsonl,csv}] [--csv-columns CSV_COLUMNS]
             [--csv-exclude-header] [--csv-escape-comma]
             [items ...]

positional arguments:
  items                 IP addresses or filenames. if input_type is file and
                        the items are empty, stdin is used. (default: None)

options:
  -h, --help            show this help message and exit
  -v, --version         show version and exit.
  -d, --debug           enable debug logging to stderr. (default: False)
  -c CONFIG, --config CONFIG
                        path to config file. (default: None)
  -I {ip,file}, --input-type {ip,file}
                        input type. (default: ip)
  -O {tsv,json,jsonl,csv}, --output-format {tsv,json,jsonl,csv}
                        output format. (default: json)
  --csv-columns CSV_COLUMNS
                        [csv|tsv] output columns separated by comma (,).
                        (default: None)
  --csv-exclude-header  [csv|tsv] exclude a csv header. (default: False)
  --csv-escape-comma    [csv|tsv] replace commas (,) to <comma> (useful when
                        using commands such as 'cut'). (default: False)

Example-1: First step

The following example looks up "1.1.1.1" and "8.8.8.8" from command line and dumps the results as CSV format.

$ ipref -O csv "1.1.1.1" "8.8.8.8"
meta.raw_input,meta.ip_address_types,geoip.city.continent.names.en,geoip.city.country.iso_code,geoip.city.country.names.en,geoip.city.city.names.en,geoip.city.postal.code,geoip.city.location.latitude,geoip.city.location.longitude,geoip.asn.autonomous_system_number,geoip.asn.autonomous_system_organization
1.1.1.1,public,,,,,,,,13335,CLOUDFLARENET
8.8.8.8,public,North America,US,United States,,,37.751,-97.822,15169,GOOGLE

Example-2: Flexible output

You can specify columns to be printed in CSV/TSV formats. See output.columns section in config.yaml.orig for the list of column names.

$ ipref -O csv --csv-columns meta.raw_input,geoip.city.country.name "1.1.1.1" "8.8.8.8"
meta.raw_input,geoip.city.country.name
1.1.1.1,
8.8.8.8,United States

You can also set columns in the configuration file.

Example-3: Bulk search

To look up a number of IP addresses, you can pass them through files.

# from stdin
$ cat ip-list.txt | ipref -O csv

# from files
$ ipref -I file -O csv file1.txt file2.txt ...

Web Examples

A simple web app is also avaialble.

Screenshot

Example-4: Launch a web app

Launch ipref-web from command line and access http://localhost:5000/ . A simple web interface like the above figure is shown.

$ IPREF_CONF="path/to/config.yaml" ipref-web run

Input IP addresses, select checkboxes, and click "Search" button! The "Look-up Items" are also configurable by config.yaml.

Example-5: Run as a production WSGI app

Use gunicorn to run ipref-web as a production WSGI app.

$ gunicorn --env IPREF_CONF="path/to/config.yaml" "ipref.web:create_app()"

Template files for systemd (as a unit) and nginx (as a reverse proxy) are also available:

  • Systemd's unit file: ipref-web.service
  • Nginx configuration file: nginx.conf

Alternatives

License

MIT License (link).

Contact

mkt (E-mail: md.irohas at gmail.com)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ipref-0.3.0.tar.gz (40.1 kB view details)

Uploaded Source

Built Distribution

ipref-0.3.0-py3-none-any.whl (40.6 kB view details)

Uploaded Python 3

File details

Details for the file ipref-0.3.0.tar.gz.

File metadata

  • Download URL: ipref-0.3.0.tar.gz
  • Upload date:
  • Size: 40.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.17

File hashes

Hashes for ipref-0.3.0.tar.gz
Algorithm Hash digest
SHA256 b526d936a926a9ac309a39338d72bfeedbdd8198f97c10df00a7c4024d69f5e4
MD5 60a77b1424495251b5d620dd90ad57bc
BLAKE2b-256 441deb5e450e010745d6b65161a6c012376e488791c7092e7d4cc96403822c72

See more details on using hashes here.

File details

Details for the file ipref-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: ipref-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 40.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.17

File hashes

Hashes for ipref-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 01a625d9ecdc55f69ba01e28992c74177aea6f51eb705d8f747fa02a3790d4d8
MD5 cda0d7f89f27a250b34a66b2d659465a
BLAKE2b-256 65c91ffe26dce44875f83b13699f3f8a1cdd62c06e11ebfb759d9aa03fcb6730

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page