Skip to content

Instantly share code, notes, and snippets.

@jehiah
Created July 6, 2021 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jehiah/14bd984ac72b1d42574f17047a12ff68 to your computer and use it in GitHub Desktop.
Save jehiah/14bd984ac72b1d42574f17047a12ff68 to your computer and use it in GitHub Desktop.
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