<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: ping doesn't work in python script running on Nexus in Tools</title>
    <link>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4484198#M635</link>
    <description>&lt;P&gt;I have tried cli and clip which both are working but i cannot do error handling. I have tried the try-except and the below but nothing worked&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;response = clip('ping 192.168.1.1')&lt;BR /&gt;if response == 0:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; print(hostname, 'is up')&lt;BR /&gt;else:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; print(hostname, 'is down')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think that the error handling isn't working because the ping command is running on NexusOS and it's not a native python command.&lt;/P&gt;</description>
    <pubDate>Tue, 12 Oct 2021 06:58:37 GMT</pubDate>
    <dc:creator>anousakisioannis</dc:creator>
    <dc:date>2021-10-12T06:58:37Z</dc:date>
    <item>
      <title>ping doesn't work in python script running on Nexus</title>
      <link>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4483522#M629</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I try to create a python script and schedule it to run every 5 minutes on Nexus switches.&lt;/P&gt;&lt;P&gt;One of the checks, it will be the output of a simple ping.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried different ways to execute a ping from python script but nothing works.&lt;/P&gt;&lt;P&gt;When i run the script from my laptop (Windows 10), it executes the ping command without any issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;import &lt;SPAN class="hljs-built_in"&gt;os&lt;/SPAN&gt;

hostnames = [
    &lt;SPAN class="hljs-string"&gt;'192.168.1.1'&lt;/SPAN&gt;
]

