| 1 |
.. -*- mode: rst-mode -*- |
| 2 |
.. |
| 3 |
.. Version number is filled in automatically. |
| 4 |
.. |version| replace:: 0.38.1 |
| 5 |
|
| 6 |
=============================================== |
| 7 |
PySubnetTree - A Python Module for CIDR Lookups |
| 8 |
=============================================== |
| 9 |
|
| 10 |
.. rst-class:: opening |
| 11 |
|
| 12 |
The PySubnetTree package provides a Python data structure |
| 13 |
``SubnetTree`` which maps subnets given in `CIDR |
| 14 |
<http://tools.ietf.org/html/rfc4632>`_ notation (incl. |
| 15 |
corresponding IPv6 versions) to Python objects. Lookups are |
| 16 |
performed by longest-prefix matching. |
| 17 |
|
| 18 |
PySubnetTree should generally work on Unix-like platforms such as Linux, |
| 19 |
macOS, and FreeBSD, but does not support Windows. |
| 20 |
|
| 21 |
Download |
| 22 |
-------- |
| 23 |
|
| 24 |
You can find the latest PySubnetTree release for download at |
| 25 |
https://www.zeek.org/download. |
| 26 |
|
| 27 |
PySubnetTree's git repository is located at |
| 28 |
https://github.com/zeek/pysubnettree |
| 29 |
|
| 30 |
This document describes PySubnetTree |version|. See the ``CHANGES`` |
| 31 |
file for version history. |
| 32 |
|
| 33 |
|
| 34 |
Example |
| 35 |
------- |
| 36 |
|
| 37 |
A simple example which associates CIDR prefixes with strings:: |
| 38 |
|
| 39 |
>>> import SubnetTree |
| 40 |
>>> t = SubnetTree.SubnetTree() |
| 41 |
>>> t["10.1.0.0/16"] = "Network 1" |
| 42 |
>>> t["10.1.42.0/24"] = "Network 1, Subnet 42" |
| 43 |
>>> print("10.1.42.1" in t) |
| 44 |
True |
| 45 |
>>> print(t["10.1.42.1"]) |
| 46 |
Network 1, Subnet 42 |
| 47 |
>>> print(t["10.1.43.1"]) |
| 48 |
Network 1 |
| 49 |
>>> print("10.20.1.1" in t) |
| 50 |
False |
| 51 |
>>> t.search_all("10.1.42.1") |
| 52 |
['Network 1, Subnet 42', 'Network 1'] |
| 53 |
>>> try: |
| 54 |
... print(t["10.20.1.1"]) |
| 55 |
... except KeyError as err: |
| 56 |
... print("Error: %s not found" % err) |
| 57 |
Error: '10.20.1.1' not found |
| 58 |
|
| 59 |
|
| 60 |
PySubnetTree also supports IPv6 addresses and prefixes:: |
| 61 |
|
| 62 |
>>> import SubnetTree |
| 63 |
>>> t = SubnetTree.SubnetTree() |
| 64 |
>>> t["2001:db8::/32"] = "Company 1" |
| 65 |
>>> t["2001:db8:4000::/48"] = "Company 1, Site 1" |
| 66 |
>>> t["2001:db8:4000:abcd::"] |
| 67 |
Company 1, Site 1 |
| 68 |
>>> t["2001:db8:fe:1234::"] |
| 69 |
Company 1 |
| 70 |
>>> t.search_all("2001:db8:4000:abcd::1") |
| 71 |
['Company 1, Site 1', 'Company 1'] |
| 72 |
|
| 73 |
|
| 74 |
By default, CIDR prefixes and IP addresses are given as strings. |
| 75 |
Alternatively, a ``SubnetTree`` object can be switched into *binary |
| 76 |
mode*, in which single addresses are passed in the form of packed |
| 77 |
binary strings as, e.g., returned by `socket.inet_aton |
| 78 |
<http://docs.python.org/3/library/socket.html#socket.inet_aton>`_:: |
| 79 |
|
| 80 |
|
| 81 |
>>> t.get_binary_lookup_mode() |
| 82 |
False |
| 83 |
>>> t.set_binary_lookup_mode(True) |
| 84 |
>>> t.get_binary_lookup_mode() |
| 85 |
True |
| 86 |
>>> import socket |
| 87 |
>>> print(t[socket.inet_aton("10.1.42.1")]) |
| 88 |
Network 1, Subnet 42 |
| 89 |
|
| 90 |
A SubnetTree also provides methods ``insert(prefix,object=None)`` for insertion |
| 91 |
of prefixes (``object`` can be skipped to use the tree like a set), and |
| 92 |
``remove(prefix)`` for removing entries (``remove`` performs an _exact_ match |
| 93 |
rather than longest-prefix). |
| 94 |
|
| 95 |
Internally, the CIDR prefixes of a ``SubnetTree`` are managed by a |
| 96 |
Patricia tree data structure and lookups are therefore efficient |
| 97 |
even with a large number of prefixes. |
| 98 |
|
| 99 |
PySubnetTree comes with a BSD license. |
| 100 |
|
| 101 |
|
| 102 |
Prerequisites |
| 103 |
------------- |
| 104 |
|
| 105 |
This package requires Python 3.9 or newer. |
| 106 |
|
| 107 |
Installation |
| 108 |
------------ |
| 109 |
|
| 110 |
Installation is pretty simple:: |
| 111 |
|
| 112 |
> python3 setup.py install |