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
- 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.
- 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.