Giter Club home page Giter Club logo

ipdetailscache's Introduction

ipdetailscache

Build Status PYPI Version Python Versions

A Python library to gather IP address details (ASN, prefix, resource holder, reverse DNS) using the RIPEStat API, with a basic cache to avoid flood of requests and to enhance performances.

Requirements / Third-party Libraries

Part of this work is based on Google Python IP address manipulation library (https://code.google.com/p/ipaddr-py/) and Jeff Ferland IPy library (https://github.com/autocracy/python-ipy).

You need either ipaddr or IPy; versions >=0.2 of this library try to import ipaddr, then fall back on IPy.

You can install them using pip:

  • pip install ipaddr
  • pip install IPy

The pip packaged version installs the IPy library.

Installation

  • Using pip:

    pip install ipdetailscache
  • Manually, by cloning this repository:

    git clone https://github.com/pierky/ipdetailscache.git

Tests

Some tests are provided within the tests directory. You can run them with nosetests -vs.

Usage

Import the library, then setup a cache object and use it to gather IP address details. The cache object will automatically load and save data to the local cache files.

Optionally, the cache object may be instantiated with the following arguments:

  • IP_ADDRESSES_CACHE_FILE, path to the file where IP addresses cache will be stored (default: "ip_addr.cache");
  • IP_PREFIXES_CACHE_FILE, path to the file where IP prefixes cache will be stored (default: "ip_pref.cache");
  • MAX_CACHE, expiration time for cache entries, in seconds (default: 604800, 1 week);
  • dont_save_on_del, avoid to save the cache on __del__ (default: False, so it saves the cache);
  • Debug, set to True to enable some debug messages (default: False).

IP_ADDRESSES_CACHE_FILE and IP_PREFIXES_CACHE_FILE can be set to None to avoid persistent storage of the cache on files.

Internet Exchange Points (IXPs) information

Starting from version 0.3.0, results can be enriched with Internet Exchange Points (IXPs) IP address space information. This feature is based on PeeringDB.com (www.peeringdb.com) API.

To enable IXPs info gathering, call the UseIXPs method of the cache.

Results are given in a dictionary containing the following keys:

- ASN           ["<ASN>" | "unknown" | "not announced"]
- Holder        "string"
- Prefix        "string"
- HostName      "string"
- IsIXP         [ None | bool ]
- IXPName       "string"
- TS            int

Hostname is obtained using the local socket.getfqdn function.

Usage example:

from pierky.ipdetailscache import IPDetailsCache
cache = IPDetailsCache( IP_ADDRESSES_CACHE_FILE = "ip_addr.cache", IP_PREFIXES_CACHE_FILE = "ip_pref.cache", MAX_CACHE = 604800, Debug = False )
cache.UseIXPs( WhenUse=1, IXP_CACHE_FILE="ixps.cache", MAX_CACHE=604800 )
result = cache.GetIPInformation( "IP_ADDRESS" )

The WhenUse argument of UseIXPs method has this meaning:

  • 0: do not use IXPs info;
  • 1: use IXPs info only when can't determine ASN (unknown or not announced)
  • 2: always use IXPs info.

Examples

:~# python
Python 2.7.2+ (default, Jul 20 2012, 22:15:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pierky.ipdetailscache import IPDetailsCache
>>> cache = IPDetailsCache()
>>> result = cache.GetIPInformation( "193.0.6.139" )
>>> result
{'HostName': 'www.ripe.net', 'TS': 1453068601, 'Prefix': u'193.0.0.0/21', 'IsIXP': None, 'IXPName': '', 'Holder': u'RIPE-NCC-AS Reseaux IP Europeens Network Coordination Centre (RIPE NCC),NL', 'ASN': '3333'}

Example with UseIXPs, WhenUse=1 (default)

80.81.203.4 = DE-CIX Hamburg IP, not announced

80.249.208.1 = AMS-IX IP, announced

>>> cache.UseIXPs()
>>> decix_ip="80.81.203.4"     # DE-CIX Hamburg IP, not announced
>>> amsix_ip="80.249.208.1"    # AMS-IX IP, announced
>>> result = cache.GetIPInformation(decix_ip)
>>> result
{'HostName': 'ge1-1-12-br2.hamburg10.iphh.net', 'TS': 1453068691, 'Prefix': u'80.81.203.4', 'IsIXP': True, 'IXPName': u'DE-CIX Hamburg', 'Holder': '', 'ASN': 'not announced'}
>>> result = cache.GetIPInformation(amsix_ip)
>>> result
{'HostName': 'rtr-eun-01.ams-ix.net', 'TS': 1453068704, 'Prefix': u'80.249.208.0/21', 'IsIXP': None, 'IXPName': '', 'Holder': u'AMS-IX1 Amsterdam Internet Exchange B.V.,NL', 'ASN': '1200'}

AMS-IX IP is announced, so IsIXP is None because no IXP info have been used here.

Example with UseIXPs, WhenUse=2

Clear local cache with rm *.cache, then:

>>> from pierky.ipdetailscache import IPDetailsCache
>>> cache = IPDetailsCache()
>>> cache.UseIXPs(WhenUse=2)
>>> decix_ip="80.81.203.4"     # DE-CIX Hamburg IP, not announced
>>> amsix_ip="80.249.208.1"    # AMS-IX IP, announced
>>> result = cache.GetIPInformation(decix_ip)
>>> result
{'HostName': 'ge1-1-12-br2.hamburg10.iphh.net', 'TS': 1453068812, 'Prefix': u'80.81.203.4', 'IsIXP': True, 'IXPName': u'DE-CIX Hamburg', 'Holder': '', 'ASN': 'not announced'}
>>> result = cache.GetIPInformation(amsix_ip)
>>> result
{'HostName': 'rtr-eun-01.ams-ix.net', 'TS': 1453068956, 'Prefix': u'80.249.208.0/21', 'IsIXP': True, 'IXPName': u'AMS-IX', 'Holder': u'AMS-IX1 Amsterdam Internet Exchange B.V.,NL', 'ASN': '1200'}

Here, even if AMS-IX announces its peering LAN prefix, IXPs info have been used to enrich results because WhenUse is 2.

>>> result = cache.GetIPInformation( "193.0.6.139" )
>>> result
{'HostName': 'www.ripe.net', 'TS': 1453068965, 'Prefix': u'193.0.0.0/21', 'IsIXP': False, 'IXPName': '', 'Holder': u'RIPE-NCC-AS Reseaux IP Europeens Network Coordination Centre (RIPE NCC),NL', 'ASN': '3333'}

The www.ripe.net IP is not on an IXPs peering LAN, so IsIXP == False.

Bug? Issues?

Have a bug? Please create an issue on GitHub at https://github.com/pierky/ipdetailscache/issues

Author

Pier Carlo Chiodi - https://pierky.com

Blog: https://blog.pierky.com

Twitter: @pierky http://twitter.com/pierky

ipdetailscache's People

Contributors

pierky avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.