BLOGPOST#159 Running Python Scripts inside Cisco NXOS Bash


In this blog post, we will discuss how to run Python scripts within Nexus OS.Python scripting within Cisco Nexus can greatly improve network management and automation. Nexus OS comes with a Python interpreter installed by default. To access Python on Nexus OS, type “python” in the terminal. You will see two versions of Python available: Python 2 and Python 3. It is recommended to use Python 3.

To view the default libraries available, execute “help(modules)” on the terminal. By default, Nexus OS has Cisco CLI, Cisco Nexus OS and NX OS Utils, and many more libraries.

In the previous video, we learned how to enable bash in Nexus OS and access the bash of Nexus OS and execute Linux commands. We created a Python file in the bash terminal. To run this Python file, you need to use the command “Python3” and the file path.

To write a basic Python script that uses CLI, create a new file in the scripts directory. We can run the Python script from the scripts directory using the “source” command.

Set the Python Location:
First, set the Python location by accessing the interpreter and importing the path. Then set the location using the path in the script file. After setting the location, open the file in bash, paste the content, and run the script.

To use the CLI model, give “help(cli)” Then, it will execute “CLI commands” to return the output in a plain string format. You can also use “clid” to get the output in a JSON format, and “clip” to get the output in a proper print format. Let’s see all these three methods.

First, we need to import the necessary functions. We can do this using the following code:

from cli import *

Once we have imported the functions, we can execute the show version command to receive the output in a plain string format. Here’s an example:

cli('show version')
clid('show version')
clip('show version')

To convert the script into a Python file, create a new file and copy the content from the bash terminal. Add the content to a variable and use “clid(show version)” to get the command output in a proper JSON format and print the output. Here’s the code:

output = clid(show version)

However, we’ll receive an error since the type of the output is a string. To convert it into a proper dictionary format, we’ll need to import the JSON module and use the “loads” function. Here’s the modified code:

from cli import *
import json

output = clid('show version')
json_data = json.loads(output)

Now we can parse any data from the output. For example, if we want to parse only the chassis ID from the output, we can use the following code:


We can also execute the script from an Nexus API sandbox by copying the command from the terminal and pasting it into the sandbox. Additionally, we can schedule the script execution within Nexus using the Scheduler feature. And for more advanced scripting, we can use the Just shell feature.

In conclusion, running Python scripts within Nexus OS is easy using the default Python interpreter. You can also use CLI to get the command output in a variety of formats. By following this beginner’s guide, you can start writing your own Python scripts to simplify your work.

Unlock the Power of Network Automation ! Enroll in our Comprehensive Udemy Course Today !

Topics & libraries Covered:

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