Skip to content

Instantly share code, notes, and snippets.

@zhongfly
Last active May 31, 2021 06:37
Show Gist options
  • Save zhongfly/cb74fe1154396df6771c736432e84ac0 to your computer and use it in GitHub Desktop.
Save zhongfly/cb74fe1154396df6771c736432e84ac0 to your computer and use it in GitHub Desktop.
合并多个.txt或.rules文件中的所有ip cidr。usage: python3 cidr-merger.py [-h] [-o OUTFILE] filename [filename ...]
# -*- encoding: utf-8 -*-
from netaddr import *
import os
import argparse
# pyinstaller需要用以下方法获得exe所在位置
# workDir = os.path.dirname(os.path.realpath(sys.argv[0]))
workDir = os.getcwd()
def cidrReader(file):
with open(file,'r',encoding='utf-8') as f:
(file_path, filename) = os.path.split(file)
lines = f.readlines()
cidrs = []
for line in lines:
line = line.strip()
if line.startswith("#") or line == "" :
continue
try:
cidrs.append(IPNetwork(line))
except AddrFormatError as e:
print(f"Find error in {filename}: {e}")
print(f'"{filename}" have read.')
return cidrs
parser = argparse.ArgumentParser(description='merge multiple cidr.txt')
parser.add_argument(dest='filenames',metavar='filename', nargs='+')
parser.add_argument('-o', dest='outfile', action='store', help='output file', default='merged_cidr.txt')
args = parser.parse_args()
cidrs = []
for path in args.filenames:
if os.path.isdir(path):
fileList = [entry.path for entry in os.scandir(path) if entry.name.endswith(".txt") or entry.name.endswith(".rules")]
for file in fileList:
cidrs.extend(cidrReader(file))
else:
cidrs.extend(cidrReader(path))
result = cidr_merge(cidrs)
output = os.path.join(workDir,args.outfile)
with open(output,'w',encoding='utf-8') as f:
for ip in result:
line = str(ip) if ip.prefixlen != 32 else str(ip.ip)
f.write(line+'\n')
input("All done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment