This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
5-netmiko-final.py
50 lines (45 loc) · 1.63 KB
/
5-netmiko-final.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
#!/usr/bin/env python
from getpass import getpass
from netmiko import Netmiko
from netmiko.ssh_exception import NetMikoTimeoutException
from paramiko.ssh_exception import SSHException
from netmiko.ssh_exception import AuthenticationException
# Collect login credentials
username = input('Enter your SSH username: ')
password = getpass('Enter your password: ')
# Sending device ip's stored in a file
with open('device_list') as f:
device_list = f.read().splitlines()
# Iterate through device list and configure the devices
for devices in device_list:
print ('Connecting to device ' + devices)
ip_address_of_device = devices
ios_device = {
'device_type': 'cisco_ios',
'ip': ip_address_of_device,
'username': username,
'password': password
}
# Error handling parameters
try:
net_connect = Netmiko(**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
# Configure the device and save config
output = net_connect.send_config_from_file('config_commands')
output += net_connect.save_config()
net_connect.disconnect()
print (output)