{"id":7,"date":"2018-04-05T17:41:26","date_gmt":"2018-04-05T17:41:26","guid":{"rendered":"http:\/\/blog.cloud-blog.eu\/?p=7"},"modified":"2018-04-06T15:46:14","modified_gmt":"2018-04-06T15:46:14","slug":"nexus-python-script","status":"publish","type":"post","link":"http:\/\/blog.cloud-blog.eu\/?p=7","title":{"rendered":"Nexus Python script"},"content":{"rendered":"<p class=\"p1\">I thought, that the ability of running Python scripts on Nexus Switches is broadly know. BUT when talking to customers about this it\u2019s 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.<\/p>\n<p class=\"p1\">e.g. Nexus 7000 Python API description <a href=\"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\">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<\/a><\/p>\n<p class=\"p1\">When you call python, please do not forget to import cisco libary!<\/p>\n<p class=\"p1\">BB2-2# python<\/p>\n<p class=\"p1\">Python 2.7.2 (default, Nov 27 2012, 17:50:33)<\/p>\n<p class=\"p1\">[GCC 4.3.2] on linux2<\/p>\n<p class=\"p1\">Type &#8222;help&#8220;, &#8222;copyright&#8220;, &#8222;credits&#8220; or &#8222;license&#8220; for more information.<\/p>\n<p class=\"p1\">Loaded cisco NxOS lib!<\/p>\n<p class=\"p1\">&gt;&gt;&gt; import cisco<\/p>\n<p class=\"p1\">&gt;&gt;&gt; dir(cisco)<\/p>\n<p class=\"p1\">[&#8218;BGPSession&#8216;, &#8218;BufferDepthMonitor&#8216;, &#8218;CLI&#8216;, &#8218;CheckPortDiscards&#8216;, &#8218;CiscoSecret&#8216;, &#8218;CiscoSocket&#8216;, &#8218;Feature&#8216;, &#8218;History&#8216;, &#8218;IPv4ACL&#8216;, &#8218;IPv6ACL&#8216;, &#8218;Interface&#8216;, &#8218;Key&#8216;, &#8218;LineParser&#8216;, &#8218;MacAddressTable&#8216;, &#8218;OSPFSession&#8216;, &#8218;Routes&#8216;, &#8218;SectionParser&#8216;, &#8218;VRF&#8216;, &#8218;Vlan&#8216;, &#8218;__all__&#8216;, &#8218;__builtins__&#8216;, &#8218;__doc__&#8216;, &#8218;__file__&#8216;, &#8218;__name__&#8216;, &#8218;__package__&#8216;, &#8218;__path__&#8216;, &#8218;acl&#8216;, &#8218;bfd&#8216;, &#8218;bgp&#8216;, &#8218;buffer_depth_monitor&#8216;, &#8218;check_port_discards&#8216;, &#8218;cisco_secret&#8216;, &#8218;cisco_socket&#8216;, &#8218;cli&#8216;, &#8218;dhcp&#8216;, &#8218;eigrp&#8216;, &#8218;feature&#8216;, &#8218;get_global_vrf&#8216;, &#8218;get_valid_port&#8216;, &#8218;history&#8216;, &#8218;hsrp&#8216;, &#8218;interface&#8216;, &#8218;interface-vlan&#8216;, &#8218;key&#8216;, &#8218;lacp&#8216;, &#8218;line_parser&#8216;, &#8218;mac_address_table&#8216;, &#8218;md5sum&#8216;, &#8218;msdp&#8216;, &#8218;ospf&#8216;, &#8218;ospfv3&#8216;, &#8218;pim&#8216;, &#8218;private-vlan&#8216;, &#8218;ptp&#8216;, &#8218;rip&#8216;, &#8218;routes&#8216;, &#8217;scheduler&#8216;, &#8217;section_parser&#8216;, &#8217;set_global_vrf&#8216;, &#8217;show_queues&#8216;, &#8217;show_run&#8216;, &#8217;ssh&#8216;, &#8218;tacacs&#8216;, &#8218;telnet&#8216;, &#8218;transfer&#8216;, &#8218;udld&#8216;, &#8218;vlan&#8216;, &#8218;vpc&#8216;, &#8218;vrf&#8216;, &#8218;vrrp&#8216;, &#8218;vtp&#8216;]<\/p>\n<p class=\"p1\">&gt;&gt;&gt;<\/p>\n<p class=\"p1\">&gt;&gt;&gt; print help(cisco.get_valid_port)<\/p>\n<p class=\"p1\">Help on function get_valid_port in module cisco.interface:<\/p>\n<p class=\"p1\">get_valid_port(port)<\/p>\n<p class=\"p1\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>Validate and return correct port here<\/p>\n<p class=\"p1\">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.<\/p>\n<p class=\"p3\"><span class=\"s1\">oCli=CLI(<\/span><span class=\"s2\">&#8218;ping &#8218;<\/span><span class=\"s1\"> + ip + <\/span><span class=\"s2\">&#8218; vrf &#8218;<\/span><span class=\"s1\"> + VRF,False)<\/span><\/p>\n<p class=\"p3\"><span class=\"s1\">pingStatus = oCli.get_output()[<\/span><span class=\"s2\">8<\/span><span class=\"s1\">].split(<\/span><span class=\"s2\">&#8218; &#8218;<\/span><span class=\"s1\">)[<\/span><span class=\"s2\">6<\/span><span class=\"s1\">]<\/span><\/p>\n<p class=\"p3\"><span class=\"s1\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><\/span><span class=\"s3\">if<\/span><span class=\"s1\"> pingStatus == <\/span><span class=\"s4\">&#8222;0.00%&#8220;<\/span><span class=\"s1\">:<\/span><\/p>\n<p class=\"p3\"><span class=\"s1\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span>pingStatus = mac + <\/span><span class=\"s4\">&#8220; (&#8222;<\/span><span class=\"s1\"> + ip + <\/span><span class=\"s4\">&#8222;) is reachable&#8220;<\/span><\/p>\n<p class=\"p3\"><span class=\"s1\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><\/span><span class=\"s3\">elif<\/span><span class=\"s1\"> pingStatus == <\/span><span class=\"s4\">&#8222;100.00%&#8220;<\/span><span class=\"s1\">:<\/span><\/p>\n<p class=\"p3\"><span class=\"s1\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span>pingStatus = mac + <\/span><span class=\"s4\">&#8220; (&#8222;<\/span><span class=\"s1\"> + ip + <\/span><span class=\"s4\">&#8222;) is unreachable&#8220;<\/span><\/p>\n<p class=\"p4\"><span class=\"s1\">else<\/span><span class=\"s5\">:<\/span><\/p>\n<p class=\"p3\"><span class=\"s1\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span>pingStatus = mac + <\/span><span class=\"s4\">&#8220; (&#8222;<\/span><span class=\"s1\"> + ip + <\/span><span class=\"s4\">&#8222;) is not stable&#8220;<\/span><\/p>\n<p class=\"p6\"><span class=\"s1\">Happy scripting!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I thought, that the ability of running Python scripts on Nexus Switches is broadly know. BUT when talking to customers about this it\u2019s more or less not know.This is a pity because this gives you huge possibilities, e.g. Collecting data<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,2,8,6],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.cloud-blog.eu\/index.php?rest_route=\/wp\/v2\/posts\/7"}],"collection":[{"href":"http:\/\/blog.cloud-blog.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.cloud-blog.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.cloud-blog.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.cloud-blog.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7"}],"version-history":[{"count":2,"href":"http:\/\/blog.cloud-blog.eu\/index.php?rest_route=\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":9,"href":"http:\/\/blog.cloud-blog.eu\/index.php?rest_route=\/wp\/v2\/posts\/7\/revisions\/9"}],"wp:attachment":[{"href":"http:\/\/blog.cloud-blog.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.cloud-blog.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.cloud-blog.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}