Repository navigation
Expand file tree
/
Copy pathnetworkFileRW.py
More file actions
151 lines (104 loc) · 3.97 KB
/
Copy pathnetworkFileRW.py
File metadata and controls
151 lines (104 loc) · 3.97 KB
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
#!/usr/bin/env python3
#networkFileRW.py
#Alexander Townsend
#2023-08-18
#Update routers and switches;
#read equipment from a file, write updates & errors to file
##---->>>> Use a try/except clause to import the JSON module
##---->>>> Create file constants for the file names; file constants can be reused
## There are 2 files to read this program: equip_r.txt and equip_s.txt
## There are 2 files to write in this program: updated.txt and errors.txt
#prompt constants
UPDATE = "\nWhich device would you like to update "
QUIT = "(enter x to quit)? "
NEW_IP = "What is the new IP address (111.111.111.111) "
SORRY = "Sorry, that is not a valid IP address\n"
#function to get valid device
def getValidDevice(routers, switches):
validDevice = False
while not validDevice:
#prompt for device to update
device = input(UPDATE + QUIT).lower()
if device in routers.keys():
return device
elif device in switches.keys():
return device
elif device == 'x':
return device
else:
print("That device is not in the network inventory.")
#function to get valid IP address
def getValidIP(invalidIPCount, invalidIPAddresses):
validIP = False
while not validIP:
ipAddress = input(NEW_IP)
octets = ipAddress.split('.')
#print("octets", octets)
for byte in octets:
byte = int(byte)
if byte < 0 or byte > 255:
invalidIPCount += 1
invalidIPAddresses.append(ipAddress)
print(SORRY)
break
else:
#validIP = True
return ipAddress, invalidIPCount
#don't need to return invalidIPAddresses list - it's an object
def main():
##---->>>> open files here
#dictionaries
##---->>>> read the routers and addresses into the router dictionary
routers = {}
##---->>>> read the switches and addresses into the switches dictionary
switches = {}
#the updated dictionary holds the device name and new ip address
updated = {}
#list of bad addresses entered by the user
invalidIPAddresses = []
#accumulator variables
devicesUpdatedCount = 0
invalidIPCount = 0
#flags and sentinels
quitNow = False
validIP = False
print("Network Equipment Inventory\n")
print("\tequipment name\tIP address")
for router, ipa in routers.items():
print("\t" + router + "\t\t" + ipa)
for switch, ipa in switches.items():
print("\t" + switch + "\t\t" + ipa)
while not quitNow:
#function call to get valid device
device = getValidDevice(routers, switches)
if device == 'x':
quitNow = True
break
#function call to get valid IP address
#python lets you return two or more values at one time
ipAddress, invalidIPCount = getValidIP(invalidIPCount, invalidIPAddresses)
#update device
if 'r' in device:
#modify the value associated with the key
routers[device] = ipAddress
#print("routers", routers)
else:
switches[device] = ipAddress
devicesUpdatedCount += 1
#add the device and ipAddress to the dictionary
updated[device] = ipAddress
print(device, "was updated; the new IP address is", ipAddress)
#loop back to the beginning
#user finished updating devices
print("\nSummary:")
print()
print("Number of devices updated:", devicesUpdatedCount)
##---->>>> write the updated equipment dictionary to a file
print("Updated equipment written to file 'updated.txt'")
print()
print("\nNumber of invalid addresses attempted:", invalidIPCount)
##---->>>> write the list of invalid addresses to a file
print("List of invalid addresses written to file 'errors.txt'")
#top-level scope check
if __name__ == "__main__":
main()