Advertisement
mikulc

json

Jun 25th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 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. import json
  7.  
  8. with open('external.json') as file:
  9. devices_list = json.load(file)
  10.  
  11.  
  12. file.close()
  13.  
  14. netmiko_exceptions = (netmiko.ssh_exception.NetMikoAuthenticationException,
  15. netmiko.ssh_exception.NetMikoTimeoutException
  16. )
  17.  
  18.  
  19. for device in devices_list:
  20.  
  21. try:
  22. connection = ConnectHandler(**device)
  23.  
  24. connection.enable()
  25.  
  26. if device['device_type'] == 'cisco_nxos':
  27. hostname = connection.send_command('show hostname')
  28. licenseid = connection.send_command('show license host-id')
  29. else:
  30. hostname = connection.send_command('show run | i host')
  31. hostname = hostname.replace('hostname ', '')
  32. licenseid = 'License hostid: no license '
  33.  
  34. connection.disconnect()
  35.  
  36. except netmiko_exceptions as ex:
  37. print('Error occur with [{}]'.format(device["ip"]))
  38. print('The problem touches: {}'.format(ex))
  39. else:
  40. # print('else')
  41. print('Host [{}] {} {}'.format(device['ip'], hostname, licenseid))
  42. # finally:
  43. # print('final')
  44.  
  45.  
  46.  
  47. external.json
  48. [
  49. {
  50. "ip":"10.1.100.90",
  51. "device_type":"cisco_nxos",
  52. "username":"admin",
  53. "password":"Cisco123$"
  54. },
  55. {
  56. "ip":"10.1.100.92",
  57. "device_type":"cisco_nxos",
  58. "username":"admin",
  59. "password":"Cisco123$q"
  60. },
  61. {
  62. "ip":"10.1.100.100",
  63. "device_type":"cisco_ios",
  64. "username":"admin",
  65. "password":"Cisco123$"
  66. }
  67. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement