Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import requests
- import json
- from requests.auth import HTTPBasicAuth
- if __name__ == "__main__":
- auth = HTTPBasicAuth('cisco', 'cisco')
- headers = {
- 'Content-Type': 'application/json'
- }
- payload = {
- "ins_api": {
- "version": "1.0",
- "type": "cli_show",
- "chunk": "0",
- "sid": "1",
- "input": "show version",
- "output_format": "json"
- }
- }
- url = 'http://nxosv/ins'
- response = requests.post(url, data=json.dumps(payload), headers=headers, auth=auth)
- print 'Status Code: ' + str(response.status_code)
- rx_object = json.loads(response.text)
- print json.dumps(rx_object, indent=4)
- print 'OS: ', rx_object['ins_api']['outputs']['output']['body']['sys_ver_str']
- =================
- #!/usr/bin/env python
- import requests
- import json
- from requests.auth import HTTPBasicAuth
- if __name__ == "__main__":
- auth = HTTPBasicAuth('cisco', 'cisco')
- headers = {
- 'Content-Type': 'application/json'
- }
- payload = {
- "ins_api": {
- "version": "1.0",
- "type": "cli_show",
- "chunk": "0",
- "sid": "1",
- "input": "show interface mgmt0",
- "output_format": "json"
- }
- }
- url = 'http://nxosv/ins'
- response = requests.post(url, data=json.dumps(payload), headers=headers, auth=auth)
- rx_object = json.loads(response.text)
- interface = rx_object['ins_api']['outputs']['output']['body']['TABLE_interface']['ROW_interface']
- print "Management Interface Information:"
- print "IP Address: " + interface['eth_ip_addr'] + '/' + str(interface['eth_ip_mask'])
- print "Speed: ", interface['eth_speed']
- print "State: ", interface['state']
- print "MTU: ", interface['eth_mtu']
- ================
- #!/usr/bin/env python
- import request
- import json
- from requests.auth import HTTPBasicAuth
- if __name__ == "__main__":
- auth = HTTPBasicAuth('cisco', 'cisco')
- headers = {
- 'Content-Type': 'application/json'
- }
- commands = ['config t', 'vlan 150', 'exit', 'interface Eth2/5', 'switchport', 'switchport access vlan 150']
- commands = ' ; '.join(commands)
- # you could have also manually created a semi-colon delimited string.
- # testing this in NX-API sandbox will show you the proper format required
- payload = {
- "ins_api": {
- "version": "1.0",
- "type": "cli_conf",
- "chunk": "0",
- "sid": "1",
- "input": commands,
- "output_format": "json"
- }
- }
- url = 'http://nxosv/ins'
- response = requests.post(url, data=json.dumps(payload), headers=headers, auth=auth)
- rx_object = json.loads(response.text)
- print json.dumps(rx_object, indent=4)
- ==============
- #!/usr/bin/env python
- from ncclient import manager
- if __name__ == '__main__':
- with manager.connect(host='nxosv', port=22, username='cisco', password='cisco',
- hostkey_verify=False, device_params={'name': 'nexus'},
- allow_agent=False, look_for_keys=False) as device:
- get_filter = """
- <show>
- <version>
- </version>
- </show>
- """
- nc_get_reply = device.get(('subtree', get_filter))
- print 'Response as XML String: '
- print nc_get_reply.xml
- ns_map = {'mod': 'http://www.cisco.com/nxos:1.0:sysmgrcli'}
- xml_rsp_cid = nc_get_reply.data_ele.find('.//mod:chassis_id', ns_map)
- cid_value = xml_rsp_cid.text
- xml_rsp_sw = nc_get_reply.data_ele.find('.//mod:sys_ver_str', ns_map)
- sw_value = xml_rsp_sw.text
- print '================================='
- print 'Chassis ID: ', cid_value
- print 'Software Versi
- ==============
- python nc_config_interface.py
- #!/usr/bin/env python
- from ncclient import manager
- if __name__ == '__main__':
- with manager.connect(host='nxosv', port=22, username='cisco', password='cisco',
- hostkey_verify=False, device_params={'name': 'nexus'},
- allow_agent=False, look_for_keys=False) as device:
- commands = ['config t', 'interface Ethernet2/6', 'switchport', 'description Configured by Python ncclient']
- nc_config_reply = device.exec_command(commands)
- print nc_config_reply.xml
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement