-
Notifications
You must be signed in to change notification settings - Fork 1
/
subnet.py
106 lines (83 loc) · 2.62 KB
/
subnet.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
from termcolor import *
import colorama
colorama.init()
def calculate_subnet(ip, cidr):
init = 0
start_cap = []
end_cap = []
host_range = []
ipRange = []
given_host = None
seg = None
start_host = 0
end_host = 0
subnetting_error = 0
classes = [0, 8, 16, 24, 32]
subnetting_error = int(0)
try:
cidr = int(cidr)
except:
return False
if int(cidr) == 0:
ipRange.append(ip[0] + '.' + ip[1] + '.' + ip[2] + '.' + ip[3])
return ipRange
else:
try:
for i in range(len(classes)):
if cidr >= classes[i] and cidr < classes[i+1]:
seg = classes[i]
except:
return False
if subnetting_error != 1:
if seg == 24:
given_host = int(ip[3])
elif seg == 16:
given_host = int(ip[2])
# print given_host #DEBUG
net_bits = cidr - seg
subnets = 2 ** net_bits
s_range = 256 / subnets
try:
for i in range(subnets):
start_cap.append(int(init))
init = init + s_range
end_cap.append(int(init) - 1)
except:
return False
try:
for j in range(subnets):
if (given_host) >= start_cap[j] and (given_host) <= end_cap[j]:
start_host = int(start_cap[j])
end_host = int(end_cap[j])
else:
sub_calc_error = 1
except:
return False
for k in range(start_host, end_host+1):
(host_range.append(k))
if seg == 24:
for l in host_range:
ipRange.append(ip[0] + "." + ip[1] + "." + ip[2] + "." + str(l))
return ipRange
elif seg == 16:
for l in host_range:
for m in range(256):
ipRange.append(ip[0] + "." + ip[1] + "." + str(l) + "." + str(m))
return ipRange
else:
cprint(
"Program is prototype. Classes A and B hasn't been developed yet. Develop it yourself from the above codes!!! ",
"red")
return False
########## CODE EXAMINATION AND TESTING ###########
#
# raw_ip = '147.211.117.114'
# ip = raw_ip.split('.')
# cidr = raw_input("Enter CIDR: ")
# result = calculate_subnet(ip, cidr)
#
# print result
#
# # if result != None:
# # for values in result:
# # print values