-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path7-netmiko-final.py
55 lines (50 loc) · 1.97 KB
/
7-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
51
52
53
54
55
#!/usr/bin/env python
import os
from getpass import getpass
from netmiko import ConnectHandler
from netmiko import NetmikoAuthenticationException
from netmiko import NetmikoTimeoutException
from paramiko.ssh_exception import SSHException
# Check environment variable for login credentials, if that fails, use getpass().
username = os.getenv("NETMIKO_USERNAME") if os.getenv("NETMIKO_USERNAME") else input('Enter username: ')
password = os.getenv("NETMIKO_PASSWORD") if os.getenv("NETMIKO_PASSWORD") else getpass()
# Sending device ip's stored in a file
with open('device_list') as f:
device_list = f.read().splitlines()
# Sending list of show commands stored in a file
with open('show_command') as f:
show_commands = f.readlines()
# 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 = ConnectHandler(**ios_device)
except (NetmikoAuthenticationException):
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
# Iterate through command list and print the output
net_connect = ConnectHandler(**ios_device)
for command in show_commands:
output = net_connect.send_command(command)
print (command + output + '\n')
net_connect.disconnect()