Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
minify a list of CIDrs to the largest CID listed
#!/usr/bin/env python
import sys
from ipaddress import IPv4Network
from collections import defaultdict
if __name__ == "__main__":
inputs = defaultdict(list)
for line in sys.stdin:
cidr = line.strip().decode('utf-8')
if not cidr:
continue
net = IPv4Network(cidr)
inputs[net.prefixlen].append(net)
c = []
for i in range(10, 25):
for net in inputs[i]:
found = False
for n in c:
if net.subnet_of(n):
found=True
break
if not found:
c.append(net)
for x in c:
print str(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment