BLOG POST#152 Cisco Nexus API Automation using Python

Introduction:

Python is a very popular language for Network Engineers, and it is used for automation, scripting, and network programming. Python also has a lot of modules available that can be used for networking, like Netmiko, Nornir, and Napalm. In this series, we will discuss how to use Python to automate Nexus API.
In the previous video, we have seen how to install Nexus 9000 V into eve-ng, enable API, and write a basic Python script to connect to the device and get the “show hardware” output in JSON format. In this video, we will discuss JSON, RPC, command types, and error actions supported in NX API. We will see how to use RPC to send NX API CLI commands and how to use CLI to send configuration or show commands.

What is JSON RPC?
JSON RPC (Remote Procedure Call) is a server-to-client communication protocol. The client requests data from the server, and the server processes the request and returns the data. In JSON RPC, data is passed in JSON format. Cisco Nexus supports JSON RPC, and XML RPC is also supported by Cisco’s standard data formats like JSON and XML.

JSON-RPC API:
JSON-RPC stands for JavaScript Object Notation – Remote Procedure Call. It is a lightweight protocol that uses JSON for data exchange. JSON-RPC is used to send commands to a remote server and retrieve data.

JSON RPC and CLI:
To use JSON RPC, we need to give the JSON or RPC with version number 2.0 and we need to add some method,parameters and ID. The response also is going to contain JSON RPC result error and ID. JSON RPC can be used for sending the Nexus CLI commands. CLI can be used for sending configuration or show commands. CLI will return the entire output of the show command in a plaintext format.
Let’s take an example of how to use CLI to send the “show version” command. The request format is in plain text, and if we want to decode it, we can write a Python script to get the response. We can use “CLI” for the same request and get the output in JSON format.                                            

Python and JSON-RPC:
Python has a built-in library called jsonrpcclient that we will use to interact with the JSON-RPC API. We will also use the requests library to send the requests and receive responses.

Let’s get started!

Step 1: Import Libraries:
We will start by importing the necessary libraries. We will import the jsonrpcclient library to interact with the JSON-RPC API and the requests library to send requests and receive responses.

import jsonrpcclient
import requests

Step 2: Set Variables:
Next, we will set the variables for the URL and credentials for the network device. We will use these variables to send requests to the device.

switchuser=’admin’
switchpassword=’admin’

url=’https://192.168.0.201/ins’

Step 3: Define Payload:
Now, we will define the payload that we will send to the device. We will use the clay API to send show commands to the device. The clay API returns the output in plain text format.

payload=[
  {
    "jsonrpc": "2.0",
    "method": "cli",
    "params": {
      "cmd": "interface e1/2",
      "version": 1
    },
    "id": 1,
    "rollback": "rollback-on-error"
  },
  {
    "jsonrpc": "2.0",
    "method": "cli",
    "params": {
      "cmd": "description from-jsonrpc",
      "version": 1
    },
    "id": 2,
    "rollback": "rollback-on-error"
  },
  {
    "jsonrpc": "2.0",
    "method": "cli",
    "params": {
      "cmd": "aaa",
      "version": 1
    },
    "id": 3,
    "rollback": "rollback-on-error"
  },
  {
    "jsonrpc": "2.0",
    "method": "cli",
    "params": {
      "cmd": "no switchport",
      "version": 1
    },
    "id": 4,
    "rollback": "rollback-on-error"
  },
  {
    "jsonrpc": "2.0",
    "method": "cli",
    "params": {
      "cmd": "ip add 1.1.1.1 255.255.255.0",
      "version": 1
    },
    "id": 5,
    "rollback": "rollback-on-error"
  }
]

Step 4: Send Request:
Now, we will send the request to the device using the requests library. We will use the jsonrpcclient library to encode the payload in JSON format.

response = requests.post(url,data=json.dumps(payload), headers=myheaders,auth=(switchuser,switchpassword), verify=False)

Step 5: Print Output:
Finally, we will print the output from the device. We will use the jsonrpcclient library to decode the response.

print(response.status_code)
output = response.json()
pprint(output)
print(response['result']['msg'])

Conclusion:
In this blog, we have seen how to use Nexus API automation using Python. We have discussed JSON RPC and CLI, which are used for sending Nexus CLI commands and configuration or show commands. Python is a powerful language and has a lot of modules available that can be used for networking. Automation using Python saves a lot of time and reduces errors that can occur when configuring devices manually. With the help of Nexus API and Python, Network Engineers can automate their work and make their job more efficient.

Sample Script for Reference:

#! /usr/local/Python_envs/Python3/bin/python3
import requests
import json
from pprint import pprint

from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
"""
Modify these please
"""

switchuser='admin'
switchpassword='admin'

url='https://192.168.0.201/ins'
myheaders={'content-type':'application/json-rpc'}
payload=[
  {
    "jsonrpc": "2.0",
    "method": "cli",
    "params": {
      "cmd": "interface e1/2",
      "version": 1
    },
    "id": 1,
    "rollback": "rollback-on-error"
  },
  {
    "jsonrpc": "2.0",
    "method": "cli",
    "params": {
      "cmd": "description from-jsonrpc",
      "version": 1
    },
    "id": 2,
    "rollback": "rollback-on-error"
  },
  {
    "jsonrpc": "2.0",
    "method": "cli",
    "params": {
      "cmd": "aaa",
      "version": 1
    },
    "id": 3,
    "rollback": "rollback-on-error"
  },
  {
    "jsonrpc": "2.0",
    "method": "cli",
    "params": {
      "cmd": "no switchport",
      "version": 1
    },
    "id": 4,
    "rollback": "rollback-on-error"
  },
  {
    "jsonrpc": "2.0",
    "method": "cli",
    "params": {
      "cmd": "ip add 1.1.1.1 255.255.255.0",
      "version": 1
    },
    "id": 5,
    "rollback": "rollback-on-error"
  }
]

response = requests.post(url,data=json.dumps(payload), headers=myheaders,auth=(switchuser,switchpassword), verify=False)
print(response.status_code)
output = response.json()
pprint(output)
print(response['result']['msg'])

Unlock the Power of Network Automation ! Enroll in our Comprehensive Udemy Course Today !

Topics & libraries Covered:

  • PARAMIKO, NETMIKO, NAPALM, NORNIR and NCCLIENT libraries
  • Cisco IOS, vIOS and NXOS SSH Automation
  • Cisco CSR 1000v Netconf and RestAPI examples
  • NX-API Automation (NXAPI CLI and bash)
  • PyATS, Cisco Genie Parser
  • NETCONF & RESTCONF API Automation
  • YANG Data Models and YANG Suite demo
  • IPAddress Module and requests module
  • Python Core Fundamentals for Network Engineers
  • Python Data Types
  • Python Text & CSV File operations (read/write) device data
  • Python IDE (PyCharm) Setup for Network Automation
  • GNS3 Lab Setup for Network Automation
  • Python Functions, Modules, Classes and Objects Tutorial
  • Python Multithreading Examples (threading and concurrent futures)
  • Python Logging, schedule and Email for monitoring
  • Python SSH CLI Parsing Using RegEx
  • How to Use JSON, YAML and XML Files for Device Config