-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetmikoMultipleDevices
More file actions
59 lines (53 loc) · 1.91 KB
/
NetmikoMultipleDevices
File metadata and controls
59 lines (53 loc) · 1.91 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
from netmiko import ConnectHandler
from netmiko.ssh_exception import NetMikoTimeoutException
from paramiko.ssh_exception import SSHException
from netmiko.ssh_exception import AuthenticationException
#read text file with comands to run
def commands():
with open('commands') as f:
commands_to_send = f.read().splitlines()
return commands_to_send
#read text file with list of devices to iterate through
def devices():
with open('devices') as f:
devices_list = f.read().splitlines()
return devices_list
#user input for username and password (for security use getpass)
def user_pass():
username = input("username: ")
password = input("password: ")
return username, password
#Main loop to call devices
def main():
u, p = user_pass()
for device in devices():
print('Connecting to device' + device)
ip_address_of_device = device
ios_device = {
'device_type': 'cisco_ios',
'ip': ip_address_of_device,
'username': u,
'password': p
}
#netmiko ConnectHandler and connection error handling
try:
net_connect = ConnectHandler(**ios_device)
except AuthenticationException:
print('Authentication failure: ' + ip_address_of_device)
continue
except NetMikoTimeoutException:
print('Timeout to device: ' + ip_address_of_device)
continue
except EOFError:
print('End of file while attempting device ' + ip_address_of_device)
continue
except SSHException:
print('SSH Issue. Are you sure SSH is enabled? ' + ip_address_of_device)
continue
except Exception as unknown_error:
print('Some other error: ' + str(unknown_error))
continue
output = net_connect.send_config_set(commands())
print(output)
if __name__ == "__main__":
main()