&lt;SPAN class="hljs-keyword"&gt;for&lt;/SPAN&gt; hostname &lt;SPAN class="hljs-keyword"&gt;in&lt;/SPAN&gt; hostnames:
    response = &lt;SPAN class="hljs-built_in"&gt;os&lt;/SPAN&gt;.system(&lt;SPAN class="hljs-string"&gt;'ping '&lt;/SPAN&gt; + hostname)
    &lt;SPAN class="hljs-keyword"&gt;if&lt;/SPAN&gt; response == &lt;SPAN class="hljs-number"&gt;0&lt;/SPAN&gt;:
        &lt;SPAN class="hljs-built_in"&gt;print&lt;/SPAN&gt;(hostname, &lt;SPAN class="hljs-string"&gt;'is up'&lt;/SPAN&gt;)
    &lt;SPAN class="hljs-keyword"&gt;else&lt;/SPAN&gt;:
        &lt;SPAN class="hljs-built_in"&gt;print&lt;/SPAN&gt;(hostname, &lt;SPAN class="hljs-string"&gt;'is down'&lt;/SPAN&gt;)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Nexus# &amp;gt;&amp;gt;&amp;gt; os.system('ping 192.168.1.1')&lt;BR /&gt;system(ping 192.168.1.1): rejected!&lt;BR /&gt;-1&lt;BR /&gt;Nexus# &amp;gt;&amp;gt;&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please let me know if there is any way to run a ping on Nexus using python?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 11:26:27 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4483522#M629</guid>
      <dc:creator>anousakisioannis</dc:creator>
      <dc:date>2021-10-11T11:26:27Z</dc:date>
    </item>
    <item>
      <title>Re: ping doesn't work in python script running on Nexus</title>
      <link>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4483602#M630</link>
      <description>&lt;P&gt;how are you running this script in nexus device ?&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 13:45:47 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4483602#M630</guid>
      <dc:creator>balaji.bandi</dc:creator>
      <dc:date>2021-10-11T13:45:47Z</dc:date>
    </item>
    <item>
      <title>Re: ping doesn't work in python script running on Nexus</title>
      <link>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4483608#M631</link>
      <description>&lt;P&gt;I have tried to execute the script command by command in python shell or with the &lt;STRONG&gt;source&lt;/STRONG&gt; command from Nexus (e.g. source version.py)&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 13:49:01 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4483608#M631</guid>
      <dc:creator>anousakisioannis</dc:creator>
      <dc:date>2021-10-11T13:49:01Z</dc:date>
    </item>
    <item>
      <title>Re: ping doesn't work in python script running on Nexus</title>
      <link>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4483620#M632</link>
      <description>&lt;P&gt;&lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/480045"&gt;@anousakisioannis&lt;/a&gt;&amp;nbsp;try this example --&amp;gt;&amp;nbsp;&lt;A href="https://developer.cisco.com/docs/nx-os/#!troubleshooting/ping" target="_blank"&gt;https://developer.cisco.com/docs/nx-os/#!troubleshooting/ping&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 13:58:28 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4483620#M632</guid>
      <dc:creator>bigevilbeard</dc:creator>
      <dc:date>2021-10-11T13:58:28Z</dc:date>
    </item>
    <item>
      <title>Re: ping doesn't work in python script running on Nexus</title>
      <link>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4483632#M633</link>
      <description>&lt;P&gt;I copied the script as it is in the link and i get the below errors&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Nexus# source test2.py&lt;BR /&gt;ERROR: ld.so: object '/isan/lib/libsandbox.so' from LD_PRELOAD cannot be preloaded: ignored.&lt;BR /&gt;no sandbox&lt;BR /&gt;disable sandbox before enabled&lt;BR /&gt;no sandbox&lt;BR /&gt;Bareword found where operator expected at (eval 1) line 6, near "'172.31.219.60'&lt;BR /&gt;count"&lt;BR /&gt;(Missing operator before count?)&lt;BR /&gt;Bareword found where operator expected at (eval 1) line 7, near "'4'&lt;BR /&gt;vrf"&lt;BR /&gt;(Missing operator before vrf?)&lt;BR /&gt;Bareword found where operator expected at (eval 1) line 10, near "'management'&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;def"&lt;BR /&gt;(Missing operator before def?)&lt;BR /&gt;String found where operator expected at (eval 1) line 13, near """"&lt;BR /&gt;Checks if the host is reachable&lt;BR /&gt;""&lt;BR /&gt;(Might be a runaway multi-line "" string starting on line 11)&lt;BR /&gt;(Missing operator before "&lt;BR /&gt;Checks if the host is reachable&lt;BR /&gt;"?)&lt;BR /&gt;String found where operator expected at (eval 1) line 13, near ""&lt;BR /&gt;Checks if the host is reachable&lt;BR /&gt;""""&lt;BR /&gt;(Missing operator before ""?)&lt;BR /&gt;Bareword found where operator expected at (eval 1) line 14, near """&lt;BR /&gt;try"&lt;BR /&gt;(Missing operator before try?)&lt;BR /&gt;Bareword found where operator expected at (eval 1) line 24, near ")&lt;BR /&gt;except"&lt;BR /&gt;(Missing operator before except?)&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 14:12:34 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4483632#M633</guid>
      <dc:creator>anousakisioannis</dc:creator>
      <dc:date>2021-10-11T14:12:34Z</dc:date>
    </item>
    <item>
      <title>Re: ping doesn't work in python script running on Nexus</title>
      <link>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4483740#M634</link>
      <description>&lt;P&gt;&lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/480045"&gt;@anousakisioannis&lt;/a&gt;&amp;nbsp;you can also try&amp;nbsp;Using the CLI Command APIs&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.cisco.com/c/en/us/td/docs/switches/datacenter/nexus9000/sw/7-x/programmability/guide/b_Cisco_Nexus_9000_Series_NX-OS_Programmability_Guide_7x/Python_API.html#concept_6414B967674E46A5B13EC9CF61C88880" target="_blank"&gt;https://www.cisco.com/c/en/us/td/docs/switches/datacenter/nexus9000/sw/7-x/programmability/guide/b_Cisco_Nexus_9000_Series_NX-OS_Programmability_Guide_7x/Python_API.html#concept_6414B967674E46A5B13EC9CF61C88880&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 15:20:17 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4483740#M634</guid>
      <dc:creator>bigevilbeard</dc:creator>
      <dc:date>2021-10-11T15:20:17Z</dc:date>
    </item>
    <item>
      <title>Re: ping doesn't work in python script running on Nexus</title>
      <link>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4484198#M635</link>
      <description>&lt;P&gt;I have tried cli and clip which both are working but i cannot do error handling. I have tried the try-except and the below but nothing worked&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;response = clip('ping 192.168.1.1')&lt;BR /&gt;if response == 0:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; print(hostname, 'is up')&lt;BR /&gt;else:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; print(hostname, 'is down')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think that the error handling isn't working because the ping command is running on NexusOS and it's not a native python command.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Oct 2021 06:58:37 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4484198#M635</guid>
      <dc:creator>anousakisioannis</dc:creator>
      <dc:date>2021-10-12T06:58:37Z</dc:date>
    </item>
    <item>
      <title>Re: ping doesn't work in python script running on Nexus</title>
      <link>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4522409#M645</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/480045"&gt;@anousakisioannis&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;I ran through the ping script &lt;A title="Troubleshooting NX-OS ping" href="https://developer.cisco.com/docs/nx-os/#!troubleshooting/ping" target="_blank" rel="noopener"&gt;here&lt;/A&gt; and was able to get it to run successfully.&lt;/P&gt;&lt;P&gt;Note that I didn't want to step on anyone's script, so I used two underscores when I named it: &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;&lt;EM&gt;test__ping.py&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;sandbox-nxos-1.cisco# &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;show run | in bash&lt;/STRONG&gt;&lt;/FONT&gt;
