-
Notifications
You must be signed in to change notification settings - Fork 0
/
subnet.py
37 lines (34 loc) · 1.08 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
def subnet(ip):
def specialOcted(num):
if num % 8 == 0:
return 0
elif num % 8 == 1:
return 128
elif num % 8 == 2:
return 192
elif num % 8 == 3:
return 224
elif num % 8 == 4:
return 240
elif num % 8 == 5:
return 248
elif num % 8 == 6:
return 252
else:
return 254
Subnet = int(ip.split(sep="/")[1])
remainder = Subnet // 8
if remainder == 0:
return str(specialOcted(Subnet))+".0.0.0"
elif remainder == 1:
return "255."+str(specialOcted(Subnet))+".0.0"
elif remainder == 2:
return "255.255."+str(specialOcted(Subnet))+".0"
elif remainder == 3:
return "255.255.255."+str(specialOcted(Subnet))
elif remainder == 4 and specialOcted(Subnet) == 0:
return "255.255.255.255"
elif (remainder == 4 and specialOcted(Subnet) != 0) or 4 < remainder or remainder < 0:
return "Invalid Subnet!"
test = subnet("10.11.12.13/23")
print(test)