I thought, that the ability of running Python scripts on Nexus Switches is broadly know. BUT when talking to customers about this it’s more or less not know.This is a pity because this gives you huge possibilities, e.g. Collecting data for statistics or trouble shooting. Python is supported on Nexus 3000, Nexus 5500, Nexus 5600, Nexus 6000, Nexus 7000 and Nexus 9000.
e.g. Nexus 7000 Python API description http://www.cisco.com/en/US/partner/docs/switches/datacenter/sw/6_x/nx-os/fundamentals/configuration/guide/b_Cisco_Nexus_7000_Series_NX-OS_Fundamentals_Configuration_Guide_Release_6.x_chapter_01011.html
When you call python, please do not forget to import cisco libary!
BB2-2# python
Python 2.7.2 (default, Nov 27 2012, 17:50:33)
[GCC 4.3.2] on linux2
Type „help“, „copyright“, „credits“ or „license“ for more information.
Loaded cisco NxOS lib!
>>> import cisco
>>> dir(cisco)
[‚BGPSession‘, ‚BufferDepthMonitor‘, ‚CLI‘, ‚CheckPortDiscards‘, ‚CiscoSecret‘, ‚CiscoSocket‘, ‚Feature‘, ‚History‘, ‚IPv4ACL‘, ‚IPv6ACL‘, ‚Interface‘, ‚Key‘, ‚LineParser‘, ‚MacAddressTable‘, ‚OSPFSession‘, ‚Routes‘, ‚SectionParser‘, ‚VRF‘, ‚Vlan‘, ‚__all__‘, ‚__builtins__‘, ‚__doc__‘, ‚__file__‘, ‚__name__‘, ‚__package__‘, ‚__path__‘, ‚acl‘, ‚bfd‘, ‚bgp‘, ‚buffer_depth_monitor‘, ‚check_port_discards‘, ‚cisco_secret‘, ‚cisco_socket‘, ‚cli‘, ‚dhcp‘, ‚eigrp‘, ‚feature‘, ‚get_global_vrf‘, ‚get_valid_port‘, ‚history‘, ‚hsrp‘, ‚interface‘, ‚interface-vlan‘, ‚key‘, ‚lacp‘, ‚line_parser‘, ‚mac_address_table‘, ‚md5sum‘, ‚msdp‘, ‚ospf‘, ‚ospfv3‘, ‚pim‘, ‚private-vlan‘, ‚ptp‘, ‚rip‘, ‚routes‘, ’scheduler‘, ’section_parser‘, ’set_global_vrf‘, ’show_queues‘, ’show_run‘, ’ssh‘, ‚tacacs‘, ‚telnet‘, ‚transfer‘, ‚udld‘, ‚vlan‘, ‚vpc‘, ‚vrf‘, ‚vrrp‘, ‚vtp‘]
>>>
>>> print help(cisco.get_valid_port)
Help on function get_valid_port in module cisco.interface:
get_valid_port(port)
Validate and return correct port here
As you see there are a lot of builtin functions, also there is a nice possibility to execute CLI command and work with the output. Here you see an example how to ping an IP and parse the output.
oCli=CLI(‚ping ‚ + ip + ‚ vrf ‚ + VRF,False)
pingStatus = oCli.get_output()[8].split(‚ ‚)[6]
if pingStatus == „0.00%“:
pingStatus = mac + “ („ + ip + „) is reachable“
elif pingStatus == „100.00%“:
pingStatus = mac + “ („ + ip + „) is unreachable“
else:
pingStatus = mac + “ („ + ip + „) is not stable“
Happy scripting!