-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathip_calculator.py
77 lines (63 loc) · 2.49 KB
/
ip_calculator.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
__author__ = 'Kailash Joshi'
import sys
def _dec_to_binary(ip_address):
return map(lambda x: bin(x)[2:].zfill(8), ip_address)
def _negation_mask(net_mask):
wild = list()
for i in net_mask:
wild.append(255 - int(i))
return wild
class IPCalculator(object):
def __init__(self, ip_address, cdir=24):
if '/' in ip_address:
self._address_val, self._cidr = ip_address.split('/')
self._address = map(int, self._address_val.split('.'))
else:
self._address = map(int, ip_address.split('.'))
self._cidr = cdir
self.binary_IP = _dec_to_binary(self._address)
self.binary_Mask = None
self.negation_Mask = None
self.network = None
self.broadcast = None
def __repr__(self):
print "Calculating the IP range of %s/%s" % (".".join(map(str, self._address)), self._cidr)
print "=================================="
print "Netmask %s" % (".".join(map(str, self.net_mask())))
print "Network ID %s" % (".".join(map(str, self.network_ip())))
print "Subnet Broadcast address %s" % (".".join(map(str, self.broadcast_ip())))
print "Host range %s" % (self.host_range())
print "Max number of hosts %s" % (self.number_of_host())
def net_mask(self):
mask = [0, 0, 0, 0]
for i in range(int(self._cidr)):
mask[i / 8] += 1 << (7 - i % 8)
self.binary_Mask = _dec_to_binary(mask)
self.negation_Mask = _dec_to_binary(_negation_mask(mask))
return mask
def broadcast_ip(self):
broadcast = list()
for x, y in zip(self.binary_IP, self.negation_Mask):
broadcast.append(int(x, 2) | int(y, 2))
self.broadcast = broadcast
return broadcast
def network_ip(self):
network = list()
for x, y in zip(self.binary_IP, self.binary_Mask):
network.append(int(x, 2) & int(y, 2))
self.network = network
return network
def host_range(self):
min_range = self.network
min_range[-1] += 1
max_range = self.broadcast
max_range[-1] -= 1
return "%s - %s" % (".".join(map(str, min_range)), ".".join(map(str, max_range)))
def number_of_host(self):
return (2 ** sum(map(lambda x: sum(c == '1' for c in x), self.negation_Mask))) - 2
def ip_calculate(ip):
ip = IPCalculator(ip)
ip.__repr__()
if __name__ == '__main__':
ip = sys.argv[1] if len(sys.argv) > 1 else sys.exit(0)
ip_calculate(ip)