Post

Python - library - pyasn

PyASN

How to setup pyasn for offline usage.

Install the module and build the local cache.

1
2
3
4
5
pip3 install pyasn
pyasn_util_download.py --latestv46
# There will now be a rib.<date>.bz2 in the current folder
pyasn_util_convert.py --single rib.<date>.bz2 ip2asn.dat
pyasn_util_asnames.py --output=asn-name.json

Now to use it:

1
2
import pyasn
asndb = pyasn.pyasn('ip2asn.dat', 'asn-name.json')

Useful functions:

convert_32bit_to_asdot_asn_format

1
2
>>> asndb.convert_32bit_to_asdot_asn_format(264811)
'AS4.2667'

convert_asdot_to_32bit_asn

1
2
>>> asndb.convert_asdot_to_32bit_asn('AS4.2667')
264811

get_as_name

1
2
>>> asndb.get_as_name(15169)
'GOOGLE, US'

get_as_prefixes

TODO: Add description for this vs effective version

1
2
asndb.get_as_prefixes(3)
{'18.7.13.0/24', '18.7.84.0/24', '18.3.107.0/24', '18.7.83.0/24', '18.7.130.0/24', '18.3.21.0/24', '18.7.68.0/24', '18.21.0.0/16', '18.3.106.0/24', '18.7.132.0/24', '18.9.22.0/24', '18.3.152.0/24', '192.52.62.0/24', '18.7.18.0/24', '18.7.104.0/24', '18.3.5.0/24', '18.7.88.0/24', '18.3.69.0/24', '18.9.62.0/24', '18.0.0.0/16', '18.3.15.0/24', '18.7.103.0/24', '103.41.144.0/22', '2603:4007::/32', '128.30.0.0/15', '18.3.9.0/24', '18.28.0.0/16', '18.29.0.0/16', '18.3.14.0/24', '18.3.109.0/24', '18.7.45.0/24', '18.25.0.0/16', '18.3.22.0/24', '18.7.82.0/24', '18.3.6.0/24', '2603:400a::/32', '18.6.0.0/16', '18.9.47.0/24', '103.50.168.0/24', '18.7.32.0/24', '18.8.0.0/16', '18.3.102.0/24', '18.3.104.0/24', '18.26.0.0/16', '18.3.1.0/24', '18.7.101.0/24', '18.10.0.0/16', '18.4.0.0/16', '128.52.0.0/16', '45.177.7.0/24', '18.3.111.0/24', '18.7.39.0/24', '18.22.55.0/24', '18.9.90.0/24', '18.3.11.0/24', '18.27.0.0/16', '18.12.0.0/16', '18.1.7.0/24', '192.52.64.0/24', '18.3.103.0/24', '18.4.38.0/24', '18.22.0.0/16', '18.3.105.0/24', '18.14.0.0/16', '18.9.21.0/24', '18.7.21.0/24', '18.3.154.0/24', '18.3.24.0/24', '18.7.11.0/24', '18.7.40.0/24', '18.16.0.0/16', '18.9.37.0/24', '38.126.196.0/24', '18.3.4.0/24', '18.3.8.0/24', '18.3.18.0/24', '18.7.80.0/24', '18.31.0.0/16', '2603:4001::/32', '2603:4008::/32', '18.7.71.0/24', '18.4.93.0/24', '18.4.70.0/24', '18.30.0.0/16', '18.7.57.0/24', '18.7.34.0/24', '18.4.81.0/24', '18.7.134.0/24', '18.7.131.0/24', '18.3.151.0/24', '18.17.0.0/16', '18.3.13.0/24', '18.24.0.0/16', '18.20.0.0/16', '18.11.0.0/16', '2603:4009::/32', '18.9.25.0/24', '18.9.0.0/24', '18.5.0.0/16', '18.7.0.0/16', '18.13.0.0/16', '18.3.7.0/24', '18.3.19.0/24', '18.7.133.0/24', '18.3.155.0/24', '18.3.3.0/24', '18.18.0.0/16', '2603:4010::/32', '18.3.12.0/24', '192.52.63.0/24', '18.7.102.0/24', '18.7.10.0/24', '18.7.99.0/24', '18.9.46.0/24', '18.3.90.0/24', '18.3.20.0/24', '18.3.23.0/24', '128.30.52.0/24', '18.3.17.0/24', '18.9.1.0/24', '18.3.2.0/24', '45.177.6.0/24', '18.23.0.0/16', '18.7.135.0/24', '192.52.65.0/24', '2603:4000::/32', '18.3.153.0/24', '79.137.136.0/24', '18.3.16.0/24', '18.9.60.0/24', '43.240.67.0/24', '128.30.32.0/24', '18.3.10.0/24', '2603:4011::/32', '18.7.23.0/24', '18.15.0.0/16', '18.9.49.0/24', '18.7.72.0/24', '18.7.6.0/24', '18.1.0.0/16', '18.3.108.0/24', '18.25.170.0/24', '18.3.97.0/24', '46.34.32.0/20', '18.9.0.0/16', '192.52.61.0/24', '18.3.0.0/16', '18.7.94.0/24'}

get_as_prefixes_effective

1
['18.0.0.0/15', '18.3.0.0/16', '18.4.0.0/14', '18.8.0.0/13', '18.16.0.0/15', '18.18.0.0/16', '18.20.0.0/14', '18.24.0.0/13', '38.126.196.0/24', '43.240.67.0/24', '45.177.6.0/23', '46.34.32.0/20', '79.137.136.0/24', '103.41.144.0/22', '103.50.168.0/24', '128.30.0.0/15', '128.52.0.0/16', '192.52.61.0/24', '192.52.62.0/23', '192.52.64.0/23', '2603:4000::/31', '2603:4007::/32', '2603:4008::/31', '2603:400a::/32', '2603:4010::/31']

get_as_size

TODO: Describe what the AS size means.

1
2
asndb.get_as_size(3)
2170632

lookup

1
2
asndb.lookup('8.8.8.8')
(15169, '8.8.8.0/24')

As a side note the radix datastructure is available at asndb.radix

This post is licensed under CC BY 4.0 by the author.