Cisco Nexus Python Scripting

I thought, that the ability of running Python scripts on Nexus Switches is broadly know. BUT when talking to customers about this, you figure out that it’s more or less unknow.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 6000, Nexus 7000.

e.g. Nexus 7000 Python API description

When you call python, please do not forget to import cisco library!

Nexus6000# 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!

 

Leave a Reply

Your email address will not be published. Required fields are marked *