Overview
This chapter provides an overview and installation information needed to use the Python application programming interface (API) support on Cisco Nexus 3000 Series switches.
Information About the Python API
Python is an easy-to-learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.
The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python website:
The same site also distributes and points to many free third-party Python modules, programs and tools, and additional documentation.
The Cisco Nexus 3000 Series switches support all the features available in Python v2.7.2.
The Python scripting capability on the Cisco Nexus 3000 Series switches allows you to perform the following tasks:
- Running a script to verify configuration on switch bootup.
- Backing up a configuration.
- Proactive congestion management by monitoring and responding to buffer utilization characteristics.
- Integration with the Power-On Auto Provisioning or EEM modules.
- Ability to perform a job at a specific time interval (such as Port Auto Description).
- Programmatic access to the switch command-line interface (CLI) to perform various tasks.
Installing Python
The Python interpreter is available by default in the Cisco NX-OS software.You can invoke Python by entering the python command, and you can write scripts to access Cisco NX-OS APIs by importing the cisco.py module using the import cisco command.
Installing Third Party Pure Python Packages
You can install the third-party pure Python package by copying mypkg.tgz on your server. Perform the following steps to extract and install the third-party package:
- Secure-copy the tar file by executing the copy scp://user@server/path/to/mypkg.tgz bootflash:mypkg.tgz vrf management command.
- Untar the mypkg.tgz file by using the tar extract bootflash:mypkg.tgz command.
- Move the extracted file to bootflash by using the move bootflash:mypkg-1.2/* bootflash: command.
- You can install the package by using the python setup.py install command.
- Remove the copied file from bootflash.
- You can use the third-party package in scripts or in the Python shell.
Note You will be able to install the third-party packages using the easy_install command in future releases.
Using Python
This section describes how to write and execute Python scripts by passing parameters and includes the following topics:
- Entering the Python Shell
- Executing Scripts
- Passing Parameters to the Script
- Embedded Event Manager Support
Entering the Python Shell
You can enter the Python shell by using the python command without any parameters.
Passing Parameters to the Script
You can execute a Python script by using the python filename [arg1, arg2, arg3, ...] command.
Embedded Event Manager Support
Embedded Event Manager (EEM) supports invocation of Python scripts based on events. Syslog for events can be passed to a Python script by using the variable $ command.
The following example shows EEM invoking a python script for an IF_DOWN event.
Calculating MD5 Checksum
Every time you make a change to the configuration script, ensure that you recalculate the MD5 checksum. The following example shows the script to be run in a bash shell to recalculate the MD5 checksum:
Feedback