- 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.
- Boost.Asio C++ Network Programming(Second Edition)
- Docker and Kubernetes for Java Developers
- Angular UI Development with PrimeNG
- 造個小程序:與微信一起干件正經事兒
- Java EE框架整合開發入門到實戰:Spring+Spring MVC+MyBatis(微課版)
- Spring Boot企業級項目開發實戰
- SQL經典實例(第2版)
- Visual Basic程序設計實踐教程
- Hands-On Kubernetes on Windows
- Delphi開發典型模塊大全(修訂版)
- Responsive Web Design with jQuery
- C#從入門到精通(微視頻精編版)
- JavaScript前端開發程序設計教程(微課版)
- 軟件自動化測試實戰解析:基于Python3編程語言
- 零基礎入門Python數據分析與機器學習