feature bash-shell
sandbox-nxos-1.cisco# &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;run bash&lt;/STRONG&gt;&lt;/FONT&gt;
bash-4.3$ &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;sudo su&lt;/STRONG&gt;&lt;/FONT&gt;
bash-4.3# &lt;BR /&gt;
bash-4.3# &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;vi /isan/python/scripts/cisco/test__ping.p&lt;/STRONG&gt;&lt;/FONT&gt;y
from .nxcli import NXCLI
import traceback

# Fill the details to troubleshoot using ping
host = '172.31.219.60'
count = '4'
vrf = 'management'


def check_ping_ops():
    """     
    Checks if the host is reachable
    """                     
    try:                            
        interface = NXCLI('ping %s count %s vrf %s' % (host, count, vrf))
        return True                                                     
    except:                                                                 
        return False                                                                            
    

if __name__=="__main__":
    try:
        check_ping_ops()
    except Exception,e:
        traceback.print_exc()
~
~
~
~
~
~
~
~
~
~
~
~
"/isan/python/scripts/cisco/test__ping.py" [New] 25L, 470C written
bash-4.3#&lt;/PRE&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;FONT color="#000000"&gt;Here's the running script:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;bash-4.3# &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;/isan/bin/python -m cisco.test__pi&lt;/STRONG&gt;&lt;/FONT&gt;ng
PING 172.31.219.60 (172.31.219.60): 56 data bytes
Request 0 timed out
Request 1 timed out
Request 2 timed out
Request 3 timed out

--- 172.31.219.60 ping statistics ---
4 packets transmitted, 0 packets received, 100.00% packet loss

bash-4.3# &lt;/PRE&gt;&lt;P&gt;I then modified the script so that a successful ping could also be demonstrated:&lt;/P&gt;&lt;PRE&gt;bash-4.3# &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;cat /isan/python/scripts/cisco/test__ping.py&lt;/STRONG&gt;&lt;/FONT&gt; 
from .nxcli import NXCLI
import traceback

# Fill the details to troubleshoot using ping
count = '4'
vrf = 'management'

hostinfo = [
    ('172.31.219.60', vrf),
    ('192.168.2.1', 'default')
]


def check_ping_ops(host, vrf):
    """
    Checks if the host is reachable
    """
    try:
        interface = NXCLI('ping %s count %s vrf %s' % (host, count, vrf))
        return True
    except:
        return False


