In Open NX-OS, the Python programming language is supported directly on the device. An interactive Python interpreter is available just by typing python at the CLI prompt:
n9k-sw-1# python
Python 2.7.5 (default, Oct 8 2013, 23:59:43)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
The Python interpreter on the Open NX-OS platform can also interpret script files directly, in this case the Python interpreter is running "non-interactively". To run non-interactively, users can enter the name of the script to be run after the python command at the CLI prompt. Here is an example run of the script hello_world.py
n9k-sw-1# cd bootflash:
n9k-sw-1# python hello_world.py
hello world!
n9k-sw-1# show file hello_world.py
#!/usr/bin/python
print "hello world!"
n9k-sw-1#
For more information about Python, see Programmability Tools for Network Engineers.
Cisco provides a set of helper library packages designed specifically for Python. These packages are pre-installed on the NX-OS platform and are named "cli" and "cisco".
The cli Python package is used to allow Python scripts running on the Open NX-OS device to interact with the CLI to get and set configuration on the device. This library has one function within it named cli. The input parameters to the function are the CLI commands the user desires to run, the output is a string representing the parser output from the CLI command.
Here is an example of using the CLI library to gather hardware version information:
import cli
result = cli.cli("show version | beg Hardware")
print result
Running the example in Python results in the following output:
n9k-sw-1# python get_hardware.py
Hardware
cisco Nexus9000 C9396TX Chassis
Intel(R) Core(TM) i3-3227U CPU @ 2.50GHz with 16402136 kB of memory.
Processor Board ID SAL18370NXE
Kernel uptime is 98 day(s), 21 hour(s), 16 minute(s), 43 second(s)
Last reset at 180873 usecs after Tue Jun 23 18:15:50 2015
plugin
Core Plugin, Ethernet Plugin
Active Package(s):
The cli package will accept both show commands and configuration commands in the cli function.
In addition to the cli package, a "cisco" package is provided. The cisco package provides the following functionality:
Function |
Description |
acl |
Adds the ability to create, delete, and modify Access Control Lists |
bgp |
Functions around configuring BGP |
cisco_secret |
Adjust Cisco passwords |
feature |
Get information about supported and enabled features on NX-OS |
interface |
Functions around manipulating interfaces |
nxcli |
Contains useful CLI interaction utilities |
ospf |
Functions around configuring OSPF |
ssh |
Generate SSH key information |
tacacs |
Runs and parsers TACACS+ status information |
vrf |
Creates and deletes virtual routing and forwarding (VRF) tables |
The following code sample uses the cisco package to change the state of all interfaces to up.
import cisco
tp = cisco.Interface.interfaces()
for tpint in tp:
intf = cisco.Interface(tpint)
intf.set_state()
The first line above imports the cisco package into the Python script so functions within the package can be used. A for
loop is then used to loop through each interface and set_state(). set_state with no options will default to setting the state of the interface to up.