forked from cldrn/cidrToIps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cidrToIps.py
executable file
·57 lines (53 loc) · 1.24 KB
/
cidrToIps.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
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/python
#This simple script reads a list of IP ranges in CIDR notation
# and prints the individual IP addresses.
#
#Posted this script because I got tired of re-writing the same script
# everytime I needed this. If you write security tools, please support
# CIDR notation.
#
#Usage: ./cidrToIps.py -i <inputfile>
#
#$ cat cidr-list.txt
#192.168.0.1/29
#192.168.1.1/30
#$ ./cidrToIps.py -i cidr-list.txt
#Reading IP ranges in CIDR notation from file: cidr-list.txt
#192.168.0.0
#192.168.0.1
#192.168.0.2
#192.168.0.3
#192.168.0.4
#192.168.0.5
#192.168.0.6
#192.168.0.7
#192.168.1.0
#192.168.1.1
#192.168.1.2
#192.168.1.3
#
#calderon@websec.mx
#
from netaddr import IPNetwork
import sys
import getopt
def main(argv):
inputfile = ''
try:
opts, args = getopt.getopt(argv,"i:",["ifile="])
except getopt.GetoptError:
print 'cidrToIps.py -i <inputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'cidrToIps.py -i <inputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
print 'Reading IP ranges in CIDR notation from file:', inputfile
fo = open(inputfile, "r+")
for line in fo:
for ip in IPNetwork(line):
print '%s' % ip
if __name__ == "__main__":
main(sys.argv[1:])