if __name__=="__main__":
    try:
        for info in hostinfo:
            host, vrf = info[0], info[1]
            check_ping_ops(host, vrf)
    except Exception,e:
        traceback.print_exc()&lt;/PRE&gt;&lt;P&gt;Here's the output:&lt;/P&gt;&lt;PRE&gt;bash-4.3# &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;/isan/bin/python -m cisco.test__ping&lt;/STRONG&gt;&lt;/FONT&gt;
PING 172.31.219.60 (172.31.219.60): 56 data bytes
Request 0 timed out
Request 1 timed out
Request 2 timed out
Request 3 timed out

--- 172.31.219.60 ping statistics ---
4 packets transmitted, 0 packets received, 100.00% packet loss

PING 192.168.2.1 (192.168.2.1): 56 data bytes
64 bytes from 192.168.2.1: icmp_seq=0 ttl=255 time=0.437 ms
64 bytes from 192.168.2.1: icmp_seq=1 ttl=255 time=0.366 ms
64 bytes from 192.168.2.1: icmp_seq=2 ttl=255 time=0.291 ms
64 bytes from 192.168.2.1: icmp_seq=3 ttl=255 time=0.276 ms

--- 192.168.2.1 ping statistics ---
4 packets transmitted, 4 packets received, 0.00% packet loss
round-trip min/avg/max = 0.276/0.342/0.437 ms

bash-4.3# &lt;/PRE&gt;&lt;P&gt;fjm&lt;/P&gt;&lt;P&gt;@ittybittypacket&lt;/P&gt;</description>
      <pubDate>Tue, 21 Dec 2021 23:06:24 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4522409#M645</guid>
      <dc:creator>fracjackmac</dc:creator>
      <dc:date>2021-12-21T23:06:24Z</dc:date>
    </item>
    <item>
      <title>Re: ping doesn't work in python script running on Nexus</title>
      <link>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4522433#M646</link>
      <description>&lt;P&gt;I made additional modifications to the script to hide the ping command output and provide an "up" or "down" indication.&lt;/P&gt;&lt;PRE&gt;bash-4.3# &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;cat /isan/python/scripts/cisco/test__ping.py&lt;/STRONG&gt;&lt;/FONT&gt; 
from .nxcli import NXCLI
import traceback

# Fill the details to troubleshoot using ping
count = '4'
vrf = 'management'

hostinfo = [
    ('172.31.219.60', vrf),
    ('192.168.2.1', 'default')
]


def check_ping_ops(host, vrf, do_print=False):
    """
    Checks if the host is reachable
    """
    try:
        interface = NXCLI('ping %s count %s vrf %s' % (host, count, vrf), do_print)
        for feedback in interface.get_output():
            if &lt;STRONG&gt;'100.00% packet loss'&lt;/STRONG&gt; in feedback:
                return False
        return True
    except:
        return False


if __name__=="__main__":
    try:
        for info in hostinfo:
            host, vrf = info[0], info[1]
            response = check_ping_ops(host, vrf)
            if response:
                print "\nHost {} is currently up / alive\n".format(host)
            else:
                print "\nHost {} is currently down / unreachable\n".format(host)
    except Exception,e:
        traceback.print_exc()
bash-4.3# 

bash-4.3# &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;/isan/bin/python -m cisco.test__ping&lt;/STRONG&gt;&lt;/FONT&gt;

Host 172.31.219.60 is currently down / unreachable


Host 192.168.2.1 is currently up / alive

bash-4.3# &lt;/PRE&gt;&lt;P&gt;Note that I used the phrase "&lt;EM&gt;&lt;STRONG&gt;100.00% packet loss&lt;/STRONG&gt;&lt;/EM&gt;" as the key to determine whether or not a test was considered successful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;fjm&lt;/P&gt;&lt;P&gt;@ittybittypacket&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2021 00:22:45 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/ping-doesn-t-work-in-python-script-running-on-nexus/m-p/4522433#M646</guid>
      <dc:creator>fracjackmac</dc:creator>
      <dc:date>2021-12-22T00:22:45Z</dc:date>
    </item>
  </channel>
</rss>

