Advertisement
Guest User

Untitled

a guest
Mar 11th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. #!/usr/bin/python env
  2. '''
  3. This is just a demo script I created to get started with using hte NXAPI and pycsco module.
  4. '''
  5. import getpass
  6.  
  7. import json
  8.  
  9. import xmltodict
  10.  
  11. from pycsco.nxos.device import Device
  12.  
  13. from prettytable import PrettyTable
  14.  
  15. print chr(0x1b) + '[2J'
  16. # clear the screen
  17.  
  18. print "Enter your credentials:"
  19.  
  20. username = raw_input('Username: ')
  21. # prompt for the username to use
  22.  
  23. password = getpass.getpass("Password: ")
  24. # prompt for the password to use
  25.  
  26. device_ip = raw_input("Switch Hostname or IP: ")
  27. # prompt for the hostname or IP of the switch
  28.  
  29. nxapi_connect = Device(ip=device_ip, username=username, password=password)
  30. # setup an object to include ip and creds for the pyscsco
  31.  
  32. loop = 1
  33.  
  34. while loop == 1:
  35.  
  36. '''
  37. Ask the user which function they want to launch.
  38. '''
  39. print chr(0x1b) + '[2J'
  40. # clear the screen
  41. print "\
  42. Make a selection:\n \
  43. [1] Device Info\n \
  44. [2] IP Arp Table\n \
  45. [3] Change Device\n \
  46. [4] Exit\n \
  47. "
  48. selection = raw_input('Selection [1]: ') or "1"
  49. selection = int(selection)
  50.  
  51.  
  52. if selection == 1:
  53. get_sh_ver = nxapi_connect.show('show version')
  54. # this returns a tuple of 2 xml objects. the first is headers, etc. and the second is the data we want
  55.  
  56. sh_ver_dict = xmltodict.parse(get_sh_ver[1])
  57. # converting the second element to a python dictionary
  58.  
  59. sh_ver_simple = sh_ver_dict['ins_api']['outputs']['output']['body']
  60. # trim down headers and wrapers
  61.  
  62. hostname = sh_ver_simple['host_name']
  63. # creat object of parsed data for the hostname of device
  64.  
  65. version = sh_ver_simple['rr_sys_ver']
  66. # creat object of parsed data for the version of device
  67.  
  68. chassis_type = sh_ver_simple['chassis_id']
  69. # creat object of parsed data for the model of device
  70.  
  71. '''
  72. Print device informtion
  73. '''
  74. print "Hostname: {}" .format(hostname)
  75. print "Version: {}" .format(version)
  76. print "Model: {}" .format(chassis_type)
  77.  
  78. raw_input('Press any key to continue...')
  79.  
  80. elif selection == 2:
  81. get_sh_arp = nxapi_connect.show('show ip arp vrf all')
  82. # TBD
  83.  
  84. sh_arp_dict = xmltodict.parse(get_sh_arp[1])
  85. # TBD
  86.  
  87. sh_arp_simple = sh_arp_dict['ins_api']['outputs']['output']['body']['TABLE_vrf']['ROW_vrf']
  88.  
  89. arp_entries = sh_arp_simple['cnt-total']
  90. # Total number of arp entries
  91.  
  92. t = PrettyTable(['Interface', 'IP', 'Age', 'MAC'])
  93. # Create a disable table to add data to.
  94.  
  95. for entry in sh_arp_simple['TABLE_adj']['ROW_adj']:
  96.  
  97. row = [str(entry['intf-out']), str(entry['ip-addr-out']), str(entry['time-stamp']), str(entry['mac'])]
  98. # Make a list from the data in each dictrionary entry
  99.  
  100. t.add_row(row)
  101. # add a row to the PrettyTable
  102.  
  103. '''
  104. Print device information
  105. '''
  106. print t
  107. print "{} arp entries retrieved." .format(arp_entries)
  108.  
  109. raw_input('Press any key to continue...')
  110.  
  111. elif selection == 3:
  112.  
  113. device_ip = raw_input("Switch Hostname or IP: ")
  114. # prompt for the hostname or IP of the switch
  115.  
  116. elif selection == 4:
  117.  
  118. loop = 0
  119.  
  120. print "Exiting..."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement