I am trying to understand how some code operates from the netaddr Python tutorials on https://pythonhosted.org/netaddr/tutorial_01.html. In particular the following tutorial.
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')]
I am a bit confused with the different options available. What is the difference between ip_list.extend([str(ip) for ip in IPNetwork('192.0.3.0/24')])
and ip_list.append(IPNetwork('192.0.4.0/25'))
.
If I don't want to start the list with an IPv6 (fe80::/120
) and instead want to use an IPv4 (192.0.4.0/24
) what would the syntax be. Would it be as simple as the following?
ip_list = IPNetwork('192.0.4.0/25')
Thanks.