Nexus devices CLI parsing using Python, XML and JSON

This blog is part of my series on Devops for Networking. As I indicated in 1 of my earlier blog, majority of Network devices still have CLI as the only means of configuration and monitoring. CLI is not automation friendly because of obvious reasons. In the last few years, Cisco NXOS devices provides CLI output in XML and JSON format. This allows much easier ways to parse the CLI output which aids in automation. In this blog, I will cover some examples of monitoring Cisco NXOS devices using Python interface to interact with the device and get useful information.


  • I have used Nexus 3k device for the tests below. Following is the version running on my system.
kickstart: version 6.0(2)U4(1) [build 6.0(2)U4(0.786)]
system:    version 6.0(2)U4(1) [build 6.0(2)U4(0.810)]
  • Install Paramiko Python SSH library from here. I have Python 2.7.3 and following is my Paramiko version. I am using Ubuntu 12.04.
pip show paramiko
Name: paramiko
Version: 1.11.0
Location: /usr/local/lib/python2.7/dist-packages
Requires: pycrypto

Script overview

  • I have written a Python script that connects to the device using Paramiko SSH library, executes “show interface brief” CLI command and fetches the output in JSON and XML format.
  • The script parses the output and displays the interfaces that are operationally in UP state.
  • Script used for JSON can be found here. JSON output for “show interface brief” can be found here.
  • Script used for XML can be found here. XML output for “show interface brief” can be found here.

Some thoughts:

  • As can be seen in the example, parsing XML and JSON output is much easier than parsing CLI output and it is easier to automate using this approach.
  • Even though output is easier to parse, there is no standard approach to define the data format expected in the command output.
  • I observed that some commands that does not display tables do not have JSON and XML encoded output even when JSON or XML option is used in the CLI. Not sure why it is like that.



7 thoughts on “Nexus devices CLI parsing using Python, XML and JSON

      1. No, you are running a CLI show command over SSH to generate output in XML and then parsing it. What I meant was something like XML-RPC.

  1. Hi Amit

    Now I understand what you are asking.
    Following are different schemes I have seen in Cisco Nexus devices. I dont know about other OS.
    CLI over ssh, http, https. Output can be in json or xml
    Netconf using xsd model over ssh ( This seems very primitive, will be useful with Yang models when it becomes available.
    Onepk – using Cisco provided api( there is limited flexibility here because api set is limited.

    Havent seen XML RPC. The closest I have seen to this is for configuring Cisco UCS( Here, the management tree is accessible and we can get and set any object.

    Hope, this helps.


  2. Hi Sreeni,

    I am reading your blogpost for “Nexus devices CLI parsing using Python, XML and JSON”
    I am new to DevOps concept so forgive my silly questions if I ask

    I have couple of doubts about installation.

    1. you mentioned you installed Paramiko python ssh library and you are having python 2.7.3 version. do you refer that on your ubuntu 12.04 machine, you installed python and paramiko? – please explain

    2. second question I have is, In my environment, I am often required to configure access vlan interfaces for nexus 5k and 2k FEXes. it is like Eth102/1/10 interface, make it as switchport access vlan 10. I am looking for a script which can configure the port for desired vlan membership.

    3. my third question is, from the ubuntu box, can I schedule a script to run at 4:00 AM. The script should run at that time, it should capture show interface command output before the change and after the change and send it to email format to NOC team as well as other desired email address. this will make life little easy not to wake up at 4 AM for making the change.

    Again, As I mentioned, I am new to DevOps concept, and if possible please provide answer to my questions


    1. Hi Visio
      Q1: yes, script runs from Ubuntu machine.
      Q2: As long as the device supports returning returning output in json or xml format, you can use the script.
      Q3: You can use Linux cron facility or Python timers to schedule periodic job.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s