-
Notifications
You must be signed in to change notification settings - Fork 7
/
netcider.py
163 lines (137 loc) · 5.53 KB
/
netcider.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
import sys
import operator
import itertools
try:
from functools import *
except:
pass
class cidr():
def __init__(self, address):
try:
index = address.index('/')
self.base = address[:index]
intMask = address[index+1:]
self.netmask = self.netmask(intMask)
self.wildcard = self.wildcard(intMask)
self.binBase = self.addressToBin(self.base)
self.subnet = self.listToString(self.network(self.base, self.netmask))
self.hostmin = self.hostMin(self.subnet)
self.hostmax = self.hostMax(self.subnet, self.wildcard)
self.total = self.numhosts(self.wildcard)
self.broadcast = self.hostMin(self.hostmax)
self.allips = self.getIpList(self.hostmin, self.hostmax)
except Exception as e:
print(e)
def update(self, address):
try:
index = address.index('/')
self.base = address[:index]
intMask = address[index+1:]
self.netmask = self.netmask(intMask)
self.wildcard = self.wildcard(intMask)
self.binBase = self.addressToBin(self.base)
self.subnet = self.listToString(self.network(self.base, self.netmask))
self.hostmin = self.hostMin(self.subnet)
self.hostmax = self.hostMax(self.subnet, self.wildcard)
self.total = self.numhosts(self.wildcard)
self.broadcast = self.hostMin(self.hostmax)
self.allips = self.getIpList(self.hostmin, self.hostmax)
except Exception as e:
print(e)
def toString(self):
print 'Base:\t\t%s' % self.base
print 'Netmask:\t%s' % self.netmask
print 'Wildcard:\t%s' % self.wildcard
print 'Broadcast:\t%s' % self.broadcast
print 'Subnet ID:\t%s' % self.subnet
print 'Host min:\t%s' % self.hostmin
print 'Host max:\t%s' % self.hostmax
print 'Total Hosts:\t%s' % self.total
def getIpList(self, hostmin, hostmax):
tmpmin = hostmin.split('.')
tmpmax = hostmax.split('.')
ranges = [ range(i, j + 1) for i, j in zip(list(map(int, tmpmin)),list(map(int, tmpmax))) ]
complete = []
for ip in itertools.product(*ranges):
complete.append( '.'.join(list(map(str, list(ip)))))
return complete
def numhosts(self, wildcard):
tmpWild = list(map(int, wildcard.split('.')))
ranges = list(map(lambda e: len(range(0, e + 1)), tmpWild))
numhosts = reduce(operator.mul, ranges)
return numhosts if numhosts > 0 else 1
def hostMin(self, address):
temp = address.split('.')
temp[3] = str(int(temp[3]))
return self.listToString(temp)
def hostMax(self, address, wildcard):
tmpAddr = address.split('.')
tmpWild = wildcard.split('.')
tmpWild[3] = str(int(tmpWild[3]))
return self.listToString(list(map(sum, zip(list(map(int, tmpAddr)), list(map(int, tmpWild))))))
def netmask(self, mask):
binMask = '%s%s' % ('1'*int(mask), '0'*(32-int(mask)))
maskList = list(map(''.join, zip(*[iter(binMask)] * 8)))
netmask = self.binToAddress(maskList)
return self.listToString(netmask)
def wildcard(self, mask):
binMask = '%s%s' % ('1'*int(mask), '0'*(32-int(mask)))
maskList = list(map(''.join, zip(*[iter(binMask)] * 8)))
netmask = self.binToAddress(maskList)
wildcard = [ 255-val for val in netmask ]
return self.listToString(wildcard)
def listToString(self, ipList):
return list(map('.'.join, [ list(map(str, ipList))] ))[0]
def network(self, address, netmask):
binNetwork = [ bin(int(a,2) & int(b,2))[2:].zfill(8) for a, b in zip(self.addressToBin(address), self.addressToBin(netmask))]
return self.binToAddress(binNetwork)
def addressToBin(self, address):
return [ bin(int(val))[2:].zfill(8) for val in address.split('.') ]
def printList(self):
try:
for ip in self.allips:
print(ip)
except Exception as e:
print(e)
def binToAddress(self, binAddress):
return [ int(val,2) for val in binAddress ]
def usage():
title = 'Net Cider v1.0 beta'
author = 'Shawn Evans'
email = 'sevans@nopsec.com'
print ''
print '='*61
print '='*((59-len(title))/2), title,'='*((59-len(title))/2)
print '='*((59-len(author))/2), author,'='*((59-len(author))/2)
print '='*((59-len(email))/2), email,'='*((59-len(email))/2)
print '='*61
print ''
print '-o\tOutput full IP range to stdout'
print ''
print 'Example:'
print '$ python netCider.py 192.168.0.2/24'
print '$ python netCider.py -o 192.168.0.2/24'
print ''
if __name__ == '__main__':
ipLocation = 0
cidrIP = []
if not (sys.stdin.isatty()):
stdin_ip = sys.stdin.read().split('\n')
else:
ipLocation = reduce(lambda x, y: x + y, [ i if (val.find('.') > 0 and val.find('/')) > 0 else 0 for i, val in enumerate(sys.argv) ])
if ipLocation > 0:
cidrIP.append(cidr(sys.argv[ipLocation]))
elif not (sys.stdin.isatty()):
for ip in stdin_ip:
cidrIP.append(cidr(ip))
else:
usage()
sys.exit()
if '-o' in sys.argv:
for cidrItem in cidrIP:
cidrItem.printList()
sys.exit()
else:
for cidrItem in cidrIP:
cidrItem.toString()