Created
May 2, 2018 06:42
-
-
Save jamesdavidson/9d9dc2337f189439b262341ab3fad88d to your computer and use it in GitHub Desktop.
Subtracts one list of address ranges from another list of ranges, returning a list of addresses as /32 ranges.
This file contains hidden or 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 | |
# This program requires two filename arguments. Each file should contain a list | |
# of address ranges (CIDR blocks). The output will be a list of address which | |
# are in the first list but not the second expressed as /32 ranges. | |
from __future__ import unicode_literals | |
import sys | |
import ipaddress | |
import os | |
if __name__ == "__main__": | |
assert len(sys.argv) == 3, "two arguments required" | |
assert os.path.isfile(sys.argv[1]), "file %s does not exist" % sys.argv[1] | |
assert os.path.isfile(sys.argv[2]), "file %s does not exist" % sys.argv[2] | |
xs = {} | |
f1 = open(sys.argv[1], 'r') | |
for line in f1.readlines() : | |
for x in ipaddress.IPv4Network(unicode(line.strip()), strict=False): | |
xs[x] = True | |
f1.close() | |
f2 = open(sys.argv[2], 'r') | |
for line in f2.readlines() : | |
for x in ipaddress.IPv4Network(unicode(line.strip()), strict=False): | |
if xs.get(x): | |
del xs[x] | |
f2.close() | |
for x in xs: | |
print "%s/32" % x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment