-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubnetting.py
124 lines (85 loc) · 3 KB
/
subnetting.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
# WIP?
import re
class CustomException(Exception):
"""Custom Exception"""
def getSubnetMask(host_bits:int):
return "1"*(32 - host_bits) + "0"*host_bits
def isValidIp(ip:str)-> bool:
valid_regex=re.compile(r"^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$")
return valid_regex.match(ip)!=None
def bitsNeededPerHosts(hosts=2):
count=0
while True:
hosts_per_count=2**count-2
if (hosts_per_count>=hosts):
return (count,hosts_per_count)
count+=1
def intToBinary(number:int):
return str(bin(number)[2:])
def binaryToInt(binary:str):
return int(binary,2)
def decimalIpToBinary(ip:str|list[int]):
ip_array=ip
if type(ip_array) is str:
if not isValidIp(ip): raise CustomException("Invalid IP")
ip_array=[int(part) for part in ip_array.split('.')]
result:list[str]=[]
for part in ip_array:
binary=intToBinary(part)
result.append(("0"*(8-len(binary)))+binary)
return result
def binaryIpToDecimal(ip:str|list[str]):
ip_array=ip
if type(ip_array) is str:
if len(ip)!=32: raise CustomException("Invalid IP")
ip_array=[ip_array[i:i+8] for i in range(0,32,8)]
if len(ip_array) !=4: raise CustomException("Invalid IP")
return [binaryToInt(part) for part in ip_array]
def get_ip_range(ip,sm,hostBits):
binaryIp=''.join(decimalIpToBinary(ip))
last_ip_bits=intToBinary(binaryToInt(binaryIp)+binaryToInt("0"*(32-hostBits) + "1"*hostBits))
broadcast=binaryIpToDecimal(last_ip_bits)
first_ip=ip[:3]+[ip[3]+1]
last_ip=broadcast[:3]+[broadcast[3]-1]
print(f"\tSTART IP: {forgeIp(ip)} /{32-hostBits}")
print(f"\t\tSUBNET MASK : {forgeIp(sm)}")
print(f"\t\tBROADCAST: {forgeIp(broadcast)}")
print(f"\t\tHOSTS RANGE: {forgeIp(first_ip)} ─ {forgeIp(last_ip)}")
print(f"\t\t\tTOTAL HOSTS: {2**hostBits-2}")
nextip=binaryIpToDecimal(intToBinary(binaryToInt(''.join(decimalIpToBinary(broadcast)))+1))
return nextip
def forgeIp(ip_array: list[int|str]):
return (".".join(str(i) for i in ip_array))
def splitIp(ip:str):
if not isValidIp(ip): raise CustomException("Invalid IP")
return [int(i) for i in ip.split('.')]
if __name__=="__main__":
initialIp=input(f"INITIAL IP [e.g. 192.168.1.0] : ").strip()
try:
initialIpList=splitIp(initialIp)
except CustomException:
print('Invalid IP')
exit(1)
try:
totalSubnets=int(input("N of subnets: "))
except:
print('Invalid NUMBER')
exit(1)
hosts=[]
for i in range(totalSubnets):
try:
hosts.append(int(input(f"[{i+1}]Subnet, hosts: ")))
except:
i-=1
print('Invalid number')
nextIpList=initialIpList
hosts=sorted(hosts,reverse=True)
for index,host in enumerate(hosts):
print(f"{index+1}: {host} hosts:")
bitsNeeded=bitsNeededPerHosts(host)
host_bits=bitsNeeded[0]
net_bits=32-host_bits
subnetMaskBinary=getSubnetMask(host_bits)
subnetMaskDecimalList=binaryIpToDecimal(subnetMaskBinary)
start_ip=[nextIpList[i]&subnetMaskDecimalList[i] for i in range(4)]
nextIpList=get_ip_range(start_ip,subnetMaskDecimalList,host_bits)