forked from strawp/random-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip2cidr.py
More file actions
37 lines (26 loc) · 786 Bytes
/
ip2cidr.py
File metadata and controls
37 lines (26 loc) · 786 Bytes
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
#!/usr/bin/python3
"""
Usage: ip2cidr.py input_file
Based on http://www.unix.com/shell-programming-and-scripting/233825-convert-ip-ranges-cidr-netblocks.html
"""
import sys, re, netaddr
def sanitize (ip):
seg = ip.split('.')
return '.'.join([ str(int(v)) for v in seg ])
# pointer to input file
fp_source = open(sys.argv[1], "r")
ptrnSplit = re.compile(' - | , ')
for line in fp_source:
# parse on ' - ' et ' , '
s = re.split(ptrnSplit, line)
# sanitize ip: 001.004.000.107 --> 1.4.0.107 to avoid netaddr err.
ip = [ sanitize(v) for v in s[:2] ]
# conversion ip range to CIDR netblocks
# single ip in range
if ip[0] == ip[1]:
print( ip[0])
# multiple ip's in range
else:
ipCidr = netaddr.IPRange(ip[0], ip[1])
for cidr in ipCidr.cidrs():
print(cidr)