BLOGPOST#156 Nexus API CLI Show Output Parsing Example: A Step-by-Step Guide to Parsing CLI command output using regex

Introduction:

Welcome to the Python Learning series for Network Engineers! In this post, we will focus on learning how to do Nexus API automation using Python. Specifically, we will see how to manually parse Nexus API. In the previous video, we learned how to parse a “cli show” command in CLI show and get data in JSON format. For this demonstration, we will use Cisco ‘sandbox-nxos-1’, which is accessible from the internet for everyone.

Command Execution:
First, we will try executing “cli show command type” and get the response in JSON format. If we change this to “cli show ASCII,” the entire output will be in plain text format, which is the same as what we receive in “cli show version” output. This method will be useful if you already have a mechanism to parse plain “cli” commands. You can replace that message with this API and apply the same parsing logic.

We will write basic regex and parse some of the output, using the API to do so. To begin, we will take a copy of the previous script, which used Cli Show Command and got the entire output in JSON format, and we have parsed all this output from the three commands. We will change the third command to “cli_show_ascii” execute only one command (“show version”), and try to print the output.

myheaders={'content-type':'application/json'}
payload={
  "ins_api": {
    "version": "1.0",
    "type": "cli_show_ascii",
    "chunk": "0",
    "sid": "sid",
    "input": "show version",
    "output_format": "json"
  }
}

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

For this demonstration let’s use ‘devnet’ device details by changing the hostname and password. From here also we will get the output as follow:

switchuser=’admin’
switchpassword=’Admin_1234!’

url=’https://sandbox-nxos-1.cisco.com/ins’

CLI Output:
We will then create a variable called “cli,” with the key equal to “api.” From the response, we have a key called “api,” and inside the “api,” we have “outputs.” This “outputs” is followed by “output,” and then “body,” which is the message body. Here, we’ll be getting “load.”

cli_ascii_out = response['ins_api']['outputs']['output']['body']
print(cli_ascii_out)

Parsing Specific Data:
To parse this output, we will create another variable and match it with this particular line. We will try to catch the processor family name and processor speed. If you look into the sandbox output, we are already receiving that data, but it is a single line output. We’ll try to parse it separately. We will use regex 101 for creating the regex logic to parse almost all the outputs. By default, it is parsing.

my_pattern = re.compile(r"(?P\S.+) @ (?P\S+) with \d+ \S+ of memory.")

processor_match = my_pattern.search(cli_ascii_out).groupdict()
print(processor_match)
print(processor_match['ProcessorFamily'])
print(processor_match['ProcessorSpeed'])

Comparison between “Cli Show” and “Cli Array” Outputs:
To compare the outputs of Cli Show and Cli Array, a package can be used to perform a comparison of the outputs. First, execute the “show hardware” command in the local device to get the data in Cli Show format. Next, execute the “array” command to get the data in Cli Array format. Save both outputs in text files and use a comparison tool to compare the outputs.

When comparing the outputs, it is important to pay attention to the formatting of the data, as well as the data types. While using Cli Show, the data will be displayed in a dictionary format, and all data types will be displayed as strings. When using Cli Array, the data will be displayed in an array format, and the data types will be displayed as their true type.

Conclusion:
In conclusion, we have learned how to manually parse Nexus API and parse the output using basic regex. This is an important skill for network engineers who want to automate their tasks using Python. By understanding the differences between Cli Show and Cli Array, users can effectively use Cli to interact with hardware and software interfaces.

Complete Script for Reference:

#! /usr/local/Python_envs/Python3/bin/python3
import re

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_1234!'

url='https://sandbox-nxos-1.cisco.com/ins'
myheaders={'content-type':'application/json'}
payload={
  "ins_api": {
    "version": "1.0",
    "type": "cli_show_ascii",
    "chunk": "0",
    "sid": "sid",
    "input": "show version",
    "output_format": "json"
  }
}
my_pattern = re.compile(r"(?P\S.+) @ (?P\S+) with \d+ \S+ of memory.")
response = requests.post(url,data=json.dumps(payload), headers=myheaders,auth=(switchuser,switchpassword), verify=False).json()
print(response)
cli_ascii_out = response['ins_api']['outputs']['output']['body']
print(cli_ascii_out)
processor_match = my_pattern.search(cli_ascii_out).groupdict()
print(processor_match)
print(processor_match['ProcessorFamily'])
print(processor_match['ProcessorSpeed'])

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