官术网_书友最值得收藏!

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.

Requests is a very popular, self-proclaimed HTTP for the human library used by companies like Amazon, Google, NSA, and more. You can find more information about it on the official site ( http://docs.python-requests.org/en/master/).

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.

主站蜘蛛池模板: 喜德县| 繁峙县| 重庆市| 温州市| 新建县| 宿州市| 神池县| 青海省| 南充市| 库伦旗| 乌鲁木齐市| 宁城县| 泾川县| 八宿县| 海阳市| 保亭| 忻城县| 宁河县| 双牌县| 元江| 潮安县| 抚顺县| 珠海市| 铜川市| 卓资县| 新昌县| 大新县| 武威市| 铅山县| 永顺县| 衡阳县| 高台县| 玉环县| 荥经县| 西华县| 鄂州市| 通海县| 台东县| 治多县| 禄劝| 昌宁县|