pyasn
=====
[![PyPI version](https://badge.fury.io/py/pyasn.svg)](https://badge.fury.io/py/pyasn)
**pyasn** is a Python extension module that enables very fast IP address to Autonomous System Number lookups.
Current state and Historical lookups can be done, based on the MRT/RIB BGP archive used as input.
*pyasn* is different from other ASN lookup tools in that it provides **offline** and **historical** lookups.
It provides utility scripts for users to build their own lookup databases based on any MRT/RIB archive.
This makes *pyasn* much faster than online dig/whois/json lookups.
The module is written in C and Python, and cross-compiles on Linux and Windows. Underneath, it uses a radix tree
data structure for storage of IP addresses. In the current version, it borrows code from *py-radix* to support
both IPV4 and IPV6 network prefixes. Compared to the previous version, it provides
support for Python 2 and 3; adds new functionality, performance improvements, and unit-tests.
*pyasn* has been developed by [Hadi Asghari](https://hadiasghari.com) and
[Arman Noroozian](https://anoroozian.nl/) during their PhD research on cybersecurity measurements. The package is used widely and bugs are fixed quickly. Please report any bugs via GitHub [issues](https://github.com/hadiasghari/pyasn/issues).
Installation
============
Installation is a breeze via pip: ::
pip3 install pyasn
Or with the standard Python: ::
python3 setup.py build
python3 setup.py install --record log
You will need to have pip, setuptools and build essentials installed if you build the package manually. On
Ubuntu/Debian you can get them using the following command: ::
sudo apt install python3-pip python3-dev python3-setuptools build-essential
Building the C module on Windows, using either pip or from source, requires Microsoft Visual C++ to be installed.
pyasn has been tested using Visual C++ Express 2010, available freely from Microsoft's website, on both the
official Python 3.4 release and Miniconda3. Other versions of Python, Visual Studio, and Cygwin could also work
with minor modifications.
Usage
=====
A simple example that demonstrates most of the features: ::
import pyasn
# Initialize module and load IP to ASN database
# the sample database can be downloaded or built - see below
asndb = pyasn.pyasn('ipasn_20140513.dat')
asndb.lookup('8.8.8.8')
# should return: (15169, '8.8.8.0/24'), the origin AS, and the BGP prefix it matches
asndb.get_as_prefixes(1128)
# returns ['130.161.0.0/16', '131.180.0.0/16', '145.94.0.0/16'], TU-Delft prefixes
IPASN Data Files
================
IPASN data files are a long list of prefixes used to lookup AS number for IPs. An excerpt from such a file looks
like this: ::
; IP-ASN32-DAT file
; Original file : <Path to a rib file>
; Converted on : Tue May 13 22:03:05 2014
; CIDRs : 512490
;
1.0.0.0/24 15169
1.0.128.0/17 9737
1.0.128.0/18 9737
1.0.128.0/19 9737
1.0.129.0/24 23969
...
IPASN data files can be created by downloading MRT/RIB BGP archives from Routeviews (or similar sources),
and parsing them using provided scripts that tail the BGP AS-Path. This can be done simply as follows: ::
pyasn_util_download.py --latest # or --latestv46
pyasn_util_convert.py --single <Downloaded RIB File> <ipasn_db_file_name>
**NOTE:** These scripts are by default installed to ``/usr/local/bin`` and can be executed directly. If you installed
the package to a user directory (using `--user`), these scripts might not be on the path. In such a case invoke them from
the directory in which they have been copied (e.g. ``~/.local/bin``).
**New in v1.6:** To save disk space, you can gzip IPASN data files. The load time will be slighlty longer.
Performance Tip
===============
Initial loading of a IPASN data file is the most heavy operation of the package. For fast lookups using multiple
IPASN data files, for instance for historical lookups on multiple dates, we recommend caching of loaded data files
for better performance.
Uninstalling pyasn
==================
You can remove *pyasn* as follows: ::
pip3 uninstall pyasn
If this command generates an error, update your `pip` package to the latest version first.
Package Structure
=================
The main portions of the directory tree are as follows: ::
.
├── pyasn/__init__.py # Python code of the main pyasn module
├── pyasn/pyasn_radix.c # C extension code (Python RADIX module with bulk load)
├── pyasn/_radix/* # C extension code (Based on original RADIX code from MRTd)
├── pyasn/mrtx.py # python module used to convert MRT files to pyasn DB files
├── pyasn-utils/*.py # Scripts to download & convert BGP MRT dumps to IPASN databases
├── data/ # Test Resources and some sample DBs to use
├── tests/ # Tests
└── setup.py # Standard setup.py for installation/testing/etc.
Testing pyasn Sources
=====================
A number of unit tests are provided in the ``tests/`` directory when downlading from source. The tests can be run using the `pytest` command (`pytest-3` in Ubuntu).
This release has been tested under python version 2.6, 2.7, and 3.3 to 3.11. We appreciate contributions towards
testing and extending *pyasn*!
License & Acknowledgments
=========================
*pyasn* is licensed under the MIT license.
It extends code from py-radix (Michael J. Schultz and Damien Miller), and improves upon it in several ways, for
instance in lowering memory usage and adding bulk prefix/origin load. The underlying radix tree implementation is
taken (and modified) from MRTd. These are all subject to their respective licenses. Please see the LICENSE file
for details.
Thanks to Dr. Chris Lee (of Shadowserver) for proposing the use of radix trees.
A handful of GitHub developers have contributed features and bug fixes to the latest releases.
Many thanks to all of them.
Raw data
{
"_id": null,
"home_page": "https://github.com/hadiasghari/pyasn",
"name": "pyasn",
"maintainer": "Hadi Asghari",
"docs_url": null,
"requires_python": "",
"maintainer_email": "hd.asghari@gmail.com",
"keywords": "ip asn autonomous system bgp whois prefix radix python routing networking",
"author": "",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/a1/55/881259e00d62ac11713567ada5152b0ad3a744e9f12d5d1f115d40bf17d9/pyasn-1.6.2.tar.gz",
"platform": null,
"description": "pyasn\n=====\n\n[![PyPI version](https://badge.fury.io/py/pyasn.svg)](https://badge.fury.io/py/pyasn)\n\n\n**pyasn** is a Python extension module that enables very fast IP address to Autonomous System Number lookups.\nCurrent state and Historical lookups can be done, based on the MRT/RIB BGP archive used as input.\n\n*pyasn* is different from other ASN lookup tools in that it provides **offline** and **historical** lookups.\nIt provides utility scripts for users to build their own lookup databases based on any MRT/RIB archive.\nThis makes *pyasn* much faster than online dig/whois/json lookups.\n\nThe module is written in C and Python, and cross-compiles on Linux and Windows. Underneath, it uses a radix tree\ndata structure for storage of IP addresses. In the current version, it borrows code from *py-radix* to support\nboth IPV4 and IPV6 network prefixes. Compared to the previous version, it provides\nsupport for Python 2 and 3; adds new functionality, performance improvements, and unit-tests.\n\n*pyasn* has been developed by [Hadi Asghari](https://hadiasghari.com) and\n[Arman Noroozian](https://anoroozian.nl/) during their PhD research on cybersecurity measurements. The package is used widely and bugs are fixed quickly. Please report any bugs via GitHub [issues](https://github.com/hadiasghari/pyasn/issues).\n\n\nInstallation\n============\nInstallation is a breeze via pip: ::\n\n pip3 install pyasn\n\nOr with the standard Python: ::\n\n python3 setup.py build\n python3 setup.py install --record log\n\nYou will need to have pip, setuptools and build essentials installed if you build the package manually. On\nUbuntu/Debian you can get them using the following command: ::\n\n sudo apt install python3-pip python3-dev python3-setuptools build-essential\n\nBuilding the C module on Windows, using either pip or from source, requires Microsoft Visual C++ to be installed.\npyasn has been tested using Visual C++ Express 2010, available freely from Microsoft's website, on both the\nofficial Python 3.4 release and Miniconda3. Other versions of Python, Visual Studio, and Cygwin could also work\nwith minor modifications.\n\n\n\nUsage\n=====\nA simple example that demonstrates most of the features: ::\n\n import pyasn\n\n # Initialize module and load IP to ASN database\n # the sample database can be downloaded or built - see below\n asndb = pyasn.pyasn('ipasn_20140513.dat')\n\n asndb.lookup('8.8.8.8')\n # should return: (15169, '8.8.8.0/24'), the origin AS, and the BGP prefix it matches\n\n asndb.get_as_prefixes(1128)\n # returns ['130.161.0.0/16', '131.180.0.0/16', '145.94.0.0/16'], TU-Delft prefixes\n\n\nIPASN Data Files\n================\nIPASN data files are a long list of prefixes used to lookup AS number for IPs. An excerpt from such a file looks\nlike this: ::\n\n ; IP-ASN32-DAT file\n ; Original file : <Path to a rib file>\n ; Converted on : Tue May 13 22:03:05 2014\n ; CIDRs : 512490\n ;\n 1.0.0.0/24\t15169\n 1.0.128.0/17\t9737\n 1.0.128.0/18\t9737\n 1.0.128.0/19\t9737\n 1.0.129.0/24\t23969\n ...\n\nIPASN data files can be created by downloading MRT/RIB BGP archives from Routeviews (or similar sources),\nand parsing them using provided scripts that tail the BGP AS-Path. This can be done simply as follows: ::\n\n pyasn_util_download.py --latest # or --latestv46 \n pyasn_util_convert.py --single <Downloaded RIB File> <ipasn_db_file_name>\n\n\n**NOTE:** These scripts are by default installed to ``/usr/local/bin`` and can be executed directly. If you installed\nthe package to a user directory (using `--user`), these scripts might not be on the path. In such a case invoke them from \nthe directory in which they have been copied (e.g. ``~/.local/bin``).\n\n**New in v1.6:** To save disk space, you can gzip IPASN data files. The load time will be slighlty longer.\n\n\nPerformance Tip\n===============\nInitial loading of a IPASN data file is the most heavy operation of the package. For fast lookups using multiple\nIPASN data files, for instance for historical lookups on multiple dates, we recommend caching of loaded data files\nfor better performance.\n\n\nUninstalling pyasn\n==================\nYou can remove *pyasn* as follows: ::\n\n pip3 uninstall pyasn\n\n\nIf this command generates an error, update your `pip` package to the latest version first.\n\n\n\nPackage Structure\n=================\nThe main portions of the directory tree are as follows: ::\n\n .\n \u251c\u2500\u2500 pyasn/__init__.py # Python code of the main pyasn module\n \u251c\u2500\u2500 pyasn/pyasn_radix.c # C extension code (Python RADIX module with bulk load)\n \u251c\u2500\u2500 pyasn/_radix/* # C extension code (Based on original RADIX code from MRTd)\n \u251c\u2500\u2500 pyasn/mrtx.py # python module used to convert MRT files to pyasn DB files\n \u251c\u2500\u2500 pyasn-utils/*.py # Scripts to download & convert BGP MRT dumps to IPASN databases\n \u251c\u2500\u2500 data/ # Test Resources and some sample DBs to use\n \u251c\u2500\u2500 tests/ # Tests\n \u2514\u2500\u2500 setup.py # Standard setup.py for installation/testing/etc.\n\n\n\nTesting pyasn Sources\n=====================\nA number of unit tests are provided in the ``tests/`` directory when downlading from source. The tests can be run using the `pytest` command (`pytest-3` in Ubuntu). \n\nThis release has been tested under python version 2.6, 2.7, and 3.3 to 3.11. We appreciate contributions towards\ntesting and extending *pyasn*!\n\n\nLicense & Acknowledgments\n=========================\n*pyasn* is licensed under the MIT license.\n\nIt extends code from py-radix (Michael J. Schultz and Damien Miller), and improves upon it in several ways, for\ninstance in lowering memory usage and adding bulk prefix/origin load. The underlying radix tree implementation is\ntaken (and modified) from MRTd. These are all subject to their respective licenses. Please see the LICENSE file\nfor details.\n\nThanks to Dr. Chris Lee (of Shadowserver) for proposing the use of radix trees.\n\nA handful of GitHub developers have contributed features and bug fixes to the latest releases.\nMany thanks to all of them.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Offline IP address to Autonomous System Number lookup module.",
"version": "1.6.2",
"project_urls": {
"Homepage": "https://github.com/hadiasghari/pyasn"
},
"split_keywords": [
"ip",
"asn",
"autonomous",
"system",
"bgp",
"whois",
"prefix",
"radix",
"python",
"routing",
"networking"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "a155881259e00d62ac11713567ada5152b0ad3a744e9f12d5d1f115d40bf17d9",
"md5": "f1e565a530fb748b0a36400914204c47",
"sha256": "a2d55fb3ee47947609f50211ca5b0bac411a86f3c935fb92ca4b0b8ab7c668ff"
},
"downloads": -1,
"filename": "pyasn-1.6.2.tar.gz",
"has_sig": false,
"md5_digest": "f1e565a530fb748b0a36400914204c47",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 49132,
"upload_time": "2023-09-01T14:47:24",
"upload_time_iso_8601": "2023-09-01T14:47:24.219675Z",
"url": "https://files.pythonhosted.org/packages/a1/55/881259e00d62ac11713567ada5152b0ad3a744e9f12d5d1f115d40bf17d9/pyasn-1.6.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-09-01 14:47:24",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hadiasghari",
"github_project": "pyasn",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"lcname": "pyasn"
}