This guide provides information needed to use the Python Software Development Kit (SDK) to develop programs for the Cisco Nexus 9000 Series switches operating in Cisco NX-OS Software mode. This guide includes descriptions of the application programming interface (API) classes and methods.
Note: This guide is intended for users who are familiar with the Python programming language and who have experience configuring Cisco Nexus switches. For detailed configuration information, refer to other Cisco documentation, such as Cisco Nexus 9000 Series NX-OS configuration guides and CLI command references.
About the Python Programming Language
The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python website at www.python.org. The Python website also distributes and links to many free third-party Python modules, programs and tools, and additional documentation.
The Cisco Nexus 9000 Series switches support all of the features available in Python v2.7.5.
Features of Nexus 9000 Python Scripting
The Python scripting capability on the Cisco Nexus 9000 Series switches allows you to perform the following tasks:
Run a script to verify configuration on switch bootup.
Back up a configuration.
Perform proactive congestion management by monitoring and responding to buffer utilization characteristics.
Integrate with the Power-On Auto Provisioning for EEM modules.
Perform a job at a specific time interval (such as Port Auto Description).
Programmatically access the switch command-line interface (CLI) to perform various tasks.
Python can also be accessed from the Bash shell.
Getting Started with the Cisco Nexus 9000 Series Python SDK
Installing Python and the Python API
The Python interpreter and the Cisco Python SDK are available by default in the Cisco NX-OS software. Standard Python packages are included. Unless you require additional Python packages, no further installation is necessary.
Cisco Python Package
The Cisco Python SDK provides a cisco Python package that enables access to many core network device modules, such as interfaces, VLANs, ACLs, and routes.
The methods and functions in the cisco Python package are implemented in Python source files included with the SDK package. Many of these files have documentation embedded within the source code. In the Python source files, documentation is contained in documentation strings, bracketed by three back-ticks, or ```. Some source files and methods are for internal use and do not have embedded documentation.
Using Python
Entering the Python Shell
To enter the Python shell, enter the python command from the NX-OS command line with no parameters as shown in the example.
Note: While you can run Python directly from the NX-OS command line, we recommend that you run it from the Guest Shell, a container environment, which is described later in this document.
To exit the Python shell, type exit().
Importing the cisco Python Package and Other Modules
Import the cisco Python package as shown in the example. From the cisco package, import individual modules as needed using
from cisco.module_name import *
where module_name is the name of the individual module. In the example, the ACL module is imported.
Other useful modules include the cli module and the json module.
Accessing Python Help
To display Python help, enter help() at the Python prompt. In Python help mode, enter modules to list the available Python modules.
To display help on a specific Python module, enter help (module_name) where module_name is the name of the for which you are requesting help. For example, enter help (json) to display help on the json module.
After you have imported the cisco package, you can display its help by entering help (cisco) at the Python prompt. To display help on a specific module in the Cisco Python package, enter help (cisco.module_name) where module_name is the name of the module. For example, to display help on the Cisco ACL module, enter help (cisco.acl).
Running Python Code
After starting Python and importing the required packages and modules, you can run Python scripts directly or you can enter blocks of Python code and run the code.
The example shows how to query the interfaces running on the switch.
You can enter lines of Python code to execute a block of code. A colon (":") at the end of a line tells the Python interpreter that the subsequent lines will form a code block. After the colon, you must indent the subsequent lines in the block, following Python indentation rules. When you have typed the block, press return twice to execute the code.
You can run stored scripts in non-interactive mode from the switch CLI, as described in "Executing Scripts".
When you run CLI commands using the Python interpreter, you can use methods within the cli class to format the output displayed on the console. The following examples show several ways to format the command output.
Example 1: Using clip
The clip method allows you to display multiple lines of output.
Example 2: Using 'where detail' with the cli Method
Example 3: Using 'where detail' with a Variable and the cli Method
Example 4: Using JSON to Display Output
You can use JSON to display particularly long output, as shown in the following example:
Executing Scripts
You can run a Python script in non-interactive mode by providing the Python script name as an argument to the Python CLI command. Python scripts must be placed under the bootflash or volatile scheme.
To execute a Python script, enter the python command followed by the filename of the script followed by any arguments for the script, as shown in this example:
switch# python bootflash:scripts/deltaCounters.py Ethernet1/1 1 5
A maximum of 32 command line arguments for the Python script are allowed with the python CLI command.
Several example scripts are provided in "Example Scripts".
Using the source Command
The Cisco Nexus 9000 Series device supports the source CLI command for running Python scripts. The bootflash:scripts directory is the default script directory for the source CLI command.
Using the source command, the previous example could also be executed as follows:
switch# source deltaCounters Ethernet1/1 1 5
The source command is helpful when executing compound commands from the CLI. The following example shows a source command following a pipe operator ("|"), allowing a Python script to receive input from another CLI command:
switch# show running-config | source sys/cgrep policy-map