-
Notifications
You must be signed in to change notification settings - Fork 0
/
ips2cidr.py
executable file
·46 lines (33 loc) · 1.02 KB
/
ips2cidr.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
"""Turns any IP addresses provided on stdin into the smallest set of CIDRs for them. Works for both ipv4 and ipv6."""
#Copyright William Stearns <[email protected]>
#Released under the GPL.
#Based on
#https://unix.stackexchange.com/questions/704845/convert-ip-list-to-minimal-cidr-representation
import ipaddress
import sys
cidr2ips_version = '0.3'
Devel = True
def Debug(DebugStr):
"""Prints a note to stderr"""
if Devel:
sys.stderr.write(DebugStr + '\n')
#AllSucceeded = True
all_ips = [ipaddress.ip_address(line.rstrip('\n')) for line in sys.stdin]
print('\n'.join([ip.with_prefixlen for ip in ipaddress.collapse_addresses(all_ips)]))
#for InLine in sys.stdin:
# InLine = InLine.rstrip('\n')
# #Debug(InLine)
# net = None
# try:
# new_ip = ipaddress.ip_network(InLine, strict=False)
# except ValueError:
# AllSucceeded = False
#
# if new_ip:
# for Address in net:
# print(Address)
#
#
#if not AllSucceeded:
# Debug('One or more input lines were not recognized as cidr networks or hosts')