- Mastering Python Networking
- Eric Chou
- 598字
- 2021-07-02 21:42:39
NX-API examples
Since we have turned on sandbox, we can launch a web browser and take a look at the various message formats, requests, and responses based on the CLI command that we are already familiar with:

In the following example, I have selected JSON-RPC and CLI command type for the command called show version:

The sandbox comes in handy if you are unsure about the supportability of message format, or if you have questions about the field key for which the value you want to retrieve in your code.
In our first example, we are just going to connect to the Nexus device and print out the capabilities exchanged when the connection was first made:
#!/usr/bin/env python3
from ncclient import manager
conn = manager.connect(
host='172.16.1.90',
port=22,
username='cisco',
password='cisco',
hostkey_verify=False,
device_params={'name': 'nexus'},
look_for_keys=False)
for value in conn.server_capabilities:
print(value)
conn.close_session()
The connection parameters of host, port, username, and password are pretty self explanatory. The device parameter specifies the kind of device the client is connecting to. We will also see a differentiation in the Juniper NETCONF sections. The hostkey_verify bypass the known_host requirement for SSH, otherwise the host needs to be listed in the ~/.ssh/known_hosts file. The look_for_keys option disables public-private key authentication but uses a username and password for authentication.
The output will show that the XML and NETCONF supported feature by this version of NX-OS:
$ python3 cisco_nxapi_1.py
urn:ietf:params:xml:ns:netconf:base:1.0
urn:ietf:params:netconf:base:1.0
Using ncclient and NETCONF over SSH is great because it gets us closer to the native implementation and syntax. We will use the library more later on. For NX-API, I personally feel that it is easier to deal with HTTPS and JSON-RPC. In the earlier screenshot of NX-API Developer Sandbox, if you noticed in the Request box, there is a box labeled Python. If you click on it, you will be able to get an automatically converted Python script based on the request library.
For the show version example, the following Python script is automatically generated for you. I am pasting in the output without any modification:
"""
NX-API-BOT
"""
import requests
import json
"""
Modify these please
"""
url='http://YOURIP/ins'
switchuser='USERID'
switchpassword='PASSWORD'
myheaders={'content-type':'application/json-rpc'}
payload=[
{
"jsonrpc": "2.0",
"method": "cli",
"params": {
"cmd": "show version",
"version": 1.2
},
"id": 1
}
]
response = requests.post(url,data=json.dumps(payload),
headers=myheaders,auth=(switchuser,switchpassword)).json()
In cisco_nxapi_2.py file, you will see that I have only modified the URL, username, and password of the preceding file, and I have parsed the output to only include the software version. Here is the output:
$ python3 cisco_nxapi_2.py
7.2(0)D1(1) [build 7.2(0)ZD(0.120)]
The best part about using this method is that the same syntax works with both configuration command as well as show commands. This is illustrated in the cisco_nxapi_3.py file. For multi-line configuration, you can use the id field to specify the order of operations. In cisco_nxapi_4.py, the following payload was listed for changing the description of the interface Ethernet 2/12 in the interface configuration mode:
{
"jsonrpc": "2.0",
"method": "cli",
"params": {
"cmd": "interface ethernet 2/12",
"version": 1.2
},
"id": 1
},
{
"jsonrpc": "2.0",
"method": "cli",
"params": {
"cmd": "description foo-bar",
"version": 1.2
},
"id": 2
},
{
"jsonrpc": "2.0",
"method": "cli",
"params": {
"cmd": "end",
"version": 1.2
},
"id": 3
},
{
"jsonrpc": "2.0",
"method": "cli",
"params": {
"cmd": "copy run start",
"version": 1.2
},
"id": 4
}
]
In the next section, we will look at the examples for Cisco NETCONF and the YANG model.
- 程序員修煉之道:程序設計入門30講
- JavaScript全程指南
- LabVIEW Graphical Programming Cookbook
- 小程序實戰視頻課:微信小程序開發全案精講
- Java應用開發與實踐
- 軟件測試項目實戰之性能測試篇
- Visual C++串口通信技術詳解(第2版)
- Visual C++應用開發
- Python機器學習算法與實戰
- R Deep Learning Cookbook
- 飛槳PaddlePaddle深度學習實戰
- JavaScript動態網頁編程
- OpenCV 3.0 Computer Vision with Java
- Hack與HHVM權威指南
- Arduino Electronics Blueprints