BLOGPOST#157 Cisco Nexus API Automation using Python: Send CLI Conf Commands with JSON

Introduction:

Welcome to the Python Learning Series for Network Engineers! In this blog post, we will cover how to perform Nexus API automation using Python. Specifically, we will cover how to send configuration commands using Nexus API message format, JSON, and command type cli conf. We will also discuss the error actions supported and how to handle errors.

Cisco’s native JSON format is more capable compared to the standard protocol JSON RPC when managing codebases. JSON RPC only allows sending one command at a time, whereas Cisco’s native JSON method allows multiple commands to be sent as a single line of command. Each command can be separated using semicolons.

Sending Configuration:
To send a configuration, you can input the configuration separated by semicolons and select JSON as the message format. The default action is stop on error, which means that it will stop after the first error encountered. You can also choose continue on error, which will execute all the commands, including the ones after the error. Additionally, you can choose rollback on error, which will roll back the configuration if an error occurs.

payload = json.dumps({
  "ins_api": {
    "version": "1.0",
    "type": "cli_conf",
    "chunk": "0",
    "sid": "sid",
    "input": "interface e1/2 ; description from-jsonrpc ; aaa ; no switchport ; ip add 1.1.1.1 255.255.255.0",
    "output_format": "json",
    "rollback": "rollback-on-error"
  }
})

Test it from Postman:
To test this from Postman, you can create a collection and add a new request. The method should be POST, and the content type should be application/json. You should also include the basic username and password in the headers. Under the body, select raw and paste the content there.

To get the Python Script:
If you want to get the Python script, you can click on code from Postman, and you should see the Python code. The payload is the same, and the authorization is basic with the base64-encoded value of the username and password. To decode the same string give ‘base64 –decode’.

Running from Pycharm:
Create a new Python file and paste the entire Script without cookie. If you run as it is, you will get the SSL Validation error. To handle this we need to give ‘Verify=False’ and run it.

Conclusion:
Overall, using Nexus API automation with Python can be very helpful for network engineers. With JSON, you can send multiple commands as a single line of command, and with the error actions, you can handle errors effectively.

Complete Script for Reference:

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

url = "https://192.168.0.201/ins"

payload = json.dumps({
  "ins_api": {
    "version": "1.0",
    "type": "cli_conf",
    "chunk": "0",
    "sid": "sid",
    "input": "interface e1/2 ; description from-jsonrpc ; aaa ; no switchport ; ip add 1.1.1.1 255.255.255.0",
    "output_format": "json",
    "rollback": "rollback-on-error"
  }
})
headers = {
  'Content-Type': 'application/json',
  'Authorization': 'Basic YWRtaW46YWRtaW4=',
}

response = requests.request("POST", url, headers=headers, data=payload, verify=False)

print(response.text)

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