-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNapalmMultipleDevices
88 lines (69 loc) · 2.31 KB
/
NapalmMultipleDevices
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
from napalm import get_network_driver
from netmiko.ssh_exception import NetMikoTimeoutException
from paramiko.ssh_exception import SSHException
from netmiko.ssh_exception import AuthenticationException
def devices():
with open('devices') as f:
devices_list = f.read().splitlines()
return devices_list
def user_pass():
username = input("enter your username: ")
password = input("enter your password: ")
return username, password
def commands():
with open('commands') as f:
commands_to_send = f.read()
return commands_to_send
def main():
print("Devices to be configured: ")
print(devices())
print("*" * 100)
print("Currently loaded configuration:\n")
print(commands())
print("\n")
u, p = user_pass()
for device in devices():
try:
driver = get_network_driver('ios')
ios = driver(device, u, p)
ios.open()
except (AuthenticationException):
print('Authentication failure: ' + device)
continue
except (NetMikoTimeoutException):
print('Timeout to device: ' + device)
continue
except (EOFError):
print('End of file while attempting device ' + device)
continue
except (SSHException):
print('SSH Issue. Are you sure SSH is enabled? ' + device)
continue
except Exception as unknown_error:
print('Unspecified error: ' + str(unknown_error))
continue
ios_output = ios.get_facts()
os_version = ios_output.get("os_version", "")
hostname = ios_output.get("hostname", "")
fqdn = ios_output.get("fqdn", "")
print("*" * 100)
print(f"""Accessing
{hostname}
{device}
{fqdn}
{os_version}
""")
ios.load_merge_candidate(filename='commands')
diffs = ios.compare_config()
if len(diffs) > 0:
print(diffs)
print("\n")
print(f"you are conected to: ", {hostname}, {device})
ios.commit_config()
ios.close()
else:
print("No change is required")
ios.discard_config()
ios.close()
if __name__ == "__main__":
main()