Skip to main content

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.

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)

Metadata

Release files for ipref 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ipref 0.3.0
File Size Uploaded Provenance Actions
ipref-0.3.0.tar.gz 40.1 kB Without provenance Details for ipref-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ipref 0.3.0
File Interpreter ABI Platform Actions
ipref-0.3.0-py3-none-any.whl Python 3 none any Details for ipref-0.3.0-py3-none-any.whl

Total release size: 80.7 kB

Release files / ipref-0.3.0.tar.gz

Download URL ipref-0.3.0.tar.gz
Size 40.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b526d936a926a9ac309a39338d72bfeedbdd8198f97c10df00a7c4024d69f5e4
BLAKE2b-256 checksum
How to use checksums
441deb5e450e010745d6b65161a6c012376e488791c7092e7d4cc96403822c72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.17

Release files / ipref-0.3.0-py3-none-any.whl

Download URL ipref-0.3.0-py3-none-any.whl
Size 40.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
01a625d9ecdc55f69ba01e28992c74177aea6f51eb705d8f747fa02a3790d4d8
BLAKE2b-256 checksum
How to use checksums
65c91ffe26dce44875f83b13699f3f8a1cdd62c06e11ebfb759d9aa03fcb6730
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.17

Security for ipref 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page