minify a list of CIDrs to the largest CID listed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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