aboutsummaryrefslogtreecommitdiff
path: root/utils/mergeipaddrlist.py
blob: 2248f1a7edf2a630188be7fa1290302936b0af81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python3

import sys
import netaddr

if len(sys.argv) == 3:
    proto = sys.argv[2]

if len(sys.argv) < 2:
    print("Usage: mergeipaddrlist.py <file>")
    sys.exit(1)

ipFile = open(sys.argv[1])
ipAddresses = list(ipFile.readlines())
ipAddresses = [x.replace("\n", "") for x in ipAddresses]
ipAddresses = sorted(ipAddresses)
cidrs = netaddr.cidr_merge(ipAddresses)

lines = 0
for cidr in cidrs:
    lines += 1
    print(cidr)

if lines == 0:
    sys.stderr.write(f'{sys.argv[0]}: file {sys.argv[1]} is empty\n')