This is a small python snippet to merge multiple subnet lists mixed with single ip addresses and CIDRs.
netaddr module required, install it with “pip install netaddr”.
Usage: python merge.py merge1.txt merge2.txt etc.
from netaddr import cidr_merge
import sys
if __name__ == "__main__":
if len(sys.argv) == 1:
print("No merge files defined. merge.py merge1.txt merge2.txt ...")
sys.exit(1)
MERGE_FILES = sys.argv[1:]
buffer = []
for MERGE_FILE in MERGE_FILES:
with open(MERGE_FILE) as mf:
for line in mf.readlines():
buffer.append(line.strip())
for element in cidr_merge(buffer):
print(element.cidr)