diff options
Diffstat (limited to 'tutorials/2.x')
-rw-r--r-- | tutorials/2.x/eui/tutorial.txt | 191 | ||||
-rw-r--r-- | tutorials/2.x/ip/sets.txt | 526 | ||||
-rw-r--r-- | tutorials/2.x/ip/tutorial.txt | 752 |
3 files changed, 1469 insertions, 0 deletions
diff --git a/tutorials/2.x/eui/tutorial.txt b/tutorials/2.x/eui/tutorial.txt new file mode 100644 index 0000000..6a7831b --- /dev/null +++ b/tutorials/2.x/eui/tutorial.txt @@ -0,0 +1,191 @@ +First of all you need to pull the various MAC related classes and functions into your namespace. + +.. note:: Do this for the purpose of this tutorial only. In your own code, you should be explicit about the classes, functions and constants you import to avoid name clashes. + +>>> from netaddr import * + +You can reasonably safely import everything from the netaddr namespace as care has been taken to only export the necessary classes, functions and constants. + +Always hand pick your imports if you are unsure about possible name clashes. + +---------------- +Basic operations +---------------- + +Instances of the EUI class are used to represent MAC addresses. + +>>> mac = EUI('00-1B-77-49-54-FD') + +Standard repr() access returns a Python statement that can reconstruct the MAC address object from scratch if executed in the Python interpreter. + +>>> mac +EUI('00-1B-77-49-54-FD') + +Accessing the EUI object in the string context. + +>>> str(mac) +'00-1B-77-49-54-FD' +>>> '%s' % mac +'00-1B-77-49-54-FD' + +Here are a few other common properties. + +>>> str(mac), str(mac.oui), mac.ei, mac.version +('00-1B-77-49-54-FD', '00-1B-77', '49-54-FD', 48) + +------------------------- +Numerical representations +------------------------- + +You can view an individual MAC address in various other formats. + +>>> int(mac) == 117965411581 +True +>>> hex(mac) +'0x1b774954fd' +>>> oct(mac) +'01556722252375' +>>> mac.bits() +'00000000-00011011-01110111-01001001-01010100-11111101' +>>> mac.bin +'0b1101101110111010010010101010011111101' + +---------- +Formatting +---------- + +It is very common to see MAC address in many different formats other than the standard IEEE EUI-48. + +The EUI class constructor handles all these common forms. + +>>> EUI('00-1B-77-49-54-FD') +EUI('00-1B-77-49-54-FD') + +IEEE EUI-48 lowercase format + +>>> EUI('00-1b-77-49-54-fd') +EUI('00-1B-77-49-54-FD') + +Common UNIX format + +>>> EUI('0:1b:77:49:54:fd') +EUI('00-1B-77-49-54-FD') + +Cisco triple hextet format + +>>> EUI('001b:7749:54fd') +EUI('00-1B-77-49-54-FD') +>>> EUI('1b:7749:54fd') +EUI('00-1B-77-49-54-FD') +>>> EUI('1B:7749:54FD') +EUI('00-1B-77-49-54-FD') + +Bare MAC addresses (no delimiters) + +>>> EUI('001b774954fd') +EUI('00-1B-77-49-54-FD') +>>> EUI('01B774954FD') +EUI('00-1B-77-49-54-FD') + +PostreSQL format (found in documentation) + +>>> EUI('001B77:4954FD') +EUI('00-1B-77-49-54-FD') + +It is equally possible to specify a selected format for your MAC string output in the form of a 'dialect' class. Its use is similar to the dialect class used in the Python standard library csv module. + +>>> mac = EUI('00-1B-77-49-54-FD') +>>> mac +EUI('00-1B-77-49-54-FD') +>>> mac.dialect = mac_unix +>>> mac +EUI('0:1b:77:49:54:fd') +>>> mac.dialect = mac_unix_expanded +>>> mac +EUI('00:1b:77:49:54:fd') +>>> mac.dialect = mac_cisco +>>> mac +EUI('001b.7749.54fd') +>>> mac.dialect = mac_bare +>>> mac +EUI('001B774954FD') +>>> mac.dialect = mac_pgsql +>>> mac +EUI('001b77:4954fd') + +You can, of course, create your own dialect classes to customise the MAC formatting if the standard ones do not suit your needs. + +Here's a tweaked UNIX MAC dialect that generates uppercase, zero-filled octets. + +>>> class mac_custom(mac_unix): pass +>>> mac_custom.word_fmt = '%.2X' +>>> mac = EUI('00-1B-77-49-54-FD', dialect=mac_custom) +>>> mac +EUI('00:1B:77:49:54:FD') + +----------------------------------- +Querying organisational information +----------------------------------- + +EUI objects provide an interface to the OUI (Organisationally Unique Identifier) and IAB (Individual Address Block) registration databases available from the IEEE. + +Here is how you query an OUI with the EUI interface. + +>>> mac = EUI('00-1B-77-49-54-FD') +>>> oui = mac.oui +>>> oui +OUI('00-1B-77') +>>> oui.registration().address +[u'Lot 8, Jalan Hi-Tech 2/3', u'Kulim Kedah 09000', u'MY'] +>>> oui.registration().org +u'Intel Corporate' + +You can also use OUI objects directly without going through the EUI interface. + +A few OUI records have multiple registrations against them. I'm not sure if this is recording historical information or just a quirk of the IEEE registration process. + +This example shows you how you access them individually by specifying an index number. + +>>> oui = OUI(524336) # OUI constructor accepts integer values, too. +>>> oui +OUI('08-00-30') +>>> oui.registration(0).address +[u'2380 N. ROSE AVENUE', u'OXNARD CA 93010', u'US'] +>>> oui.registration(0).org +u'NETWORK RESEARCH CORPORATION' +>>> oui.registration(0).oui +'08-00-30' +>>> oui.registration(1).address +[u'GPO BOX 2476V', u'MELBOURNE VIC 3001', u'AU'] +>>> oui.registration(1).org +u'ROYAL MELBOURNE INST OF TECH' +>>> oui.registration(1).oui +'08-00-30' +>>> oui.registration(2).address +[u'CH-1211 GENEVE 23', u'SUISSE/SWITZ', u'CH'] +>>> oui.registration(2).org +u'CERN' +>>> oui.registration(2).oui +'08-00-30' +>>> for i in range(oui.reg_count): +... str(oui), oui.registration(i).org +... +('08-00-30', u'NETWORK RESEARCH CORPORATION') +('08-00-30', u'ROYAL MELBOURNE INST OF TECH') +('08-00-30', u'CERN') + +Here is how you query an IAB with the EUI interface. + +>>> mac = EUI('00-50-C2-00-0F-01') +>>> mac.is_iab() +True +>>> iab = mac.iab +>>> iab +IAB('00-50-C2-00-00-00') +>>> iab.registration() +{'address': [u'1241 Superieor Ave E', u'Cleveland OH 44114', u'US'], + 'iab': '00-50-C2-00-00-00', + 'idx': 84680704, + ... + 'org': u'T.L.S. Corp.', + 'size': 537} diff --git a/tutorials/2.x/ip/sets.txt b/tutorials/2.x/ip/sets.txt new file mode 100644 index 0000000..d65bb68 --- /dev/null +++ b/tutorials/2.x/ip/sets.txt @@ -0,0 +1,526 @@ +First of all you need to pull the various netaddr classes and functions into your namespace. + +.. note:: Do this for the purpose of this tutorial only. In your own code, you should be explicit about the classes, functions and constants you import to avoid name clashes. + +>>> from netaddr import * + +---------------- +Creating IP sets +---------------- + +Here how to create IP sets. + +An empty set. + +>>> IPSet() +IPSet([]) +>>> IPSet([]) +IPSet([]) +>>> len(IPSet([])) +0 + +You can specify either IP addresses and networks as strings. Alternatively, you +can use IPAddress, IPNetwork, IPRange or other IPSet objects. + +>>> IPSet(['192.0.2.0']) +IPSet(['192.0.2.0/32']) +>>> IPSet([IPAddress('192.0.2.0')]) +IPSet(['192.0.2.0/32']) +>>> IPSet([IPNetwork('192.0.2.0')]) +IPSet(['192.0.2.0/32']) +>>> IPSet(IPNetwork('1234::/32')) +IPSet(['1234::/32']) +>>> IPSet([IPNetwork('192.0.2.0/24')]) +IPSet(['192.0.2.0/24']) +>>> IPSet(IPSet(['192.0.2.0/32'])) +IPSet(['192.0.2.0/32']) +>>> IPSet(IPRange("10.0.0.0", "10.0.1.31")) +IPSet(['10.0.0.0/24', '10.0.1.0/27']) +>>> IPSet(IPRange('0.0.0.0', '255.255.255.255')) +IPSet(['0.0.0.0/0']) + +You can iterate over all the IP addresses that are members of the IP set. + +>>> for ip in IPSet(['192.0.2.0/28', '::192.0.2.0/124']): +... print(ip) +192.0.2.0 +192.0.2.1 +192.0.2.2 +192.0.2.3 +192.0.2.4 +192.0.2.5 +192.0.2.6 +192.0.2.7 +192.0.2.8 +192.0.2.9 +192.0.2.10 +192.0.2.11 +192.0.2.12 +192.0.2.13 +192.0.2.14 +192.0.2.15 +::192.0.2.0 +::192.0.2.1 +::192.0.2.2 +::192.0.2.3 +::192.0.2.4 +::192.0.2.5 +::192.0.2.6 +::192.0.2.7 +::192.0.2.8 +::192.0.2.9 +::192.0.2.10 +::192.0.2.11 +::192.0.2.12 +::192.0.2.13 +::192.0.2.14 +::192.0.2.15 + +-------------------------------- +Adding and removing set elements +-------------------------------- + +>>> s1 = IPSet() +>>> s1.add('192.0.2.0') +>>> s1 +IPSet(['192.0.2.0/32']) +>>> s1.remove('192.0.2.0') +>>> s1 +IPSet([]) +>>> s1.add(IPRange("10.0.0.0", "10.0.0.255")) +>>> s1 +IPSet(['10.0.0.0/24']) +>>> s1.remove(IPRange("10.0.0.128", "10.10.10.10")) +>>> s1 +IPSet(['10.0.0.0/25']) + +-------------- +Set membership +-------------- + +Here is a simple arbitrary IP address range. + +>>> iprange = IPRange('192.0.1.255', '192.0.2.16') + +We can see the CIDR networks that can existing with this defined range. + +>>> iprange.cidrs() +[IPNetwork('192.0.1.255/32'), IPNetwork('192.0.2.0/28'), IPNetwork('192.0.2.16/32')] + +Here's an IP set. + +>>> ipset = IPSet(['192.0.2.0/28']) + +Now, let's iterate over the IP addresses in the arbitrary IP address range and see if they are found within the IP set. + +>>> for ip in iprange: +... print(ip, ip in ipset) +192.0.1.255 False +192.0.2.0 True +192.0.2.1 True +192.0.2.2 True +192.0.2.3 True +192.0.2.4 True +192.0.2.5 True +192.0.2.6 True +192.0.2.7 True +192.0.2.8 True +192.0.2.9 True +192.0.2.10 True +192.0.2.11 True +192.0.2.12 True +192.0.2.13 True +192.0.2.14 True +192.0.2.15 True +192.0.2.16 False + +More exotic IPSets + +>>> bigone = IPSet(['0.0.0.0/0']) +>>> IPAddress("10.0.0.1") in bigone +True +>>> IPAddress("0.0.0.0") in bigone +True +>>> IPAddress("255.255.255") in bigone +True +>>> IPNetwork("10.0.0.0/24") in bigone +True +>>> IPAddress("::1") in bigone +False + +>>> smallone = IPSet(["10.0.0.42/32"]) +>>> IPAddress("10.0.0.42") in smallone +True +>>> IPAddress("10.0.0.41") in smallone +False +>>> IPAddress("10.0.0.43") in smallone +False +>>> IPNetwork("10.0.0.42/32") in smallone +True +>>> IPNetwork("10.0.0.42/31") in smallone +False + +------------------------------------- +Unions, intersections and differences +------------------------------------- + +Here are some examples of union operations performed on `IPSet` objects. + +>>> IPSet(['192.0.2.0']) +IPSet(['192.0.2.0/32']) + +>>> IPSet(['192.0.2.0']) | IPSet(['192.0.2.1']) +IPSet(['192.0.2.0/31']) + +>>> IPSet(['192.0.2.0']) | IPSet(['192.0.2.1']) | IPSet(['192.0.2.3']) +IPSet(['192.0.2.0/31', '192.0.2.3/32']) + +>>> IPSet(['192.0.2.0']) | IPSet(['192.0.2.1']) | IPSet(['192.0.2.3/30']) +IPSet(['192.0.2.0/30']) + +>>> IPSet(['192.0.2.0']) | IPSet(['192.0.2.1']) | IPSet(['192.0.2.3/31']) +IPSet(['192.0.2.0/30']) + +>>> IPSet(['192.0.2.0/24']) | IPSet(['192.0.3.0/24']) | IPSet(['192.0.4.0/24']) +IPSet(['192.0.2.0/23', '192.0.4.0/24']) + +Here is an example of the union, intersection and symmetric difference operations all in play at the same time. + +>>> adj_cidrs = list(IPNetwork('192.0.2.0/24').subnet(28)) +>>> even_cidrs = adj_cidrs[::2] +>>> evens = IPSet(even_cidrs) +>>> evens +IPSet(['192.0.2.0/28', '192.0.2.32/28', '192.0.2.64/28', '192.0.2.96/28', '192.0.2.128/28', '192.0.2.160/28', '192.0.2.192/28', '192.0.2.224/28']) +>>> IPSet(['192.0.2.0/24']) & evens +IPSet(['192.0.2.0/28', '192.0.2.32/28', '192.0.2.64/28', '192.0.2.96/28', '192.0.2.128/28', '192.0.2.160/28', '192.0.2.192/28', '192.0.2.224/28']) +>>> odds = IPSet(['192.0.2.0/24']) ^ evens +>>> odds +IPSet(['192.0.2.16/28', '192.0.2.48/28', '192.0.2.80/28', '192.0.2.112/28', '192.0.2.144/28', '192.0.2.176/28', '192.0.2.208/28', '192.0.2.240/28']) +>>> evens | odds +IPSet(['192.0.2.0/24']) +>>> evens & odds +IPSet([]) +>>> evens ^ odds +IPSet(['192.0.2.0/24']) + +--------------------- +Supersets and subsets +--------------------- + +IP sets provide the ability to test whether a group of addresses ranges fit within the set of another group of address ranges. + +>>> s1 = IPSet(['192.0.2.0/24', '192.0.4.0/24']) +>>> s2 = IPSet(['192.0.2.0', '192.0.4.0']) +>>> s1 +IPSet(['192.0.2.0/24', '192.0.4.0/24']) +>>> s2 +IPSet(['192.0.2.0/32', '192.0.4.0/32']) +>>> s1.issuperset(s2) +True +>>> s2.issubset(s1) +True +>>> s2.issuperset(s1) +False +>>> s1.issubset(s2) +False + +Here's a more complete example using various well known IPv4 address ranges. + +>>> ipv4_addr_space = IPSet(['0.0.0.0/0']) +>>> private = IPSet(['10.0.0.0/8', '172.16.0.0/12', '192.0.2.0/24', '192.168.0.0/16', '239.192.0.0/14']) +>>> reserved = IPSet(['225.0.0.0/8', '226.0.0.0/7', '228.0.0.0/6', '234.0.0.0/7', '236.0.0.0/7', '238.0.0.0/8', '240.0.0.0/4']) +>>> unavailable = reserved | private +>>> available = ipv4_addr_space ^ unavailable + +Let's see what we've got: + +>>> for cidr in available.iter_cidrs(): +... print(cidr, cidr[0], cidr[-1]) +0.0.0.0/5 0.0.0.0 7.255.255.255 +8.0.0.0/7 8.0.0.0 9.255.255.255 +11.0.0.0/8 11.0.0.0 11.255.255.255 +12.0.0.0/6 12.0.0.0 15.255.255.255 +16.0.0.0/4 16.0.0.0 31.255.255.255 +32.0.0.0/3 32.0.0.0 63.255.255.255 +64.0.0.0/2 64.0.0.0 127.255.255.255 +128.0.0.0/3 128.0.0.0 159.255.255.255 +160.0.0.0/5 160.0.0.0 167.255.255.255 +168.0.0.0/6 168.0.0.0 171.255.255.255 +172.0.0.0/12 172.0.0.0 172.15.255.255 +172.32.0.0/11 172.32.0.0 172.63.255.255 +172.64.0.0/10 172.64.0.0 172.127.255.255 +172.128.0.0/9 172.128.0.0 172.255.255.255 +173.0.0.0/8 173.0.0.0 173.255.255.255 +174.0.0.0/7 174.0.0.0 175.255.255.255 +176.0.0.0/4 176.0.0.0 191.255.255.255 +192.0.0.0/23 192.0.0.0 192.0.1.255 +192.0.3.0/24 192.0.3.0 192.0.3.255 +192.0.4.0/22 192.0.4.0 192.0.7.255 +192.0.8.0/21 192.0.8.0 192.0.15.255 +192.0.16.0/20 192.0.16.0 192.0.31.255 +192.0.32.0/19 192.0.32.0 192.0.63.255 +192.0.64.0/18 192.0.64.0 192.0.127.255 +192.0.128.0/17 192.0.128.0 192.0.255.255 +192.1.0.0/16 192.1.0.0 192.1.255.255 +192.2.0.0/15 192.2.0.0 192.3.255.255 +192.4.0.0/14 192.4.0.0 192.7.255.255 +192.8.0.0/13 192.8.0.0 192.15.255.255 +192.16.0.0/12 192.16.0.0 192.31.255.255 +192.32.0.0/11 192.32.0.0 192.63.255.255 +192.64.0.0/10 192.64.0.0 192.127.255.255 +192.128.0.0/11 192.128.0.0 192.159.255.255 +192.160.0.0/13 192.160.0.0 192.167.255.255 +192.169.0.0/16 192.169.0.0 192.169.255.255 +192.170.0.0/15 192.170.0.0 192.171.255.255 +192.172.0.0/14 192.172.0.0 192.175.255.255 +192.176.0.0/12 192.176.0.0 192.191.255.255 +192.192.0.0/10 192.192.0.0 192.255.255.255 +193.0.0.0/8 193.0.0.0 193.255.255.255 +194.0.0.0/7 194.0.0.0 195.255.255.255 +196.0.0.0/6 196.0.0.0 199.255.255.255 +200.0.0.0/5 200.0.0.0 207.255.255.255 +208.0.0.0/4 208.0.0.0 223.255.255.255 +224.0.0.0/8 224.0.0.0 224.255.255.255 +232.0.0.0/7 232.0.0.0 233.255.255.255 +239.0.0.0/9 239.0.0.0 239.127.255.255 +239.128.0.0/10 239.128.0.0 239.191.255.255 +239.196.0.0/14 239.196.0.0 239.199.255.255 +239.200.0.0/13 239.200.0.0 239.207.255.255 +239.208.0.0/12 239.208.0.0 239.223.255.255 +239.224.0.0/11 239.224.0.0 239.255.255.255 + +>>> ipv4_addr_space ^ available +IPSet(['10.0.0.0/8', '172.16.0.0/12', '192.0.2.0/24', '192.168.0.0/16', '225.0.0.0/8', '226.0.0.0/7', '228.0.0.0/6', '234.0.0.0/7', '236.0.0.0/7', '238.0.0.0/8', '239.192.0.0/14', '240.0.0.0/4']) + + +------------------------------ +Combined IPv4 and IPv6 support +------------------------------ + +In keeping with netaddr's pragmatic approach, you are free to mix and match IPv4 and IPv6 within the same data structure. + +>>> s1 = IPSet(['192.0.2.0', '::192.0.2.0', '192.0.2.2', '::192.0.2.2']) +>>> s2 = IPSet(['192.0.2.2', '::192.0.2.2', '192.0.2.4', '::192.0.2.4']) + +>>> s1 +IPSet(['192.0.2.0/32', '192.0.2.2/32', '::192.0.2.0/128', '::192.0.2.2/128']) +>>> s2 +IPSet(['192.0.2.2/32', '192.0.2.4/32', '::192.0.2.2/128', '::192.0.2.4/128']) + +^^^^^^^^^^^^^^^^^^^^^^^ +IPv4 and IPv6 set union +^^^^^^^^^^^^^^^^^^^^^^^ + +>>> s1 | s2 +IPSet(['192.0.2.0/32', '192.0.2.2/32', '192.0.2.4/32', '::192.0.2.0/128', '::192.0.2.2/128', '::192.0.2.4/128']) +>>> s2 | s1 +IPSet(['192.0.2.0/32', '192.0.2.2/32', '192.0.2.4/32', '::192.0.2.0/128', '::192.0.2.2/128', '::192.0.2.4/128']) + +^^^^^^^^^^^^^^^^ +set intersection +^^^^^^^^^^^^^^^^ + +>>> s1 & s2 +IPSet(['192.0.2.2/32', '::192.0.2.2/128']) + +^^^^^^^^^^^^^^ +set difference +^^^^^^^^^^^^^^ + +>>> s1 - s2 +IPSet(['192.0.2.0/32', '::192.0.2.0/128']) +>>> s2 - s1 +IPSet(['192.0.2.4/32', '::192.0.2.4/128']) + +^^^^^^^^^^^^^^^^^^^^^^^^ +set symmetric difference +^^^^^^^^^^^^^^^^^^^^^^^^ + +>>> s1 ^ s2 +IPSet(['192.0.2.0/32', '192.0.2.4/32', '::192.0.2.0/128', '::192.0.2.4/128']) + +------------------ +Disjointed IP sets +------------------ + +>>> s1 = IPSet(['192.0.2.0', '192.0.2.1', '192.0.2.2']) +>>> s2 = IPSet(['192.0.2.2', '192.0.2.3', '192.0.2.4']) +>>> s1 & s2 +IPSet(['192.0.2.2/32']) +>>> s1.isdisjoint(s2) +False +>>> s1 = IPSet(['192.0.2.0', '192.0.2.1']) +>>> s2 = IPSet(['192.0.2.3', '192.0.2.4']) +>>> s1 & s2 +IPSet([]) +>>> s1.isdisjoint(s2) +True + +------------------ +Updating an IP set +------------------ + +As with a normal Python set you can also update one IP set with the contents of another. + +>>> s1 = IPSet(['192.0.2.0/25']) +>>> s1 +IPSet(['192.0.2.0/25']) +>>> s2 = IPSet(['192.0.2.128/25']) +>>> s2 +IPSet(['192.0.2.128/25']) +>>> s1.update(s2) +>>> s1 +IPSet(['192.0.2.0/24']) +>>> s1.update(['192.0.0.0/24', '192.0.1.0/24', '192.0.3.0/24']) +>>> s1 +IPSet(['192.0.0.0/22']) + +>>> s2 = IPSet(['10.0.0.0/16']) +>>> s2.update(IPRange('10.1.0.0', '10.1.255.255')) +>>> s2 +IPSet(['10.0.0.0/15']) + +>>> s2.clear() +>>> s2 +IPSet([]) + +-------------------------------- +Removing elements from an IP set +-------------------------------- + +Removing an IP address from an IPSet will split the CIDR subnets within it into their constituent parts. + +Here we create a set representing the entire IPv4 address space. + +>>> s1 = IPSet(['0.0.0.0/0']) +>>> s1 +IPSet(['0.0.0.0/0']) + +Then we strip off the last address. + +>>> s1.remove('255.255.255.255') + +Leaving us with: + +>>> s1 +IPSet(['0.0.0.0/1', '128.0.0.0/2', ..., '255.255.255.252/31', '255.255.255.254/32']) +>>> list(s1.iter_cidrs()) +[IPNetwork('0.0.0.0/1'), IPNetwork('128.0.0.0/2'), ..., IPNetwork('255.255.255.252/31'), IPNetwork('255.255.255.254/32')] +>>> len(list(s1.iter_cidrs())) +32 + +Let's check the result using the `cidr_exclude` function. + +>>> list(s1.iter_cidrs()) == cidr_exclude('0.0.0.0/0', '255.255.255.255') +True + +Next, let's remove the first address from the original range. + +>>> s1.remove('0.0.0.0') + +This fractures the CIDR subnets further. + +>>> s1 +IPSet(['0.0.0.1/32', '0.0.0.2/31', ..., '255.255.255.252/31', '255.255.255.254/32']) +>>> len(list(s1.iter_cidrs())) +62 + +You can keep doing this but be aware that large IP sets can take up a lot of memory if they contain many thousands of entries. + +---------------------------- +Adding elements to an IP set +---------------------------- + +Let's fix up the fractured IP set from the previous section by re-adding the IP addresses we removed. + +>>> s1.add('255.255.255.255') +>>> s1 +IPSet(['0.0.0.1/32', '0.0.0.2/31', ..., '64.0.0.0/2', '128.0.0.0/1']) + +Getting better. + +>>> list(s1.iter_cidrs()) +[IPNetwork('0.0.0.1/32'), IPNetwork('0.0.0.2/31'), ..., IPNetwork('64.0.0.0/2'), IPNetwork('128.0.0.0/1')] + +>>> len(list(s1.iter_cidrs())) +32 + +Add back the other IP address. + +>>> s1.add('0.0.0.0') + +And we're back to our original address. + +>>> s1 +IPSet(['0.0.0.0/0']) + +-------------------------------- +Convert an IP set to an IP Range +-------------------------------- +Sometimes you may want to convert an IPSet back to an IPRange. + +>>> s1 = IPSet(['10.0.0.0/25', '10.0.0.128/25']) +>>> s1.iprange() +IPRange('10.0.0.0', '10.0.0.255') + +This only works if the IPSet is contiguous + +>>> s1.iscontiguous() +True +>>> s1.remove('10.0.0.16') +>>> s1 +IPSet(['10.0.0.0/28', '10.0.0.17/32', '10.0.0.18/31', '10.0.0.20/30', '10.0.0.24/29', '10.0.0.32/27', '10.0.0.64/26', '10.0.0.128/25']) +>>> s1.iscontiguous() +False +>>> s1.iprange() +Traceback (most recent call last): + File "<stdin>", line 1, in <module> +ValueError: IPSet is not contiguous + +If it is not contiguous, you can still convert the IPSet, but you will get multiple IPRanges. +>>> list(s1.iter_ipranges()) +[IPRange('10.0.0.0', '10.0.0.15'), IPRange('10.0.0.17', '10.0.0.255')] + +>>> s2 = IPSet(['0.0.0.0/0']) +>>> s2.iscontiguous() +True +>>> s2.iprange() +IPRange('0.0.0.0', '255.255.255.255') + +>>> s3 = IPSet() +>>> s3.iscontiguous() +True +>>> s3.iprange() + +>>> s4 = IPSet(IPRange('10.0.0.0', '10.0.0.8')) +>>> s4.iscontiguous() +True + +---------------------- +Pickling IPSet objects +---------------------- + +As with all other netaddr classes, you can use ``pickle`` to persist IP sets for later use. + +>>> import pickle +>>> ip_data = IPSet(['10.0.0.0/16', 'fe80::/64']) +>>> buf = pickle.dumps(ip_data) +>>> ip_data_unpickled = pickle.loads(buf) +>>> ip_data == ip_data_unpickled +True + +---------------------- +Compare IPSet objects +---------------------- + +>>> x = IPSet(['fc00::/2']) +>>> y = IPSet(['fc00::/3']) + +>>> x > y +True + +>>> x < y +False + +>>> x != y +True diff --git a/tutorials/2.x/ip/tutorial.txt b/tutorials/2.x/ip/tutorial.txt new file mode 100644 index 0000000..fa8d880 --- /dev/null +++ b/tutorials/2.x/ip/tutorial.txt @@ -0,0 +1,752 @@ +First of all you need to pull the various netaddr classes and functions into your namespace. + +.. note:: Do this for the purpose of this tutorial only. In your own code, you should be explicit about the classes, functions and constants you import to avoid name clashes. + +>>> from netaddr import * + +We also import the standard library module `pprint` to help format our output. + +>>> import pprint + +---------------- +Basic operations +---------------- + +The following `IPAddress` object represents a single IP address. + +>>> ip = IPAddress('192.0.2.1') +>>> ip.version +4 + +The `repr()` call returns a Python statement that can be used to reconstruct an equivalent IP address object state from scratch when run in the Python interpreter. + +>>> repr(ip) +"IPAddress('192.0.2.1')" +>>> ip +IPAddress('192.0.2.1') + +Access in the string context returns the IP object as a string value. + +>>> str(ip) +'192.0.2.1' +>>> '%s' % ip +'192.0.2.1' +>>> ip.format() # only really useful for IPv6 addresses. +'192.0.2.1' + +------------------------ +Numerical representation +------------------------ + +You can view an IP address in various other formats. + +>>> int(ip) == 3221225985 +True +>>> hex(ip) +'0xc0000201' +>>> ip.bin +'0b11000000000000000000001000000001' +>>> ip.bits() +'11000000.00000000.00000010.00000001' +>>> ip.words == (192, 0, 2, 1) +True + +--------------------------------- +Representing networks and subnets +--------------------------------- + +`IPNetwork` objects are used to represent subnets, networks or VLANs that accept CIDR prefixes and netmasks. + +>>> ip = IPNetwork('192.0.2.1') +>>> ip.ip +IPAddress('192.0.2.1') +>>> ip.network, ip.broadcast +(IPAddress('192.0.2.1'), None) +>>> ip.netmask, ip.hostmask +(IPAddress('255.255.255.255'), IPAddress('0.0.0.0')) +>>> ip.size +1 + +In this case, the network and broadcast address are the same, akin to a host route. + +>>> ip = IPNetwork('192.0.2.0/24') +>>> ip.ip +IPAddress('192.0.2.0') +>>> ip.network, ip.broadcast +(IPAddress('192.0.2.0'), IPAddress('192.0.2.255')) +>>> ip.netmask, ip.hostmask +(IPAddress('255.255.255.0'), IPAddress('0.0.0.255')) +>>> ip.size +256 + +And finally, this IPNetwork object represents an IP address that belongs to a given IP subnet. + +>>> ip = IPNetwork('192.0.3.112/22') +>>> ip.ip +IPAddress('192.0.3.112') +>>> ip.network, ip.broadcast +(IPAddress('192.0.0.0'), IPAddress('192.0.3.255')) +>>> ip.netmask, ip.hostmask +(IPAddress('255.255.252.0'), IPAddress('0.0.3.255')) +>>> ip.size +1024 + +Internally, each IPNetwork object only stores 3 values : + + * the IP address value as an unsigned integer + * a reference to the IP protocol module for the IP version being represented + * the CIDR prefix bitmask + +All the other values are calculated on-the-fly on access. + +It is possible to adjust the IP address value and the CIDR prefix after object instantiation. + +>>> ip = IPNetwork('0.0.0.0/0') +>>> ip +IPNetwork('0.0.0.0/0') +>>> ip.value = 3221225985 +>>> ip +IPNetwork('192.0.2.1/0') +>>> ip.prefixlen +0 +>>> ip.prefixlen = 23 +>>> ip +IPNetwork('192.0.2.1/23') + +The prefix length can also be changed by specifying a subnet mask: + +>>> ip = IPNetwork('192.168.1.0/24') +>>> ip.netmask = '255.255.0.0' +>>> ip +IPNetwork('192.168.1.0/16') +>>> ip = IPNetwork('fe80::dead:beef/64') +>>> ip.netmask = 'ffff:ffff::' +>>> ip +IPNetwork('fe80::dead:beef/32') + +There is also a property that lets you access the *true* CIDR address which removes all host bits from the network address based on the CIDR subnet prefix. + +>>> ip.cidr +IPNetwork('192.0.2.0/23') + +This is handy for specifying some networking configurations correctly. + +If you want to access information about each of the various IP addresses that form the IP subnet, this is available by performing pass through calls to sub methods of each `IPAddress` object. + +For example if you want to see a binary digit representation of each address you can do the following. + +>>> ip.ip.bits() +'11000000.00000000.00000010.00000001' +>>> ip.network.bits() +'11000000.00000000.00000010.00000000' +>>> ip.netmask.bits() +'11111111.11111111.11111110.00000000' +>>> ip.broadcast.bits() +'11000000.00000000.00000011.11111111' + +------------ +IPv6 support +------------ + +Full support for IPv6 is provided. Let's try a few examples: + +>>> ip = IPAddress(0, 6) +>>> ip +IPAddress('::') +>>> ip = IPNetwork('fe80::dead:beef/64') +>>> str(ip), ip.prefixlen, ip.version +('fe80::dead:beef/64', 64, 6) +>>> int(ip.ip) == 338288524927261089654018896845083623151 +True +>>> hex(ip.ip) +'0xfe8000000000000000000000deadbeef' + +Bit-style output isn't as quite as friendly as hexadecimal for such a long numbers, but here the proof that it works! + +>>> ip.ip.bits() +'1111111010000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:1101111010101101:1011111011101111' + +Here are some networking details for an IPv6 subnet. + +>>> ip.network, ip.broadcast, ip.netmask, ip.hostmask +(IPAddress('fe80::'), IPAddress('fe80::ffff:ffff:ffff:ffff'), IPAddress('ffff:ffff:ffff:ffff::'), IPAddress('::ffff:ffff:ffff:ffff')) + +-------------------------------------- +Interoperability between IPv4 and IPv6 +-------------------------------------- + +It is likely that with IPv6 becoming more prevalent, you'll want to be able to interoperate between IPv4 and IPv6 address seamlessly. + +Here are a couple of methods that help achieve this. + +^^^^^^^^^^^^^^^^^^^^^^^ +IPv4 to IPv6 conversion +^^^^^^^^^^^^^^^^^^^^^^^ + +>>> IPAddress('192.0.2.15').ipv4() +IPAddress('192.0.2.15') +>>> ip = IPAddress('192.0.2.15').ipv6() +>>> ip +IPAddress('::ffff:192.0.2.15') +>>> ip.is_ipv4_mapped() +True +>>> ip.is_ipv4_compat() +False + +>>> IPAddress('192.0.2.15').ipv6(ipv4_compatible=True) +IPAddress('::192.0.2.15') +>>> IPAddress('192.0.2.15').ipv6(ipv4_compatible=True).is_ipv4_compat() +True +>>> IPAddress('192.0.2.15').ipv6(True) +IPAddress('::192.0.2.15') +>>> ip = IPNetwork('192.0.2.1/23') +>>> ip.ipv4() +IPNetwork('192.0.2.1/23') +>>> ip.ipv6() +IPNetwork('::ffff:192.0.2.1/119') +>>> ip.ipv6(ipv4_compatible=True) +IPNetwork('::192.0.2.1/119') + +^^^^^^^^^^^^^^^^^^^^^^^ +IPv6 to IPv4 conversion +^^^^^^^^^^^^^^^^^^^^^^^ + +>>> IPNetwork('::ffff:192.0.2.1/119').ipv6() +IPNetwork('::ffff:192.0.2.1/119') +>>> IPNetwork('::ffff:192.0.2.1/119').ipv6(ipv4_compatible=True) +IPNetwork('::192.0.2.1/119') +>>> IPNetwork('::ffff:192.0.2.1/119').ipv4() +IPNetwork('192.0.2.1/23') +>>> IPNetwork('::192.0.2.1/119').ipv4() +IPNetwork('192.0.2.1/23') + +Note that the IP object returns IPv4 "mapped" addresses by default in preference to IPv4 "compatible" ones. This has been chosen purposefully as the latter form has been deprecated (see RFC 4291 for details). + +--------------- +List operations +--------------- + +If you treat an `IPNetwork` object as if it were a standard Python list object it will give you access to a list of individual IP address objects. This of course is illusory and they are not created until you access them. + +>>> ip = IPNetwork('192.0.2.16/29') + +Accessing an IP object using the `list()` context invokes the default generator which returns a list of all IP objects in the range specified by the IP object's subnet. + +>>> ip_list = list(ip) +>>> len(ip_list) +8 +>>> ip_list +[IPAddress('192.0.2.16'), IPAddress('192.0.2.17'), ..., IPAddress('192.0.2.22'), IPAddress('192.0.2.23')] + +The length of that list is 8 individual IP addresses. + +>>> len(ip) +8 + +^^^^^^^^ +Indexing +^^^^^^^^ + +You can use standard index access to IP addresses in the subnet. + +>>> ip[0] +IPAddress('192.0.2.16') +>>> ip[1] +IPAddress('192.0.2.17') +>>> ip[-1] +IPAddress('192.0.2.23') + +^^^^^^^ +Slicing +^^^^^^^ + +You can also use list slices on IP addresses in the subnet. + +>>> ip[0:4] +<generator object ...> + +The slice is a generator function. This was done to save time and system resources as some slices can end up being very large for certain subnets! + +Here is how you'd access all elements in a slice. + +>>> list(ip[0:4]) +[IPAddress('192.0.2.16'), IPAddress('192.0.2.17'), IPAddress('192.0.2.18'), IPAddress('192.0.2.19')] + +Extended slicing is also supported. + +>>> list(ip[0::2]) +[IPAddress('192.0.2.16'), IPAddress('192.0.2.18'), IPAddress('192.0.2.20'), IPAddress('192.0.2.22')] + +List reversal. + +>>> list(ip[-1::-1]) +[IPAddress('192.0.2.23'), IPAddress('192.0.2.22'), ..., IPAddress('192.0.2.17'), IPAddress('192.0.2.16')] + +Use of generators ensures working with large IP subnets is efficient. + +>>> for ip in IPNetwork('192.0.2.0/23'): +... print('%s' % ip) +... +192.0.2.0 +192.0.2.1 +192.0.2.2 +192.0.2.3 +... +192.0.3.252 +192.0.3.253 +192.0.3.254 +192.0.3.255 + +In IPv4 networks you only usually assign the addresses between the network and broadcast addresses to actual host interfaces on systems. + +Here is the iterator provided for accessing these IP addresses : + +>>> for ip in IPNetwork('192.0.2.0/23').iter_hosts(): +... print('%s' % ip) +... +192.0.2.1 +192.0.2.2 +192.0.2.3 +192.0.2.4 +... +192.0.3.251 +192.0.3.252 +192.0.3.253 +192.0.3.254 + +--------------------------------- +Sorting IP addresses and networks +--------------------------------- + +It is fairly common and useful to be able to sort IP addresses and networks canonically. + +Here is how sorting works with individual addresses. + +>>> import random +>>> ip_list = list(IPNetwork('192.0.2.128/28')) +>>> random.shuffle(ip_list) +>>> sorted(ip_list) +[IPAddress('192.0.2.128'), IPAddress('192.0.2.129'), ..., IPAddress('192.0.2.142'), IPAddress('192.0.2.143')] + +For convenience, you are able to sort IP subnets at the same time as addresses and they can be combinations of IPv4 and IPv6 addresses at the same time as well (IPv4 addresses and network appear before IPv6 ones). + +>>> ip_list = [ +... IPAddress('192.0.2.130'), +... IPAddress('10.0.0.1'), +... IPNetwork('192.0.2.128/28'), +... IPNetwork('192.0.3.0/24'), +... IPNetwork('192.0.2.0/24'), +... IPNetwork('fe80::/64'), +... IPAddress('::'), +... IPNetwork('172.24/12')] +>>> random.shuffle(ip_list) +>>> ip_list.sort() +>>> pprint.pprint(ip_list) +[IPAddress('10.0.0.1'), + IPNetwork('172.24.0.0/12'), + IPNetwork('192.0.2.0/24'), + IPNetwork('192.0.2.128/28'), + IPAddress('192.0.2.130'), + IPNetwork('192.0.3.0/24'), + IPAddress('::'), + IPNetwork('fe80::/64')] + +Notice how overlapping subnets also sort in order from largest to smallest. + +----------------------------------------- +Summarizing list of addresses and subnets +----------------------------------------- + +Another useful operation is the ability to summarize groups of IP subnets and addresses, merging them together where possible to create the smallest possible list of CIDR subnets. + +You do this in netaddr using the `cidr_merge()` function. + +First we create a list of IP objects that contains a good mix of individual addresses and subnets, along with some string based IP address values for good measure. To make things more interesting some IPv6 addresses are thrown in as well. + +>>> ip_list = [ip for ip in IPNetwork('fe80::/120')] +>>> ip_list.append(IPNetwork('192.0.2.0/24')) +>>> ip_list.extend([str(ip) for ip in IPNetwork('192.0.3.0/24')]) +>>> ip_list.append(IPNetwork('192.0.4.0/25')) +>>> ip_list.append(IPNetwork('192.0.4.128/25')) +>>> len(ip_list) +515 +>>> cidr_merge(ip_list) +[IPNetwork('192.0.2.0/23'), IPNetwork('192.0.4.0/24'), IPNetwork('fe80::/120')] + +Useful isn't it? + +--------------------- +Supernets and subnets +--------------------- + +It is quite common to have a large CIDR subnet that you may want to split up into multiple smaller component blocks to better manage your network allocations, firewall rules etcc and netaddr gives you the tools required to do this. + +Here we take a large /16 private class B network block and split it up into a set of smaller 512 sized blocks. + +>>> ip = IPNetwork('172.24.0.0/16') +>>> ip.subnet(23) +<generator object ...> + +Once again, this method produces and iterator because of the possibility for a large number of return values depending on this subnet size specified. + +>>> subnets = list(ip.subnet(23)) +>>> len(subnets) +128 +>>> subnets +[IPNetwork('172.24.0.0/23'), IPNetwork('172.24.2.0/23'), IPNetwork('172.24.4.0/23'), ..., IPNetwork('172.24.250.0/23'), IPNetwork('172.24.252.0/23'), IPNetwork('172.24.254.0/23')] + +It is also possible to retrieve the list of supernets that a given IP address or subnet belongs to. You can also specify an optional limit. + +>>> ip = IPNetwork('192.0.2.114') +>>> supernets = ip.supernet(22) +>>> pprint.pprint(supernets) +[IPNetwork('192.0.0.0/22'), + IPNetwork('192.0.2.0/23'), + IPNetwork('192.0.2.0/24'), + IPNetwork('192.0.2.0/25'), + IPNetwork('192.0.2.64/26'), + IPNetwork('192.0.2.96/27'), + IPNetwork('192.0.2.112/28'), + IPNetwork('192.0.2.112/29'), + IPNetwork('192.0.2.112/30'), + IPNetwork('192.0.2.114/31')] + +Here, we return a list rather than a generator because the potential list of values is of a predictable size (no more than 31 subnets for an IPv4 address and 127 for IPv6). + +--------------------------------------- +Support for non-standard address ranges +--------------------------------------- + +While CIDR is a useful way to describe networks succinctly, it is often necessary (particularly with IPv4 which predates the CIDR specification) to be able to generate lists of IP addresses that have an arbitrary start and end address that do not fall on strict bit mask boundaries. + +The `iter_iprange()` function allow you to do just this. + +>>> ip_list = list(iter_iprange('192.0.2.1', '192.0.2.14')) +>>> len(ip_list) +14 +>>> ip_list +[IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), ..., IPAddress('192.0.2.13'), IPAddress('192.0.2.14')] + +It is equally nice to know what the actual list of CIDR subnets is that would correctly cover this non-aligned range of addresses. + +Here `cidr_merge()` comes to the rescue once more. + +>>> cidr_merge(ip_list) +[IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/30'), IPNetwork('192.0.2.12/31'), IPNetwork('192.0.2.14/32')] + +-------------------------------------------- +Dealing with older IP network specifications +-------------------------------------------- + +Until the advent of the CIDR specification it was common to infer the netmask of an IPv4 address based on its first octet using an set of classful rules (first defined in RFC 791). + +You frequently come across reference to them in various RFCs and they are well supported by a number of software libraries. For completeness, rather than leave out this important (but now somewhat historical) set of rules, they are supported via the cryptically named `cidr_abbrev_to_verbose()` function. + +Here is an example of these rules for the whole of the IPv4 address space. + +>>> cidrs = [cidr_abbrev_to_verbose(octet) for octet in range(0, 256)] +>>> pprint.pprint(cidrs) +['0.0.0.0/8', +... + '127.0.0.0/8', + '128.0.0.0/16', +... + '191.0.0.0/16', + '192.0.0.0/24', +... + '223.0.0.0/24', + '224.0.0.0/4', +... + '239.0.0.0/4', + '240.0.0.0/32', +... + '255.0.0.0/32'] +>>> len(cidrs) +256 + +------------------------- +IP address categorisation +------------------------- + +IP addresses fall into several categories, not all of which are suitable for assignment as host addresses. + +^^^^^^^ +Unicast +^^^^^^^ + +>>> IPAddress('192.0.2.1').is_unicast() +True +>>> IPAddress('fe80::1').is_unicast() +True + +^^^^^^^^^ +Multicast +^^^^^^^^^ + +Used to identify multicast groups (see RFC 2365 and 3171 for more info). + +>>> IPAddress('239.192.0.1').is_multicast() +True +>>> IPAddress('ff00::1').is_multicast() +True + +^^^^^^^ +Private +^^^^^^^ + +Found on intranets and used behind NAT routers. + +>>> IPAddress('172.24.0.1').is_private() +True +>>> IPAddress('10.0.0.1').is_private() +True +>>> IPAddress('192.168.0.1').is_private() +True +>>> IPAddress('fc00::1').is_private() +True + +^^^^^^^^ +Reserved +^^^^^^^^ + +Addresses in reserved ranges are not available for general use. + +>>> IPAddress('253.0.0.1').is_reserved() +True + +^^^^^^ +Public +^^^^^^ + +Addresses accessible via the Internet. + +.. note:: circa the end of 2011 all IPv4 addresses had been allocated to the Regional Internet Registrars. A booming after market in IPv4 addresses has started. There is still plenty of life left in this protocol version yet :) + +>>> ip = IPAddress('62.125.24.5') +>>> ip.is_unicast() and not ip.is_private() +True + +^^^^^^^^ +Netmasks +^^^^^^^^ + +A bitmask used to divide an IP address into its network address and host address. + +>>> IPAddress('255.255.254.0').is_netmask() +True + +^^^^^^^^^ +Hostmasks +^^^^^^^^^ + +Similar to a netmask but with the all the bits flipped the opposite way. + +>>> IPAddress('0.0.1.255').is_hostmask() +True + +^^^^^^^^ +Loopback +^^^^^^^^ + +These addresses are used internally within an IP network stack and packets sent to these addresses are not distributed via a physical network connection. + +>>> IPAddress('127.0.0.1').is_loopback() +True +>>> IPAddress('::1').is_loopback() +True + +---------------------- +Comparing IP addresses +---------------------- + +`IPAddress` objects can be compared with each other. As an `IPAddress` object can represent both an individual IP address and an implicit network, it pays to get both sides of your comparison into the same terms before you compare them to avoid odd results. + +Here are some comparisons of individual IP address to get the ball rolling. + +>>> IPAddress('192.0.2.1') == IPAddress('192.0.2.1') +True +>>> IPAddress('192.0.2.1') < IPAddress('192.0.2.2') +True +>>> IPAddress('192.0.2.2') > IPAddress('192.0.2.1') +True +>>> IPAddress('192.0.2.1') != IPAddress('192.0.2.1') +False +>>> IPAddress('192.0.2.1') >= IPAddress('192.0.2.1') +True +>>> IPAddress('192.0.2.2') >= IPAddress('192.0.2.1') +True +>>> IPAddress('192.0.2.1') <= IPAddress('192.0.2.1') +True +>>> IPAddress('192.0.2.1') <= IPAddress('192.0.2.2') +True + +Now, lets try something a little more interesting. + +>>> IPNetwork('192.0.2.0/24') == IPNetwork('192.0.2.112/24') +True + +Hmmmmmmmm... looks a bit odd doesn't it? That's because by default, IP objects compare their subnets (or lower and upper boundaries) rather than their individual IP address values. + +The solution to this situation is very simple. Knowing this default behaviour, just be explicit about exactly which portion of each IP object you'd like to compare using pass-through properties. + +>>> IPNetwork('192.0.2.0/24').ip == IPNetwork('192.0.2.112/24').ip +False +>>> IPNetwork('192.0.2.0/24').ip < IPNetwork('192.0.2.112/24').ip +True + +That's more like it. You can also be explicit about comparing networks in this way if you so wish (although it is not strictly necessary). + +>>> IPNetwork('192.0.2.0/24').cidr == IPNetwork('192.0.2.112/24').cidr +True + +Armed with this information here are some examples of network comparisons. + +>>> IPNetwork('192.0.2.0/24') == IPNetwork('192.0.3.0/24') +False +>>> IPNetwork('192.0.2.0/24') < IPNetwork('192.0.3.0/24') +True + +This will inevitably raise questions about comparing IPAddress (scalar) objects and IPNetwork (vector) objects with each other (or at least it should). + +Here is how netaddr chooses to address this situation. + +>>> IPAddress('192.0.2.0') == IPNetwork('192.0.2.0/32') +False +>>> IPAddress('192.0.2.0') != IPNetwork('192.0.2.0/32') +True + +An IP network or subnet is different from an individual IP address and therefore cannot be (directly) compared. + +If you want to compare them successfully, you must be explicit about which aspect of the IP network you wish to match against the IP address in question. + +You can use the index of the first or last address if it is a /32 like so : + +>>> IPAddress('192.0.2.0') == IPNetwork('192.0.2.0/32')[0] +True +>>> IPAddress('192.0.2.0') == IPNetwork('192.0.2.0/32')[-1] +True +>>> IPAddress('192.0.2.0') != IPNetwork('192.0.2.0/32')[0] +False + +You can also use the base address if this is what you wish to compare : + +>>> IPAddress('192.0.2.0') == IPNetwork('192.0.2.0/32').ip +True +>>> IPAddress('192.0.2.0') != IPNetwork('192.0.2.0/32').ip +False + +While this may seem a bit pointless at first, netaddr strives to keep IP addresses and network separate from one another while still allowing reasonable interoperability. + +----------- +DNS support +----------- + +It is a common administrative task to generate reverse IP lookups for DNS. This is particularly arduous for IPv6 addresses. + +Here is how you do this using an IPAddress object's `reverse_dns()` method. + +>>> IPAddress('172.24.0.13').reverse_dns +'13.0.24.172.in-addr.arpa.' +>>> IPAddress('fe80::feeb:daed').reverse_dns +'d.e.a.d.b.e.e.f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa.' + +Note that ``ip6.int`` is not used as this has been deprecated (see RFC 3152 for details). + +--------------------------- +Non standard address ranges +--------------------------- + +As CIDR is a relative newcomer given the long history of IP version 4 you are quite likely to come across systems and documentation which make reference to IP address ranges in formats other than CIDR. Converting from these arbitrary range types to CIDR and back again isn't a particularly fun task. Fortunately, netaddr tries to make this job easy for you with two purpose built classes. + +^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Arbitrary IP address ranges +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +You can represent an arbitrary IP address range using a lower and upper bound address in the form of an IPRange object. + +>>> r1 = IPRange('192.0.2.1', '192.0.2.15') +>>> r1 +IPRange('192.0.2.1', '192.0.2.15') + +You can iterate across and index these ranges just like and IPNetwork object. + +Importantly, you can also convert it to it's CIDR equivalent. + +>>> r1.cidrs() +[IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/29')] + +Here is how individual IPRange and IPNetwork compare. + +>>> IPRange('192.0.2.0', '192.0.2.255') != IPNetwork('192.0.2.0/24') +False +>>> IPRange('192.0.2.0', '192.0.2.255') == IPNetwork('192.0.2.0/24') +True + +You may wish to compare an IP range against a list of IPAddress and IPNetwork +objects. + +>>> r1 = IPRange('192.0.2.1', '192.0.2.15') +>>> addrs = list(r1) +>>> addrs +[IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3'), IPAddress('192.0.2.4'), IPAddress('192.0.2.5'), IPAddress('192.0.2.6'), IPAddress('192.0.2.7'), IPAddress('192.0.2.8'), IPAddress('192.0.2.9'), IPAddress('192.0.2.10'), IPAddress('192.0.2.11'), IPAddress('192.0.2.12'), IPAddress('192.0.2.13'), IPAddress('192.0.2.14'), IPAddress('192.0.2.15')] +>>> r1 == addrs +False + +Oops! Not quite what we were looking for or expecting. + +The way to do this is to get either side of the comparison operation into the same terms. + +>>> list(r1) == addrs +True + +That's more like it. + +The same goes for IPNetwork objects. + +>>> subnets = r1.cidrs() +>>> subnets +[IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/29')] +>>> r1 == subnets +False +>>> r1.cidrs() == subnets +True + +The above works if the list you are comparing contains one type or the other, but what if you have a mixed list of `IPAddress`, `IPNetwork` and string addresses? + +Time for some slightly more powerful operations. Let's make use of a new class for dealing with groups of IP addresses and subnets. The IPSet class. + +>>> ips = [IPAddress('192.0.2.1'), '192.0.2.2/31', IPNetwork('192.0.2.4/31'), IPAddress('192.0.2.6'), IPAddress('192.0.2.7'), '192.0.2.8', '192.0.2.9', IPAddress('192.0.2.10'), IPAddress('192.0.2.11'), IPNetwork('192.0.2.12/30')] +>>> s1 = IPSet(r1.cidrs()) +>>> s2 = IPSet(ips) +>>> s2 +IPSet(['192.0.2.1/32', '192.0.2.2/31', '192.0.2.4/30', '192.0.2.8/29']) +>>> s1 == s2 +True + +Let's remove one of the element from one of the IPSet objects and see what happens. + +>>> s2.pop() +IPNetwork('192.0.2.4/30') +>>> s1 == s2 +False + +This is perhaps a somewhat contrived example but it just shows you some of the capabilities on offer. + +See the IPSet tutorial :doc:`tutorial_03` for more details on that class. + +^^^^^^^^^^^^^^ +IP Glob ranges +^^^^^^^^^^^^^^ + +netaddr also supports a user friendly form of specifying IP address ranges using a "glob" style syntax. + +.. note:: At present only IPv4 globs are supported. + +>>> IPGlob('192.0.2.*') == IPNetwork('192.0.2.0/24') +True + +>>> IPGlob('192.0.2.*') != IPNetwork('192.0.2.0/24') +False + +As `IPGlob` is a subclass of `IPRange`, all of the same operations apply. + |