Advertisement
mikulc

try_except

Jun 25th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import sys
  3. from netaddr import IPAddress
  4. from netmiko import ConnectHandler
  5. import netmiko
  6.  
  7. N9K1 = {"ip":"10.1.100.90", "device_type":"cisco_nxos","username":"admin","password":"Cisco123$"}
  8. N9K2 = {"ip":"10.1.100.92", "device_type":"cisco_nxos","username":"admin","password":"Cisco123$q"}
  9. core = {"ip":"10.1.100.100", "device_type":"cisco_ios","username":"admin","password":"Cisco123$"}
  10.  
  11. devices_list = [N9K1, N9K2, core]
  12.  
  13. netmiko_exceptions = (netmiko.ssh_exception.NetMikoAuthenticationException,
  14. netmiko.ssh_exception.NetMikoTimeoutException
  15. )
  16.  
  17.  
  18. for device in devices_list:
  19.  
  20. try:
  21. connection = ConnectHandler(**device)
  22.  
  23. connection.enable()
  24.  
  25. if device['device_type'] == 'cisco_nxos':
  26. hostname = connection.send_command('show hostname')
  27. licenseid = connection.send_command('show license host-id')
  28. else:
  29. hostname = connection.send_command('show run | i host')
  30. hostname = hostname.replace('hostname ', '')
  31. licenseid = 'License hostid: no license '
  32.  
  33. connection.disconnect()
  34.  
  35. except netmiko_exceptions as ex:
  36. print('Error occur with [{}]'.format(device["ip"]))
  37. print('The problem touches: {}'.format(ex))
  38. else:
  39. # print('else')
  40. print('Host [{}] {} {}'.format(device['ip'], hostname, licenseid))
  41. # finally:
  42. # print('final')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement