-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork_Calculator.py
125 lines (110 loc) · 4.85 KB
/
Network_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
import ifaddr
import json
import ipaddress
import sys
ComputeNetwork = {}
def Main(argv):
StartOption = True
while StartOption == True:
print("Choose, what want you to do?")
print("\n1. Compute network parametrs of local IP "
"\n2. Entry and compute network parametrs"
"\n3. Exit ")
strChoice = input("Your decision: ")
if strChoice.isdigit() == False:
print("\nPlease choose 1, 2 or 3")
continue
else:
UserChoice = int(strChoice)
if UserChoice not in [1, 2, 3]:
print("\nPlease choose 1, 2 or 3")
else:
if UserChoice == 3:
exit()
else:
if UserChoice == 1:
ComputeNetwork["NetworkAddress"] = GetLocalIP()
else:
if len(argv) == 2:
SettedUserIP = argv[1]
else:
SettedUserIP = input("Entry your IP: ")
ComputeNetwork["NetworkAddress"] = GetUserIP(SettedUserIP)
ComputeNetwork["ClassofNetwork"] = DefinitionClassofNetwork(ComputeNetwork["NetworkAddress"])
ComputeNetwork["SubnetMask"] = ComputeSubnetMask(ComputeNetwork["NetworkAddress"])
ComputeNetwork["BinarySubnetMask"] = SetBinaryAddresses(ComputeNetwork["SubnetMask"])
ComputeNetwork["BroadcastAddress"] = SetBroadcastAddress(ComputeNetwork["NetworkAddress"])
ComputeNetwork["BinaryBroadcastAddress"] = SetBinaryAddresses(ComputeNetwork["BroadcastAddress"])
ComputeNetwork["FirstHostsAddress"] = SetFirstHostAddress(ComputeNetwork["NetworkAddress"])
ComputeNetwork["BinaryHostsAddress"] = SetBinaryAddresses(ComputeNetwork["FirstHostsAddress"])
ComputeNetwork["MaxCountofHost"] = SetCountofHosts(ComputeNetwork["NetworkAddress"])
ComputeNetwork["BinaryMaxCountofHost"] = SetBinaryCountofHosts(ComputeNetwork["MaxCountofHost"])
print(ComputeNetwork)
SaveToJSONFile(ComputeNetwork)
print("Your data are saved in JSON file")
return
def GetLocalIP():
adapters = ifaddr.get_adapters()
localIP = adapters[6].ips[1].ip + '/' + str(adapters[6].ips[1].network_prefix)
return localIP
def GetUserIP(UserIP: str):
IPtoCheck = UserIP
while True:
try:
ipaddress.IPv4Network(IPtoCheck, False)
except ValueError:
IPtoCheck = input("Entry correct IP network address: ")
continue
break
return IPtoCheck
def DefinitionClassofNetwork(IPtoCheck: str):
TableofIPAdressWithSubnetMask = IPtoCheck.split("/")
TableofIPAdress = TableofIPAdressWithSubnetMask[0].split(".")
FirstPartofAddres = int(TableofIPAdress[0])
if FirstPartofAddres >= 0 and FirstPartofAddres <= 127:
ClassofNetwork = "A"
elif FirstPartofAddres >= 128 and FirstPartofAddres <= 191:
ClassofNetwork = "B"
elif FirstPartofAddres >= 192 and FirstPartofAddres <= 223:
ClassofNetwork = "C"
elif FirstPartofAddres >= 224 and FirstPartofAddres <= 239:
ClassofNetwork = "D"
else:
ClassofNetwork = "E"
return ClassofNetwork
def ComputeSubnetMask(NetworkIP: str):
interface = ipaddress.IPv4Interface(NetworkIP)
NetworkIPWithMask = interface.with_netmask
TableofNetwork = NetworkIPWithMask.split("/")
SubnetMask = TableofNetwork[1]
return SubnetMask
def SetBinaryAddresses(AnyAddress: str):
TableofAnyAddress = AnyAddress.split(".")
TableofAnyAddress = list(map(int, TableofAnyAddress))
BinaryAddressTmp = [bin(TableofAnyAddress[i])[2:] for i in range(0, 4)]
while '0' in BinaryAddressTmp:
BinaryAddressTmp[BinaryAddressTmp.index("0")] = "00000000"
while '1' in BinaryAddressTmp:
BinaryAddressTmp[BinaryAddressTmp.index("1")] = "00000001"
BinaryAddress = ".".join(BinaryAddressTmp)
return BinaryAddress
def SetBroadcastAddress(NetworkAddress: str):
Network = ipaddress.IPv4Network(NetworkAddress, False)
BroadcastAddress = str(Network.broadcast_address)
return BroadcastAddress
def SetFirstHostAddress(NetworkAddress: str):
TableofHHosts = list(ipaddress.IPv4Network(NetworkAddress, False).hosts())
return str(TableofHHosts[0])
def SetCountofHosts(NetworkAddress: str):
TableofNetwork = NetworkAddress.split('/')
ShortSubnetMask = int(TableofNetwork[1])
CountofHosts = pow(2, 32 - ShortSubnetMask)
return CountofHosts
def SetBinaryCountofHosts(CountofHosts: int):
return bin(CountofHosts)[2:]
def SaveToJSONFile(NetworkInfo: dict):
with open('myNetwork.json', 'w') as outfile:
json.dump(NetworkInfo, outfile)
return
##Main function
Main(sys.argv)