#!/usr/bin/env python from getpass import getpass from netmiko import ConnectHandler from netmiko.ssh_exception import NetMikoTimeoutException from paramiko.ssh_exception import SSHException from netmiko.ssh_exception import AuthenticationException username = input('Enter your SSH username: ') password = getpass() devicelist = ['10.0.1.202', '10.0.1.203', '10.0.1.204', '10.0.1.205', '10.0.1.206' ] for devices in devicelist: 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 } 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 #---- Use CLI command to get configuration data from device print ('---- Getting configuration from device') config_data = net_connect.send_command('show run') #---- Write out configuration information to file config_filename = 'config-' + devices # Important - create unique configuration file name print ('---- Writing configuration: ', config_filename) with open( config_filename, 'w' ) as config_out: config_out.write( config_data ) net_connect.disconnect()