forked from sumitmcc/subnetcalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubnetcalc.py
151 lines (121 loc) · 6.51 KB
/
subnetcalc.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
import random
import sys
import insert_subnet
def subnet_calc():
if(len(sys.argv) < 3):
print("Usage: python3 subnetcalc.py [IPv4/SUBNETNUMBER] Number of IPs to generate")
sys.exit(1)
try:
cidr_input_split = sys.argv[1].split("/")
ip_str = cidr_input_split[0].split(".") #IP Address as a list of numbers formatted as strings.
subnet_str = cidr_input_split[1]
int_octet_ip = [int(x) for x in ip_str]
octet_subnet = insert_subnet.convert_to_octal(int(subnet_str))
# Converting IP and subnet to binary
ip_in_binary = []
# Convert each IP octet to binary
ip_in_bin_octets = [bin(i).split("b")[1] for i in int_octet_ip]
# make each binary octet of 8 bit length by padding zeros
for i in range(0,len(ip_in_bin_octets)):
if len(ip_in_bin_octets[i]) < 8:
padded_bin = ip_in_bin_octets[i].zfill(8)
ip_in_binary.append(padded_bin)
else:
ip_in_binary.append(ip_in_bin_octets[i])
# join the binary octets
ip_bin_mask = "".join(ip_in_binary)
# print ip_bin_mask
sub_in_bin = []
# convert each subnet octet to binary
sub_bin_octet = [bin(i).split("b")[1] for i in octet_subnet]
# make each binary octet of 8 bit length by padding zeros
for i in sub_bin_octet:
if len(i) < 8:
sub_padded = i.zfill(8)
sub_in_bin.append(sub_padded)
else:
sub_in_bin.append(i)
# print sub_in_bin
sub_bin_mask = "".join(sub_in_bin)
# calculating number of hosts
no_zeros = sub_bin_mask.count("0")
no_ones = 32 - no_zeros
no_hosts = abs(2 ** no_zeros - 2)
# Calculating wildcard mask
wild_mask = []
for i in octet_subnet:
wild_bit = 255 - i
wild_mask.append(wild_bit)
wildcard = ".".join([str(i) for i in wild_mask])
# Calculating the network and broadcast address
network_add_bin = ip_bin_mask[:no_ones] + "0" * no_zeros
broadcast_add_bin = ip_bin_mask[:no_ones] + "1" * no_zeros
network_add_bin_octet = []
broadcast_binoct = []
[network_add_bin_octet.append(i) for i in [network_add_bin[j:j+8]
for j in range(0, len(network_add_bin), 8)]]
[broadcast_binoct.append(i) for i in [broadcast_add_bin[j:j+8]
for j in range(0,len(broadcast_add_bin),8)]]
network_add_dec_final = ".".join([str(int(i,2)) for i in network_add_bin_octet])
broadcast_add_dec_final = ".".join([str(int(i,2)) for i in broadcast_binoct])
# Calculate the host IP range
first_ip_host = network_add_bin_octet[0:3] + [(bin(int(network_add_bin_octet[3],2)+1).split("b")[1].zfill(8))]
first_ip = ".".join([str(int(i,2)) for i in first_ip_host])
last_ip_host = broadcast_binoct[0:3] + [bin(int(broadcast_binoct[3],2) - 1).split("b")[1].zfill(8)]
last_ip = ".".join([str(int(i,2)) for i in last_ip_host])
# print all the computed results
print ("\nThe entered ip address is: " + ".".join(ip_str))
print ("--------------------------------------------------")
print ("The entered subnet mask number is: " + subnet_str)
print ("--------------------------------------------------")
print ("Calculated subnet mask sequence is: " + ".".join([str(i) for i in octet_subnet]))
print ("--------------------------------------------------")
print ("Calculated number of hosts per subnet: {0}".format(str(no_hosts)))
print ("--------------------------------------------------")
print ("Calculated number of mask bits: {0}".format(str(no_ones)))
print ("--------------------------------------------------")
print ("Calculated wildcard mask is: {0}".format(wildcard))
print ("--------------------------------------------------")
print ("The Network address is: {0}".format(network_add_dec_final))
print ("--------------------------------------------------")
print ("The Broadcast address is: {0}".format(broadcast_add_dec_final))
print ("--------------------------------------------------")
print ("IP address range is: {0} - {1}".format(first_ip, last_ip))
print ("--------------------------------------------------")
print ("Maximum number of subnets is: " + str(2**abs(24 - no_ones)))
print ("--------------------------------------------------")
print ("")
list_ip = []
# generate 10 random valid IP addresses
for ip_num in range(int(sys.argv[2])):
randip = []
# Check if the octet bit is same in first and last host address.
# If same, append it. else generate random IP
for i in range(0,len(first_ip_host)):
for j in range(0,len(last_ip_host)):
if i == j:
if first_ip_host[i] == last_ip_host[j]:
randip.append(int(first_ip_host[i],2))
else:
randip.append(random.randint(int(first_ip_host[i],2),int(last_ip_host[j],2)))
random_ip_final = ".".join(str(i) for i in randip)
# check if generated IP has already been printed. If so, compute again till unique IP is obtained
if random_ip_final in list_ip:
# if all IPs in the host range are used, exit
if len(list_ip) == no_hosts:
print("All IPs in the range used up, exiting\n")
print ("--------------------------------------------------")
break
continue
else:
print("Generated IP address " + str(ip_num + 1))
print(random_ip_final + '\n')
print ("--------------------------------------------------")
list_ip.append(random_ip_final)
except KeyboardInterrupt:
print("Interrupted by the User, exiting\n")
except ValueError:
print("Seem to have entered an incorrect value, exiting\n")
# Calling the above defined function
if __name__ == '__main__':
subnet_calc()