4

python 3.3.4 netaddr 7.12 Mac OSX 10.8.5

I have a list of IPNetworks that will not merge with netaddr.cidr_merge() even though some are adjacent. Am I doing something wrong?

import netaddr
from netaddr import IPNetwork
iplist =[
    IPNetwork('10.105.205.8/29'), 
    IPNetwork('10.105.205.16/28'), 
    IPNetwork('10.105.205.32/27'), 
    IPNetwork('10.105.205.64/26'), 
    IPNetwork('10.105.205.128/26'),
    IPNetwork('10.105.205.192/28'),
    IPNetwork('10.105.205.208/29'),
    IPNetwork('10.105.206.48/28'),
    IPNetwork('10.105.206.80/28')
    ]

>>> summary = netaddr.cidr_merge(iplist)
>>> summary == iplist
    True

from the documentation:

netaddr.cidr_merge(ip_addrs)

A function that accepts an iterable sequence of IP addresses and subnets merging them into the smallest possible list of CIDRs. It merges adjacent subnets where possible, those contained within others and also removes any duplicates.

Parameters: ip_addrs – an iterable sequence of IP addresses and subnets. Returns: a summarized list of IPNetwork objects.

Sean Vieira
  • 148,604
  • 32
  • 306
  • 290
Ghost
  • 106
  • 1
  • 6
  • after some playing around with this. I've found that if I add IPNetwork('10.105.204.0/29') to iplist then it will summarize further. – Ghost Jul 10 '14 at 02:31

2 Answers2

3

@duskwuff -

Thank you for your reply. I agree with the first part but the second...I think I know where your going with it, but it's not 100% accurate. For instance if I take an edited list from above, and append 10.105.205.0/29. The subnets will summarize to a /25. Yes they must be a power of 2, but all parts of the whole subnet must be present before netaddr will summarize, regardless if they are of equal size.

   iplist =[
        IPNetwork('10.105.205.8/29'), 
        IPNetwork('10.105.205.16/28'), 
        IPNetwork('10.105.205.32/27'), 
        IPNetwork('10.105.205.64/26'),
        ]
    >>> iplist.append(IPNetwork('10.105.205.0/29'))
    >>> netaddr.cidr_merge(iplist)
        [IPNetwork('10.105.205.0/25')]
Ghost
  • 106
  • 1
  • 6
2

netaddr is working correctly here. Not all adjacent subnets can be summarized.

For instance, consider the subnets 10.255.255.0/24 and 11.0.0.0/24. While they are adjacent — the first one ends with 10.255.255.255, and the second begins with 11.0.0.0 — they cannot be summarized, as they cross a boundary that is much larger than either of these two networks.

Additionally, regardless of how they are aligned, two adjacent networks can only be joined together if they're of equal size. Mismatched sizes cannot be combined into a single range, because the size of the combined network will not be a power of 2.