Subnetting in Python with IPv6

We can also find the subnet information from the IPv6Network objects, as follows:

>>> subnets = list(net6.subnets())
>>> subnets
[IPv6Network('2001:db8::/49'), IPv6Network('2001:db8:0:8000::/49')]

The ipaddress module includes various functions to create subnets and supernets, so we can check whether a network overlaps:

>>> ipnet = ipaddress.IPv6Network("2001:db8::/48")
>>> list(ipnet.subnets())
[IPv6Network('2001:db8::/49'), IPv6Network('2001:db8:0:8000::/49')]

We can use the subnets method to expand the network mask and obtain new networks:

>>> list(ipnet.subnets(prefixlen_diff=4))
[IPv6Network('2001:db8::/52'), IPv6Network('2001:db8:0:1000::/52'), IPv6Network('2001:db8:0:2000::/52'), IPv6Network('2001:db8:0:3000::/52'), IPv6Network('2001:db8:0:4000::/52'), IPv6Network('2001:db8:0:5000::/52'), IPv6Network('2001:db8:0:6000::/52'), IPv6Network('2001:db8:0:7000::/52'), IPv6Network('2001:db8:0:8000::/52'), IPv6Network('2001:db8:0:9000::/52'), IPv6Network('2001:db8:0:a000::/52'), IPv6Network('2001:db8:0:b000::/52'), IPv6Network('2001:db8:0:c000::/52'), IPv6Network('2001:db8:0:d000::/52'), IPv6Network('2001:db8:0:e000::/52'), IPv6Network('2001:db8:0:f000::/52')]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset