Check Hostname of Device and Then Apply Specific Configs (Simple and Easy) using:Python

Описание к видео Check Hostname of Device and Then Apply Specific Configs (Simple and Easy) using:Python

lab@admin:~$ cat Hostname-check-and-apply-Configs-4
from netmiko import Netmiko
from getpass import getpass
from netmiko import NetMikoTimeoutException
from netmiko import NetMikoAuthenticationException

username = input('Enter your SSH username: ')
password = getpass()

Read command sets from files
with open('PTX-Commands') as f:
PTX = f.read().splitlines()

with open('MX-Commands') as f:
MX = f.read().splitlines()

with open('PTX1') as f:
PTX1 = f.read().splitlines()

with open('Additional-Commands') as f:
Additional_Commands = f.read().splitlines()

Read device list from file
with open('Device-file') as f:
devices = f.read().splitlines()

print(devices)

for router in devices:
print('Connecting to device ====================" ' + router)
ip_address_of_device = router
Junos_device = {
'device_type': 'juniper_junos',
'ip': ip_address_of_device,
'username': username,
'password': password
}

try:
net_connect = Netmiko(**Junos_device)
except NetMikoAuthenticationException:
print('===== We have an authentication failure: ==========' + ip_address_of_device)
continue
except NetMikoTimeoutException:
print('========= We have a timeout to device: ============' + ip_address_of_device)
continue
except EOFError:
print('End of file while attempting device ' + ip_address_of_device)
continue
except Exception as unknown_error:
print('Some other error: ' + str(unknown_error))
continue

try:
Check software versions
output_hostname = net_connect.send_command('show version')

Match against known hostnames
found_hostname = None
for hostname in ['R12-MX960-RE0', 'R12-PTX10k-re0', 'PTX-36MR', 'R12-Additional']:
if hostname in output_hostname:
found_hostname = hostname
print('Hostname found: ' + hostname)
break

if found_hostname == 'R12-MX960-RE0':
print('Running ' + found_hostname + ' commands')
output = net_connect.send_config_set(MX, exit_config_mode=False)
output += net_connect.commit(and_quit=True)
print(output)
elif found_hostname == 'R12-PTX10k-re0':
print('Running ' + found_hostname + ' commands')
output = net_connect.send_config_set(PTX, exit_config_mode=False)
output += net_connect.commit(and_quit=True)
print(output)
elif found_hostname == 'PTX-36MR':
print('Running ' + found_hostname + ' commands')
output = net_connect.send_config_set(PTX1, exit_config_mode=False)
output += net_connect.commit(and_quit=True)
print(output)
elif found_hostname == 'R12-Additional':
print('Running ' + found_hostname + ' commands')
output = net_connect.send_config_set(Additional_Commands, exit_config_mode=False)
output += net_connect.commit(and_quit=True)
print(output)
else:
print('Hostname not recognized or not found in output.')

except Exception as e:
print('An error occurred while processing the device: ' + str(e))

finally:
net_connect.disconnect()

Комментарии

Информация по комментариям в разработке