BLOGPOST#155 Nexus API Automation using Python: Parsing JSON-formatted Show Command Outputs for Effective Network Management

Introduction:

In this blog post, we will discuss how to parse the output of a show command using Nexus API message format JSON. In our previous video, we learned how to parse the show command output using JSON RPC, which is a standard protocol for remote procedure calls. However, in this post, we will look into Cisco’s native method, which has more capabilities and provides better command handling and responses.

First, let’s understand the commands that this API supports. It supports “cli_show,” “cli_show array,” “cli_ascii,” and “cli_conf.” If you want to send multiple commands, you can separate them using a semicolon( ; ). According to Cisco’s documentation, you need to add a space before and after the semicolon.

Parsing Command Output:
Now let’s try to parse the command output using JSON. We will use the message format JSON and send the show command. The output will contain additional information like input command, code, message status, etc. We can parse this data and extract the required information.

You can find the list of supported commands in the command references. By default, it supports many commands, and you can see the command schema there. To send multiple commands in a single request, we can use Python’s multi-line string and replace the newline character with a semicolon and space. If you have 15-20 lines, you can use this technique to send multiple commands.

>>> s = '''show version
... show hardware
... show ip interface brief'''
>>> s.replace('/n',' ; ')
'show version ; show hardware ; show ip interface brief'
>>>

Script from Python:         
In the Python script, the only difference from the JSON RPC script is the application content type. In JSON RPC, it was JSON-RPC, but here it is JSON. We will add the JSON header and pass the commands as a payload. We will also add verify false, as our nexus is using a self-signed certificate. In production, we suggest using a valid SSL certificate. We can remove this and print the response.

myheaders={'content-type':'application/json'}
payload={
  "ins_api": {
    "version": "1.0",
    "type": "cli_show",
    "chunk": "0",
    "sid": "sid",
    "input": "show version ; show hardware ; show ip interface brief",
    "output_format": "json"
  }
}

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

Parsing the Output:
Now, let’s try to parse the output. We will pass the ‘chassis_id’ to get the output. The format will be “ins API, outputs, output, and a list.” The list contains three elements because we executed three commands. We will take the first element for the subversion command and parse the data. We can do the same for other commands like the processor board ID.

print(response["ins_api"]["outputs"]["output"][0]['body']['chassis_id'])
print(response["ins_api"]["outputs"]["output"][0]['body']['proc_board_id'])
print(response["ins_api"]["outputs"]["output"][1]['body']['manufacturer'])
print(response["ins_api"]["outputs"]["output"][2]['body']['TABLE_intf']['ROW_intf']['intf-name'])
print(response["ins_api"]["outputs"]["output"][2]['body']['TABLE_intf']['ROW_intf']['prefix'])

Conclusion:
In conclusion, parsing show command output using Nexus API message format JSON is easy and straightforward. It provides better command handling and responses, and you can send multiple commands in a single request. With the help of Python, you can parse the data and extract the required information.

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)

switchuser='admin'
switchpassword='admin'

url='https://192.168.0.201/ins'
myheaders={'content-type':'application/json'}
payload={
  "ins_api": {
    "version": "1.0",
    "type": "cli_show",
    "chunk": "0",
    "sid": "sid",
    "input": "show version ; show hardware ; show ip interface brief",
    "output_format": "json"
  }
}

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

print(response["ins_api"]["outputs"]["output"][0]['body']['chassis_id'])
print(response["ins_api"]["outputs"]["output"][0]['body']['proc_board_id'])
print(response["ins_api"]["outputs"]["output"][1]['body']['manufacturer'])
print(response["ins_api"]["outputs"]["output"][2]['body']['TABLE_intf']['ROW_intf']['intf-name'])
print(response["ins_api"]["outputs"]["output"][2]['body']['TABLE_intf']['ROW_intf']['prefix'])

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