Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import requests
  4. import json
  5. from requests.auth import HTTPBasicAuth
  6.  
  7. if __name__ == "__main__":
  8.  
  9. auth = HTTPBasicAuth('cisco', 'cisco')
  10. headers = {
  11. 'Content-Type': 'application/json'
  12. }
  13.  
  14. payload = {
  15. "ins_api": {
  16. "version": "1.0",
  17. "type": "cli_show",
  18. "chunk": "0",
  19. "sid": "1",
  20. "input": "show version",
  21. "output_format": "json"
  22. }
  23. }
  24. url = 'http://nxosv/ins'
  25.  
  26. response = requests.post(url, data=json.dumps(payload), headers=headers, auth=auth)
  27.  
  28. print 'Status Code: ' + str(response.status_code)
  29. rx_object = json.loads(response.text)
  30. print json.dumps(rx_object, indent=4)
  31. print 'OS: ', rx_object['ins_api']['outputs']['output']['body']['sys_ver_str']
  32.  
  33.  
  34.  
  35.  
  36. #!/usr/bin/env python
  37.  
  38. import requests
  39. import json
  40. from requests.auth import HTTPBasicAuth
  41.  
  42. if __name__ == "__main__":
  43.  
  44. auth = HTTPBasicAuth('cisco', 'cisco')
  45. headers = {
  46. 'Content-Type': 'application/json'
  47. }
  48.  
  49. payload = {
  50. "ins_api": {
  51. "version": "1.0",
  52. "type": "cli_show",
  53. "chunk": "0",
  54. "sid": "1",
  55. "input": "show interface mgmt0",
  56. "output_format": "json"
  57. }
  58. }
  59. url = 'http://nxosv/ins'
  60.  
  61. response = requests.post(url, data=json.dumps(payload), headers=headers, auth=auth)
  62. rx_object = json.loads(response.text)
  63.  
  64. interface = rx_object['ins_api']['outputs']['output']['body']['TABLE_interface']['ROW_interface']
  65.  
  66. print "Management Interface Information:"
  67. print "IP Address: " + interface['eth_ip_addr'] + '/' + str(interface['eth_ip_mask'])
  68. print "Speed: ", interface['eth_speed']
  69. print "State: ", interface['state']
  70. print "MTU: ", interface['eth_mtu']
  71.  
  72.  
  73.  
  74. #!/usr/bin/env python
  75.  
  76. import requests
  77. import json
  78. from requests.auth import HTTPBasicAuth
  79.  
  80. if __name__ == "__main__":
  81.  
  82. auth = HTTPBasicAuth('cisco', 'cisco')
  83. headers = {
  84. 'Content-Type': 'application/json'
  85. }
  86.  
  87. payload = {
  88. "ins_api": {
  89. "version": "1.0",
  90. "type": "cli_conf",
  91. "chunk": "0",
  92. "sid": "1",
  93. "input": "config t ;vlan 150 ;exit ;interface eth2/5 ;switchport; switchport access vlan 150",
  94. "output_format": "json"
  95. }
  96. }
  97. url = 'http://nxosv/ins'
  98.  
  99. response = requests.post(url, data=json.dumps(payload), headers=headers, auth=auth)
  100. rx_object = json.loads(response.text)
  101.  
  102. print json.dumps(rx_object, indent=4)
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. #!/usr/bin/env python
  114.  
  115. from ncclient import manager
  116.  
  117. if __name__ == '__main__':
  118.  
  119. with manager.connect(host='nxosv', port=22, username='cisco', password='cisco',
  120. hostkey_verify=False, device_params={'name': 'nexus'},
  121. allow_agent=False, look_for_keys=False) as device:
  122.  
  123. get_filter = """
  124. <show>
  125. <hostname>
  126. </hostname>
  127. </show>
  128. """
  129. nc_get_reply = device.get(('subtree', get_filter))
  130. print 'Response as XML String: '
  131. print nc_get_reply.xml
  132.  
  133. ns_map = {'mod': 'http://www.cisco.com/nxos:1.0:vdc_mgr'}
  134. xml_rsp = nc_get_reply.data_ele.find('.//mod:hostname', ns_map)
  135. value = xml_rsp.text
  136.  
  137. print '================================='
  138. print 'Hostname: ', value
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. #!/usr/bin/env python
  151.  
  152. from ncclient import manager
  153.  
  154. if __name__ == '__main__':
  155.  
  156. with manager.connect(host='nxosv', port=22, username='cisco', password='cisco',
  157. hostkey_verify=False, device_params={'name': 'nexus'},
  158. allow_agent=False, look_for_keys=False) as device:
  159.  
  160. get_filter = """
  161. <show>
  162. <version>
  163. </version>
  164. </show>
  165. """
  166. nc_get_reply = device.get(('subtree', get_filter))
  167. print 'Response as XML String: '
  168. print nc_get_reply.xml
  169.  
  170. ns_map = {'mod': 'http://www.cisco.com/nxos:1.0:sysmgrcli'}
  171. xml_rsp_cid = nc_get_reply.data_ele.find('.//mod:chassis_id', ns_map)
  172. cid_value = xml_rsp_cid.text
  173. xml_rsp_sw = nc_get_reply.data_ele.find('.//mod:sys_ver_str', ns_map)
  174. sw_value = xml_rsp_sw.text
  175.  
  176. print '================================='
  177. print 'Chassis ID: ', cid_value
  178. print 'Software Version: ', sw_value
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189. #!/usr/bin/env python
  190.  
  191. from ncclient import manager
  192.  
  193. if __name__ == '__main__':
  194.  
  195. with manager.connect(host='nxosv', port=22, username='cisco', password='cisco',
  196. hostkey_verify=False, device_params={'name': 'nexus'},
  197. allow_agent=False, look_for_keys=False) as device:
  198.  
  199. commands = ['config t', 'interface Ethernet2/6', 'switchport', 'description Configured by Python ncclient']
  200. nc_config_reply = device.exec_command(commands)
  201. print nc_config_reply.xml
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement