-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubnet_calculator_python3.py
192 lines (138 loc) · 6.95 KB
/
subnet_calculator_python3.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#Application #1 - Part #1
#Import the random and sys modules
import random
import sys
def subnet_calc():
try:
print("\n")
#checking IP address validity
while True:
ip_address = input("Enter an IP address: ")
#Checking octets
ip_octets = ip_address.split('.')
if (len(ip_octets) == 4) and (1 <= int(ip_octets[0]) <= 223) and (int(ip_octets[0]) != 127) and (int(ip_octets[0]) != 169 or int(ip_octets[1]) != 254) and (0 <= int(ip_octets[1]) <= 255 and 0 <= int(ip_octets[2]) <= 255 and 0 <= int(ip_octets[3]) <= 255):
break
else:
print("\nThe IP address in INVALID! Please retry!\n")
continue
masks = [255, 254, 252, 248, 240, 224, 192, 128, 0]
#Checking subnet mask validity
while True:
subnet_mask = input("Enter a subnet mask: ")
#Checking octets
mask_octets = subnet_mask.split('.')
if(len(mask_octets) == 4) and (int(mask_octets[0]) == 255) and (int(mask_octets[1]) in masks) and (int(mask_octets[2]) in masks) and (int(mask_octets[3]) in masks) and (int(mask_octets[0]) >= int(mask_octets[1]) >= int(mask_octets[2]) >= int(mask_octets[3])):
break
else:
print("\nThe subnet mask is INVALID! Please retry!\n")
continue
#Application #1 - Part #2
#Algorithm for subnet identification, based on IP and subnet mask
#convert mask to binary string
mask_octets_binary = []
for octet in mask_octets:
binary_octet = bin(int(octet)).lstrip('0b')
#print(binary_octet)
mask_octets_binary.append(binary_octet.zfill(8))
#print (mask_octets_binary)
binary_mask = "".join(mask_octets_binary)
#print(decimal_mask)
#Example: for 255.255.255.0 => 11111111111111111111111100000000
#Counting host bits in the mask and calculating number of hosts/subnet
no_of_zeros = binary_mask.count("0")
no_of_ones = 32 - no_of_zeros
no_of_hosts = abs(2 ** no_of_zeros - 2) #return a positive value for the /32 mask (all 255s)
#print(no_of_zeros)
#print(no_of_ones)
#print(no_of_hosts)
#Obtaining wildcard mask
wildcard_octets = []
for octet in mask_octets:
wild_octet = 255 - int(octet)
wildcard_octets.append(str(wild_octet))
#print(wildcard_octets)
wildcard_mask = ".".join(wildcard_octets)
#print(wildcard_mask)
#Application #1 - Part #3
#Convert IP to binary string
ip_octets_binary = []
for octet in ip_octets:
binary_octet = bin(int(octet)).lstrip('0b')
#print(binary_octet)
ip_octets_binary.append(binary_octet.zfill(8))
#print(ip_octets_binary)
#Example: for 192.168.10.1 =>
binary_ip = "".join(ip_octets_binary)
#print(binary_ip)
#Example: for 192.168.2.100 => 11000000101010000000101000000001
#Getting the network address and broadcast address from the binary strings obtained above
network_address_binary = binary_ip[:(no_of_ones)] + "0" * no_of_zeros
print(network_address_binary)
broadcast_address_binary = binary_ip[:(no_of_ones)] + "1" * no_of_zeros
print(broadcast_address_binary)
#Converting everything back to decimal_mask (readable format)
net_ip_octets = []
#range(0, 32, 8) means 0, 8, 16, 24
for bit in range(0, 32, 8):
net_ip_octet = network_address_binary[bit: bit + 8]
net_ip_octets.append(net_ip_octet)
#We will end up with 4 slices of the binary IP address: [0: 8], [8: 16], [16: 24] and ##############
print(net_ip_octets)
net_ip_address = []
for each_octet in net_ip_octets:
net_ip_address.append(str(int(each_octet, 2)))
print(net_ip_address)
network_address = ".".join(net_ip_address)
#print(network_address)
bst_ip_octets = []
#range(0, 32, 8) means 0, 8, 16, 24
for bit in range(0, 32, 8):
bst_ip_octet = broadcast_address_binary[bit: bit + 8]
bst_ip_octets.append(bst_ip_octet)
#print(bst_ip_address)
bst_ip_address = []
for each_octet in bst_ip_octets:
bst_ip_address.append(str(int(each_octet, 2)))
broadcast_address = ".".join(bst_ip_address)
print(broadcast_address)
#Results for selected IP/mask
print("\n")
print("Network address is: %s" % network_address)
print("Broadcast address is: %s" % broadcast_address)
print("Number of valid hosts per subnet: %s" % no_of_hosts)
print("Wildcard mask: %s" % wildcard_mask)
print("Mask bits: %s" % no_of_ones)
print("\n")
#Application #1 - Part #4
#Generation of random IP addresses in the subnet
while True:
generate = input("Generate random IP address from this subnet? (y/n)")
if generate == "y":
generated_ip = []
#Obtain available IP address in range, based on the difference between octets in broadcast address and network address
for indexb, oct_bst in enumerate(bst_ip_address):
#print(indexb, oct_bst)
for indexn, oct_net in enumerate(net_ip_address):
if indexb == indexn:
if oct_bst == oct_net:
#Add identical octets to the generated_ip list
generated_ip.append(oct_bst)
else:
#Generate random number(s) from within octet intervals and list
generated_ip.append(str(random.randint(int(oct_net), int(oct_bst))))
#IP address generated from the subnet pool
print(generated_ip)
y_iaddr = ".".join(generated_ip)
#print(y_iaddr)
print("Random IP address is: %s" % y_iaddr)
print("\n")
continue
else:
print("Ok, bye!\n")
break
except KeyboardInterrupt:
print("\n\nProgram aborted by user. Exiting...\n")
sys.exit()
#Calling the function
subnet_calc()
#End of program