Table Of Contents
Overview
Information About the Python API
Installing Python
Installing Third Party Pure Python Packages
Using Python
Entering Python Shell
Executing Scripts
Passing Parameters to the Script
Overview
This chapter provides the overview and installation information needed to use the Python Application Programming Interface (API) support on Cisco Nexus 5000 Series switches.
This chapter includes the following sections:
•
Information About the Python API
•
Installing Python
•
Installing Third Party Pure Python Packages
•
Using Python
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:
http://www.python.org/
The same site also contains distributions of and pointers to many free third-party Python modules, programs and tools, and additional documentation.
Note
You can know the latest development on Cisco Nexus 5000 Series switches and contribute your ideas by joining the community codesharing page:
https://github.com/datacenter
The Cisco Nexus 5000 series switches support all the features available in Python v2.7.2.
The Python scripting capability on the Cisco Nexus 5000 series switches enables you to perform the following tasks:
•
Run a script to verify configuration on switch bootup.
•
Back 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 on the Cisco NX-OS software.You can invoke Python by entering the python command, and write scripts to access Cisco NX-OS APIs by importing the cisco.py module using the import cisco.py 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 the future releases.
Using Python
This section describes how to write and execute Python scripts by passing parameters and includes the following topics:
•
Entering Python Shell
•
Executing Scripts
•
Passing Parameters to the Script
Entering Python Shell
You can enter the Python shell by using the python command without any parameters.
Python 2.7.2 (default, Oct 11 2011, 13:55:49)
[GCC 3.4.3 (MontaVista 3.4.3-25.0.143.0800417 2008-02-22)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Executing Scripts
You can execute a Python script by using the python <filename> command.
Passing Parameters to the Script
You can execute a Python script by using the python <filename> [arg1, arg2, arg3,.....] command.
switch# python test.py foo bar 1 2
['/bootflash/test.py', 'foo', 'bar', '1', '2']