-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathip-calculator.py
310 lines (260 loc) · 12.7 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env python3
'''
Simple IP-Calculator
'''
from packages.modules import BinaryOctet, DecimalConversion
checked_ip = ""
checked_subnet = ""
checked_prefix = ""
checked_ip_class = ""
checked_net_id = ""
checked_total_host = ""
checked_broadcast = ""
checked_ip_type = ""
def IpCalc():
'''calculate ip address information'''
# set allowed and disallowed ip and subnets
allowed_masks = [0, 128, 192, 224, 240, 248, 252, 254, 255]
disallowed_first_ip_octets = [0, 127]
default_subnet_masks = {"A":"255.0.0.0", "B":"255.255.0.0", "C":"255.255.255.0"}
ip_checking = True
while ip_checking :
try :
ip_address = input("IP address :")
subnet_mask = input("Subnet mask :").strip()
ip_address_octets = ip_address.split(".")
int_ip_address_octets = [int(x) for x in ip_address_octets]
# seting default subnet masks
if subnet_mask == "" :
if 1 <= int_ip_address_octets[0] <= 126 :
subnet_mask = default_subnet_masks["A"]
elif 128 <= int_ip_address_octets[0] <= 191 :
subnet_mask = default_subnet_masks["B"]
elif 192 <= int_ip_address_octets[0] <= 223 :
subnet_mask = default_subnet_masks["C"]
# ip address checking
if len(int_ip_address_octets) == 4 and \
(1 <= int_ip_address_octets[0] <= 223) and \
(int_ip_address_octets[0] not in disallowed_first_ip_octets) and \
(0 <= int_ip_address_octets[1] <= 255) and \
(0 <= int_ip_address_octets[2] <= 255) and \
(0 <= int_ip_address_octets[3] <= 254) :
checked_ip = ip_address
else:
print("Invalid IP address !")
continue
# subnet mask checking
subnet_mask_octets = subnet_mask.split(".")
int_subnet_mask_octets = [int(y) for y in subnet_mask_octets]
if int_subnet_mask_octets[0] == 255 and \
int_subnet_mask_octets[1] in allowed_masks and \
int_subnet_mask_octets[2] in allowed_masks and \
int_subnet_mask_octets[3] in allowed_masks and \
int_subnet_mask_octets[3] <= 252 and \
(int_subnet_mask_octets[0] >= int_subnet_mask_octets[1] >= int_subnet_mask_octets[2] >= int_subnet_mask_octets[3]):
checked_subnet = subnet_mask
else:
print("Invalid subnet mask !")
continue
# set a dictionary for subnet calculations and prefix
equal_zero_subnet = {"255": 0, "254": 1, "252": 2, "248": 3, "240": 4, "224": 5, "192": 6, "128": 7, "0": 8}
subnet_oct1_zeros = equal_zero_subnet[subnet_mask_octets[0]]
subnet_oct2_zeros = equal_zero_subnet[subnet_mask_octets[1]]
subnet_oct3_zeros = equal_zero_subnet[subnet_mask_octets[2]]
subnet_oct4_zeros = equal_zero_subnet[subnet_mask_octets[3]]
#this is host zeros not network zeros
total_HOST_zeros = subnet_oct1_zeros + subnet_oct2_zeros + subnet_oct3_zeros + subnet_oct4_zeros
checked_prefix = 32 - total_HOST_zeros
# set ip class (A,B,C)
if 1 <= int_ip_address_octets[0] <= 126 :
checked_ip_class = "A"
elif 128 <= int_ip_address_octets[0] <= 191 :
checked_ip_class = "B"
elif 192 <= int_ip_address_octets[0] <= 223 :
checked_ip_class = "C"
# set number of hosts per network
hosts = 2 ** total_HOST_zeros - 2
checked_total_host = hosts
#set net ID and broadcast ID
total_NETWORK_zeros = 32 - total_HOST_zeros
# converting normal *SUBNET MASK* bins into 8 length bins
complete_SUBNET_8_length_bin = []
for subnet_bins in int_subnet_mask_octets :
subnet_bin_changing = BinaryOctet(subnet_bins)
complete_SUBNET_8_length_bin.append(subnet_bin_changing)
# converting normal *IP ADDRESS* bins into 8 length bins
complete_IP_8_length_bin = []
for network_bins in int_ip_address_octets :
network_bin_changing = BinaryOctet(network_bins)
complete_IP_8_length_bin.append(network_bin_changing)
#network id calculating
netid_IP_8_length_bin = complete_IP_8_length_bin[:]
#convert 8-15 prefixes
if (8 <= total_NETWORK_zeros <= 15) :
set_end_main_seq = total_NETWORK_zeros - 8
main_seq = complete_IP_8_length_bin[1]
main_seq = main_seq[0:set_end_main_seq]
zero_network_number = 16 - total_NETWORK_zeros
str_zero_network_number = ""
for str_zero in range(0,zero_network_number):
str_zero_network_number += "0"
new_network_id_oct2 = main_seq + str_zero_network_number
new_network_id_oct3 = "00000000"
new_network_id_oct4 = "00000000"
netid_IP_8_length_bin[1] = new_network_id_oct2
netid_IP_8_length_bin[2] = new_network_id_oct3
netid_IP_8_length_bin[3] = new_network_id_oct4
# convert 16-23 prefixes
elif (16 <= total_NETWORK_zeros <= 23) :
set_end_main_seq = total_NETWORK_zeros - 16
main_seq = complete_IP_8_length_bin[2]
main_seq = main_seq[0:set_end_main_seq]
zero_network_number = 24 - total_NETWORK_zeros
str_zero_network_number = ""
for str_zero in range(0, zero_network_number):
str_zero_network_number += "0"
new_network_id_oct3 = main_seq + str_zero_network_number
new_network_id_oct4 = "00000000"
netid_IP_8_length_bin[2] = new_network_id_oct3
netid_IP_8_length_bin[3] = new_network_id_oct4
# convert 24-30 prefixes
elif (24 <= total_NETWORK_zeros <= 30) :
set_end_main_seq = total_NETWORK_zeros - 24
main_seq = complete_IP_8_length_bin[3]
main_seq = main_seq[0:set_end_main_seq]
zero_network_number = 32 - total_NETWORK_zeros
str_zero_network_number = ""
for str_zero in range(0, zero_network_number):
str_zero_network_number += "0"
new_network_id_oct4 = main_seq + str_zero_network_number
netid_IP_8_length_bin[3] = new_network_id_oct4
# convert to decimal and convert it to string number
net_decimal_list = []
for decimal_net_num in netid_IP_8_length_bin :
new_dec = DecimalConversion(decimal_net_num)
net_decimal_list.append(new_dec)
checked_net_id = ".".join(net_decimal_list)
# broadcast calculating
broadcast_IP_8_length_bin = complete_IP_8_length_bin[:]
# convert 8-15 prefixes
if (8 <= total_NETWORK_zeros <= 15):
set_end_main_seq = total_NETWORK_zeros - 8
main_seq = complete_IP_8_length_bin[1]
main_seq = main_seq[0:set_end_main_seq]
zero_network_number = 16 - total_NETWORK_zeros
str_zero_network_number = ""
for str_zero in range(0, zero_network_number):
str_zero_network_number += "1"
new_broadcast_oct2 = main_seq + str_zero_network_number
new_broadcast_oct3 = "11111111"
new_broadcast_oct4 = "11111111"
broadcast_IP_8_length_bin[1] = new_broadcast_oct2
broadcast_IP_8_length_bin[2] = new_broadcast_oct3
broadcast_IP_8_length_bin[3] = new_broadcast_oct4
# convert 16-23 prefixes
elif (16 <= total_NETWORK_zeros <= 23):
set_end_main_seq = total_NETWORK_zeros - 16
main_seq = complete_IP_8_length_bin[2]
main_seq = main_seq[0:set_end_main_seq]
zero_network_number = 24 - total_NETWORK_zeros
str_zero_network_number = ""
for str_zero in range(0, zero_network_number):
str_zero_network_number += "1"
new_broadcast_oct3 = main_seq + str_zero_network_number
new_broadcast_oct4 = "11111111"
broadcast_IP_8_length_bin[2] = new_broadcast_oct3
broadcast_IP_8_length_bin[3] = new_broadcast_oct4
# convert 24-30 prefixes
elif (24 <= total_NETWORK_zeros <= 30):
set_end_main_seq = total_NETWORK_zeros - 24
main_seq = complete_IP_8_length_bin[3]
main_seq = main_seq[0:set_end_main_seq]
zero_network_number = 32 - total_NETWORK_zeros
str_zero_network_number = ""
for str_zero in range(0, zero_network_number):
str_zero_network_number += "1"
new_broadcast_oct4 = main_seq + str_zero_network_number
broadcast_IP_8_length_bin[3] = new_broadcast_oct4
#convert to decimal and convert it to string number
broadcast_decimal_list = []
for decimal_broadcast_num in broadcast_IP_8_length_bin:
new_dec = DecimalConversion(decimal_broadcast_num)
broadcast_decimal_list.append(new_dec)
checked_broadcast = ".".join(broadcast_decimal_list)
# checking the type of IP address (public or private)
if int_ip_address_octets[0] == 10 :
ip_type = "Private"
checked_ip_type = ip_type
elif int_ip_address_octets[0] == 192 and \
int_ip_address_octets[1] == 168 :
ip_type = "Private"
checked_ip_type = ip_type
elif int_ip_address_octets[0] == 169 and \
int_ip_address_octets[1] == 254 :
ip_type = "Private"
checked_ip_type = ip_type
elif int_ip_address_octets[0] == 172 and \
16 <= int_ip_address_octets[1] <= 31 :
ip_type = "Private"
checked_ip_type = ip_type
else :
ip_type = "Public"
checked_ip_type = ip_type
# error handling
except ValueError :
print("Invalid IP address or subnet mask !")
again_or_not = input("again ? (y) :").lower()
if again_or_not == "y" or again_or_not == "yes" :
ip_checking = True
else:
ip_checking = False
except KeyboardInterrupt :
print("Canceled by user ...")
except IndexError :
print("Invalid IP address or subnet mask !")
again_or_not = input("again ? (y) :").lower()
if again_or_not == "y" or again_or_not == "yes":
ip_checking = True
else:
ip_checking = False
except KeyError :
print("Invalid IP address or subnet mask !")
again_or_not = input("again ? (y) :").lower()
if again_or_not == "y" or again_or_not == "yes":
ip_checking = True
else:
ip_checking = False
# error handling
else:
try :
print("\n")
print("the Entered IP address : ", checked_ip)
print("")
print("the Entered Subnet mask : ", checked_subnet)
print("")
print("IP Type : " + checked_ip_type)
print("")
print("Prefix length : ", checked_prefix)
print("")
print("IP class : ", checked_ip_class)
print("")
print("Network ID : ", checked_net_id)
print("")
print("Total hosts per network : ", abs(checked_total_host))
print("")
print("Broadcast : " , checked_broadcast)
print("-------------------------")
again_or_not = input("again ? (y) :").lower()
if again_or_not == "y" or again_or_not == "yes":
ip_checking = True
else:
ip_checking = False
print("")
except TypeError :
print("Invalid information")
again_or_not = input("again ? (y) :").lower()
if again_or_not == "y" or again_or_not == "yes":
ip_checking = True
else:
ip_checking = False
IpCalc()