Advertisement
mikulc

login_credential

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