pyasn: Python IP-to-ASN Lookups

repository·master·Indexed 43 days ago

https://github.com/hadiasghari/pyasn

A high-performance Python extension module for offline and historical IP-to-ASN lookups using a C-based radix tree. Features include the ability to perform lookups via asndb.lookup and retrieve prefixes via asndb.get_as_prefixes, as well as utility scripts for downloading and converting MRT/RIB BGP archives into IPASN data files. Supports gzipped data files as of v1.6 to optimize storage.

Tokens
802
Snippets
4
Records
5
Agent score
6%

What's inside pyasn

  1. Optimize pyasn performance and storage

    master

    Storage Optimization

    As of v1.6, you can use gzipped IPASN data files to save disk space. Note that loading gzipped files will result in slightly longer initial load times.

    Performance Tip

    Loading an IPASN data file is the most resource-intensive operation. If you are performing historical lookups across multiple dates, it is highly recommended to cache the loaded data files in memory to avoid repeated heavy loading operations.

  2. Install pyasn via pip or from source

    master

    You can install pyasn easily using pip. If you prefer to build from source, ensure you have pip, setuptools, and build essentials installed.

    Ubuntu/Debian dependencies:

    sudo apt install python3-pip python3-dev python3-setuptools build-essential

    Windows requirements: Building the C module on Windows requires Microsoft Visual C++ (e.g., Visual C++ Express 2010).

    pip3 install pyasn
  3. Create IPASN data files from BGP archives

    master

    IPASN data files are lists of prefixes used for lookups. You can generate these files from MRT/RIB BGP archives using the provided utility scripts.

    1. Download the latest archive: Use pyasn_util_download.py with the --latest or --latestv46 flag.
    2. Convert the archive to a pyasn database: Use pyasn_util_convert.py providing the downloaded RIB file and your desired output filename.

    Note: These scripts are typically installed to /usr/local/bin. If you installed via --user, they may be in ~/.local/bin.

    # Download latest BGP archive
    pyasn_util_download.py --latest
    
    # Convert RIB file to pyasn database
    pyasn_util_convert.py --single <Downloaded RIB File> <ipasn_db_file_name>
  4. Perform IP to ASN lookups with pyasn

    master

    To use pyasn, initialize the module by providing a path to an IPASN data file. You can then perform lookups for specific IP addresses or retrieve all prefixes associated with a specific Autonomous System Number (ASN).

    • pyasn.pyasn(file_path): Initializes the module and loads the database.
    • asndb.lookup(ip_address): Returns a tuple containing (origin_as, bgp_prefix) for the given IP.
    • asndb.get_as_prefixes(asn): Returns a list of BGP prefixes associated with the provided ASN.
    import pyasn
    
    # Initialize module and load IP to ASN database
    asndb = pyasn.pyasn('ipasn_20140513.dat')
    
    # Lookup an IP address
    # Returns: (15169, '8.8.8.0/24')
    result = asndb.lookup('8.8.8.8')
    
    # Get all prefixes for a specific ASN
    # Returns: ['130.161.0.0/16', '131.180.0.0/16', '145.94.0.0/16']
    prefixes = asndb.get_as_prefixes(1128)