SHARE
TWEET

Untitled

a guest Sep 11th, 2019 157 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. #!/usr/bin/env python
  35.  
  36. import requests
  37. import json
  38. from requests.auth import HTTPBasicAuth
  39.  
  40. if __name__ == "__main__":
  41.  
  42.     auth = HTTPBasicAuth('cisco', 'cisco')
  43.     headers = {
  44.         'Content-Type': 'application/json'
  45.     }
  46.  
  47.     payload = {
  48.         "ins_api": {
  49.             "version": "1.0",
  50.             "type": "cli_show",
  51.             "chunk": "0",
  52.             "sid": "1",
  53.             "input": "show interface mgmt0",
  54.             "output_format": "json"
  55.         }
  56.     }
  57.     url = 'http://nxosv/ins'
  58.  
  59.     response = requests.post(url, data=json.dumps(payload), headers=headers, auth=auth)
  60.     rx_object = json.loads(response.text)
  61.  
  62.     interface = rx_object['ins_api']['outputs']['output']['body']['TABLE_interface']['ROW_interface']
  63.  
  64.     print "Management Interface Information:"
  65.     print "IP Address:  " +  interface['eth_ip_addr'] + '/' + str(interface['eth_ip_mask'])
  66.     print "Speed:      ", interface['eth_speed']
  67.     print "State:      ", interface['state']
  68.     print "MTU:        ", interface['eth_mtu']
  69.  
  70. ================
  71.  
  72. #!/usr/bin/env python
  73.  
  74. import request
  75. import json
  76. from requests.auth import HTTPBasicAuth
  77.  
  78. if __name__ == "__main__":
  79.  
  80.     auth = HTTPBasicAuth('cisco', 'cisco')
  81.     headers = {
  82.         'Content-Type': 'application/json'
  83.     }
  84.  
  85.     commands = ['config t', 'vlan 150', 'exit', 'interface Eth2/5', 'switchport', 'switchport access vlan 150']
  86.  
  87.     commands = ' ; '.join(commands)
  88.  
  89.  
  90.     # you could have also manually created a semi-colon delimited string.  
  91.     # testing this in NX-API sandbox will show you the proper format required
  92.  
  93.  
  94.     payload = {
  95.         "ins_api": {
  96.             "version": "1.0",
  97.             "type": "cli_conf",
  98.             "chunk": "0",
  99.             "sid": "1",
  100.             "input": commands,
  101.             "output_format": "json"
  102.         }
  103.     }
  104.     url = 'http://nxosv/ins'
  105.  
  106.     response = requests.post(url, data=json.dumps(payload), headers=headers, auth=auth)
  107.     rx_object = json.loads(response.text)
  108.  
  109.     print json.dumps(rx_object, indent=4)
  110.    
  111. ==============
  112. #!/usr/bin/env python
  113.  
  114. from ncclient import manager
  115.  
  116. if __name__ == '__main__':
  117.  
  118.     with manager.connect(host='nxosv', port=22, username='cisco', password='cisco',
  119.                          hostkey_verify=False, device_params={'name': 'nexus'},
  120.                          allow_agent=False, look_for_keys=False) as device:
  121.  
  122.         get_filter = """
  123.                       <show>
  124.                         <version>
  125.                         </version>
  126.                       </show>
  127.                       """
  128.         nc_get_reply = device.get(('subtree', get_filter))
  129.         print 'Response as XML String: '
  130.         print nc_get_reply.xml
  131.  
  132.         ns_map = {'mod': 'http://www.cisco.com/nxos:1.0:sysmgrcli'}
  133.         xml_rsp_cid = nc_get_reply.data_ele.find('.//mod:chassis_id', ns_map)
  134.         cid_value = xml_rsp_cid.text
  135.         xml_rsp_sw = nc_get_reply.data_ele.find('.//mod:sys_ver_str', ns_map)
  136.         sw_value = xml_rsp_sw.text
  137.  
  138.         print '================================='
  139.         print 'Chassis ID: ', cid_value
  140.         print 'Software Versi
  141.  
  142. ==============
  143.  
  144. python nc_config_interface.py
  145.  
  146. #!/usr/bin/env python
  147.  
  148. from ncclient import manager
  149.  
  150. if __name__ == '__main__':
  151.  
  152.     with manager.connect(host='nxosv', port=22, username='cisco', password='cisco',
  153.                          hostkey_verify=False, device_params={'name': 'nexus'},
  154.                          allow_agent=False, look_for_keys=False) as device:
  155.  
  156.         commands = ['config t', 'interface Ethernet2/6', 'switchport', 'description Configured by Python ncclient']
  157.         nc_config_reply = device.exec_command(commands)
  158.         print nc_config_reply.xml
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top