The documentation set for this product strives to use bias-free language. For the purposes of this documentation set, bias-free is defined as language that does not imply discrimination based on age, disability, gender, racial identity, ethnic identity, sexual orientation, socioeconomic status, and intersectionality. Exceptions may be present in the documentation due to language that is hardcoded in the user interfaces of the product software, language used based on RFP documentation, or language that is used by a referenced third-party product. Learn more about how Cisco is using Inclusive Language.
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 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 code sharing page: https://github.com/datacenter |
The Cisco Nexus 5000 series switches with Releases 5.2(1)N1(1) and later and the Cisco Nexus 6000 series switches with Releases 6.0(2)N1(1)and later, support all the features available in Python v2.7.2.
The Python scripting capability on Cisco Nexus 5000 and Cisco Nexus 6000 series switches enables you to perform the following tasks:
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 command.
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:
switch# python >>> import mypkg
![]() Note | You will be able to install the third party packages using the easy_install command, in the future releases. |
This section describes how to write and execute Python scripts by passing parameters and includes the following topics:
You can enter the Python shell by using the python command without any parameters.
switch# python 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. Loaded cisco NX-OS lib! >>> print 'helo world!' helo world! >>>exit() switch#
For NX-OS releases earlier than 7.0N, you can execute a Python script by using the python <filename> command.
switch# python test.py ['/bootflash/test.py'] doing 0/1 doing 0/2 doing 1/2 switch#
For NX-OS releases 7.0N and later, you can execute a Python script by using the source <filename> command.
switch# source test.py ['/bootflash/test.py'] doing 0/1 doing 0/2 doing 1/2 switch#
For NX-OS releases earlier than 7.0N, you can pass parameters to 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'] doing 0/1 doing 0/2 doing 1/2 switch#
For NX-OS releases 7.0N and later, you can pass parameters to a Python script by using the source <filename> [arg1, arg2, arg3,.....] command.
switch# source test.py foo bar 1 2 ['/bootflash/test.py', 'foo', 'bar', '1', '2'] doing 0/1 doing 0/2 doing 1/2 switch#
This chapter provides information about the following Python Application Programming Interface (API) functions. This chapter includes the following sections:
Routes() - Class Object
Routes()
Instantiates an object of the Routes class.
None.
An object of Routes class.
rObj = Routes()
show_arp_table()
Routes.show_arp_table()
Executes the show ip arp command and returns the output.
None.
Returns the ARP table entries on the switch.
Example
routeObj = Routes() data = routeObj.show_arp_table().get_output()
Flags: D - Static Adjacencies attached to down interface IP ARP Table for context default Total number of entries: 4 Address Age MAC Address Interface 50.1.201.2 00:02:10 547f.ee40.5a7c Vlan201 50.1.1.10 00:07:53 547f.ee62.f801 Ethernet1/34 50.1.2.10 00:08:31 547f.ee62.f801 Ethernet1/35 50.1.3.10 00:08:31 547f.ee62.f801 Ethernet1/35.1 <cisco.CLI object at 0xb7c1462c>
show_vsh_routes()
Routes.show_vsh_routes()
Executes the show ip fib route and returns the output.
None.
Returns the software route entries.
Example
routeObj = Routes() data = routeObj.show_vsh_routes().get_output()
IPv4 routes for table default/base ------------------+------------------+--------------------- Prefix | Next-hop | Interface ------------------+------------------+--------------------- 0.0.0.0/32 Drop Null0 50.1.1.0/24 Attached Ethernet1/34 50.1.1.0/32 Drop Null0 50.1.1.10/32 50.1.1.10 Ethernet1/34 50.1.1.100/32 Receive sup-eth1 50.1.1.255/32 Attached Ethernet1/34 50.1.2.0/24 Attached Ethernet1/35 50.1.2.0/32 Drop Null0 50.1.2.10/32 50.1.2.10 Ethernet1/35 50.1.2.100/32 Receive sup-eth1 50.1.2.255/32 Attached Ethernet1/35 50.1.3.0/24 Attached Ethernet1/35.1 50.1.3.0/32 Drop Null0 50.1.3.10/32 50.1.3.10 Ethernet1/35.1 50.1.3.100/32 Receive sup-eth1 50.1.3.255/32 Attached Ethernet1/35.1 <cisco.CLI object at 0xb7b0a6ac>
show_hw_routes()
Routes.show_hw_routes()
Computes the hardware routes and returns the output.
None.
Returns the hardware route entries.
Example
routeObj = Routes() data = routeObj.show_hw_routes()
------------------+------------------+--------------------- Prefix | Next-hop | Interface ------------------+------------------+--------------------- 50.1.1.100/32 Receive sup-eth1 50.1.2.100/32 Receive sup-eth1 50.1.201.1/32 Receive sup-eth1 0.0.0.0/32 Drop Null0 50.1.3.0/32 Drop Null0 50.1.201.0/32 Drop Null0 50.1.2.255/32 Attached sup-hi 50.1.1.255/32 Attached sup-hi 60.1.1.0/32 Drop Null0 50.1.3.255/32 Attached sup-hi 50.1.201.255/32 Attached sup-hi 255.255.255.255/32 Receive sup-eth1
verify_routes()
Routes.verify_routes()
Verifies the software and hardware routes.
None.
Returns the number of routes matched and unmatched between hardware and software.
Example
routeObj = Routes() found,nfound = routeObj.verify_routes()
Routes verified and found: 26 Routes not found: 50.1.205.0/24 3 51.1.1.0/24 3 51.1.2.0/24 4 51.1.3.0/24 6 100.1.1.0/24 7 100.1.2.0/24 7 100.1.3.0/24 7 101.1.1.0/24 7 101.1.2.0/24 7 101.1.3.0/24 7 120.1.1.0/24 7
verify_arp_table()
Routes.verify_arp_table()
Verifies the software and hardware ARP table entries.
None.
Returns the number of ARP table entries matched and unmatched between hardware and software.
Example
routeObj = Routes() found,notfound = routeObj.verify_arp_table()
Flags: D - Static Adjacencies attached to down interface IP ARP Table for context default Total number of entries: 4 Address Age MAC Address Interface 50.1.201.2 00:02:31 547f.ee40.5a7c Vlan201 50.1.1.10 00:08:15 547f.ee62.f801 Ethernet1/34 50.1.2.10 00:08:53 547f.ee62.f801 Ethernet1/35 50.1.3.10 00:08:53 547f.ee62.f801 Ethernet1/35.1 mac address:54:7f:ee:40:5a:7c Arp entry for 50.1.201.2 547f.ee40.5a7c Vlan201 found in HW mac address:54:7f:ee:62:f8:01 Arp entry for 50.1.1.10 547f.ee62.f801 Ethernet1/34 found in HW mac address:54:7f:ee:62:f8:01 Arp entry for 50.1.2.10 547f.ee62.f801 Ethernet1/35 found in HW mac address:54:7f:ee:62:f8:01 Arp entry for 50.1.3.10 547f.ee62.f801 Ethernet1/35.1 found in HW
CheckPortDiscards(<port>)
CheckPortDiscards('ethernet1/1')
Check the input discards for given port. If discard is more than 0, query and print the discard reason from broadcom.
port
None.
Example
c = CheckPortDiscards('eth1/1')
Ethernet1/1 is up Hardware: 100/1000/10000 Ethernet, address: 547f.ee57.dd28 (bia 547f.ee57.dd28) MTU 1500 bytes, BW 10000000 Kbit, DLY 10 usec, reliability 255/255, txload 1/255, rxload 1/255 Encapsulation ARPA Port mode is trunk full-duplex, 10 Gb/s, media type is 10G Beacon is turned off Input flow-control is off, output flow-control is off Rate mode is dedicated Switchport monitor is off EtherType is 0x8100 Last link flapped 00:42:16 Last clearing of "show interface" counters never 30 seconds input rate 5016 bits/sec, 627 bytes/sec, 6 packets/sec 30 seconds output rate 3232 bits/sec, 404 bytes/sec, 5 packets/sec Load-Interval #2: 5 minute (300 seconds) input rate 4.69 Kbps, 7 pps; output rate 2.82 Kbps, 4 pps RX 297 unicast packets 20588 multicast packets 5 broadcast packets 20890 input packets 1848701 bytes 0 jumbo packets 0 storm suppression packets 0 giants 0 input error 0 short frame 0 overrun 0 underrun 0 watchdog 0 if down drop 0 input with dribble 0 input discard(includes ACL drops) 0 Rx pause TX 262 unicast packets 16151 multicast packets 5 broadcast packets 16418 output packets 1407200 bytes 0 jumbo packets 0 output errors 0 collision 0 deferred 0 late collision 0 lost carrier 0 no carrier 0 babble 0 Tx pause 2 interface resets zero discards
transfer()
transfer (<protocol>, <host>, <source>, <dest>, <vrf>, <login_timeout>, <user>, <password>)
API to transfer file specified in <source> from <host> to the path mentioned in <dest> using <protocol>. Protocol can be scp, tftp, ftp or sftp.
protocol, host, source, dest, vrf, login_timeout, user, password.
Returns True if transfer was successful.
Transfer using scp:
c = transfer("scp", "10.193.190.100", "/tftpboot/transfer_test_image", "transfer_test_image", user="scpUser", password="scpPasswd")
Transfer using sftp:
c = transfer("sftp", "10.193.190.100", "/tftpboot/transfer_test_image", "transfer_test_image", user="sftpUser", password="sftpPasswd")
Transfer using tftp:
c = transfer("tftp", "10.193.190.100", "/transfer_test_image", "transfer_test_image", user="", password="")
Transfer using ftp:
c = transfer("ftp", "10.193.190.51", "golden/home/su-ash/transfer_test_image", "transfer_test_image", user="ftpUser", password="ftpPasswd")
CLI() - Class Object
CLI (<command>, <do_print>)
Instantiates an object of the CLI class with the CLI command specified in <command>. <do_print> when set to False does not print the output of the command and prints the output when set to True, which is the default.
![]() Note | The CLI command name is case sensitive (uppercase). |
command, do_print
An object of CLI class.
c = CLI ('show runn inter eth1/1')
!Command: show running-config interface Ethernet1/1 !Time: Mon Feb 27 14:33:24 2012 version 5.0(3)U3(1) interface Ethernet1/1 switchport mode trunk udld enable channel-group 12 <cisco.CLI object at 0xb7ae948c>
get_output()
CLI.get_output()
Returns the output of the CLI command.
None.
Output of the CLI command.
Example
c = CLI ('show runn inter eth1/1') c.get_output()
['', '!Command: show running-config interface Ethernet1/1', '!Time: Mon Feb 27 14:36:10 2012', '', 'version 5.0(3)U3(1)', '', 'interface Ethernet1/1', ' switchport mode trunk', ' udld enable', ' channel-group 12', '', '']
rerun()
CLI.rerun()
Reruns the command.
None.
None.
Example
c = CLI ('show runn inter eth1/1') c.rerun()
!Command: show running-config interface Ethernet1/1 !Time: Mon Feb 27 14:37:05 2012 version 5.0(3)U3(1) interface Ethernet1/1 switchport mode trunk udld enable channel-group 12
History() - Class Object
History()
Instantiates an object of the History class.
None.
An object of History class.
Example
a = History()
get_history()
History.get_history()
Gets the history of CLI commands executed so far.
None.
Returns the history of commands executed.
Example
a = History() a.get_history()
clear_history()
History.clear_history()
Clears history.
None.
None.
Example
a = History() a.clear_history()