Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #config multiple devices switch with a commands_file
- #!/usr/bin/env python
- from getpass import getpass
- from netmiko import ConnectHandler
- from netmiko.ssh_exception import NetMikoTimeoutException
- from netmiko.ssh_exception import SSHException
- from netmiko.ssh_exception import AuthenticationException
- username = raw_input('Enter your SSH username: ')
- password = getpass()
- #command file to be sent
- with open('./commands_folder/commands_icx_file') as f:
- commands_list_hp = f.read().splitlines()
- with open('./commands_folder/commands_hp_file') as f:
- commands_list_icx = f.read().splitlines()
- with open('./commands_folder/commands_ios_file') as f:
- commands_list_ios = f.read().splitlines()
- with open('./commands_folder/commands_file') as f:
- commands_to_send = f.read().splitlines()
- #switch ip list to connect to
- with open('./devices_ip/devices_file') as f:
- devices_list = f.read().splitlines()
- #library of devices
- for devices in devices_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
- }
- 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 attemtping device" + ip_address_of_device
- continue
- except (SSHException):
- print 'SSH Issue. Are you sure SSH is enable? ' + ip_address_of_device
- continue
- except Exception as unknow_error:
- print 'Some other error: ' + unknow_error
- continue
- # Types of devices
- list_versions = ['ICX7150-',
- 'WS-C3',
- 'HP'
- ]
- # Checking the software/type version
- for software_ver in list_versions:
- print 'Checking for ' + software_ver
- output_version = net_connect.send_command('show version')
- int_version = 0 # Reset integer value
- int_version = output_version.find(software_ver) # checking software version / models type
- if int_version > 0:
- print 'Software version found: ' + software_ver
- break
- else:
- print 'Did not find ' + software_ver
- if software_ver == 'ICX7150-48' :
- print 'Running ' + software_ver + ' commands'
- output = net_connect.send_config_set(commands_list_icx)
- elif software_ver == 'WS-C3':
- print 'Running ' + software_ver + ' commands'
- output = net_connect.send_config_set(commands_list_ios)
- elif software_ver == 'HP':
- print 'Running ' + software_ver + ' commands'
- output = net_connect.send_config_set(commands_list_hp)
- print output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement