I know how to detect overlapping networks. There are two ways to do this: by using netaddr's "IPNetwork in IPNetwork" or ipaddress's "overlaps" method. The question is how to find overlapping networks in array in most efficient way.
At the moment, I use this code:
networks = {
IPNetwork('10.1.0.0/24'): 'net2',
IPNetwork('10.0.0.0/8'): 'net1',
IPNetwork('10.0.0.0/16'): 'net3',
IPNetwork('10.0.0.0/24'): 'net4',
IPNetwork('192.168.0.0/16'): 'net5',
IPNetwork('192.168.0.0/23'): 'net6',
IPNetwork('192.168.1.0/24'): 'net7',
IPNetwork('172.16.0.0/12'): 'net8'
}
net = sorted([key for key in networks.keys()])
for pi in range(0, len(net)-1):
for si in range(pi+1, len(net)):
if net[si] in net[pi]:
print(f'{net[si]} ({networks[net[si]]}) overlaps with '
f'{net[pi]} ({networks[net[pi]]})')
It does the job, but uses many iterations (e.g. for 8 entries there are 7+6+5+... = 28 comparisons) and I'm looking for a more efficient way.