How to check if a node is up/online?
<p>I want my script to [ssh] remote login to a different machine to do something but before that I want to check: that node is not the localhost and the node is online, otherwise exit. What's the best [easiest??] way of doing that? Thanks for your advise in advance. Cheers!!
<hr>
<strong>Update-1 @Jakob: Error message</strong></p>

<p>Am I doing something wrong? Running the script, I get this:  </p>

<pre><code>File "./pingTest.py", line 9, in ?
    ping('10.0.11.20')
  File "./pingTest.py", line 5, in ping
    process = subprocess.Popen('ping %s' % target, stdout = subprocess.PIPE)
  File "/usr/lib64/python2.4/subprocess.py", line 550, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.4/subprocess.py", line 996, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
</code></pre>

<p>The very same thing happens on v2.6 as well. Cheers!!</p>

<p><hr>
<strong>Update-2 @Jakob: Still not working</strong></p>

<p>So, this is the script now, with some minor modification: </p>

<pre><code>#!/usr/bin/env python

import sys, subprocess
def ping(target):
    process = subprocess.Popen('ping -c 1 -A %s' % target, stdout = subprocess.PIPE, shell = True)
    results = process.communicate()[0]
    print(results)
    #return "Reply from %s" % target in results
    print "Reply from %s" % target in results

ping(sys.argv[1])
</code></pre>

<p>I changed the <code>ping</code> statement a little bit, so that it sends ECHO_REQUEST packet just once. Also changed the <code>return</code> to <code>print</code> to get some information on the screen. And, this is what I get when I run the script.</p>

<pre><code>$ ./pingTest.py 10.0.11.1
PING 10.0.11.1 (10.0.11.1) 56(84) bytes of data.
64 bytes from 10.0.11.1: icmp_seq=1 ttl=64 time=0.665 ms

--- 10.0.11.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.665/0.665/0.665/0.000 ms

False

$ ./pingTest.py 10.0.11.2
PING 10.0.11.2 (10.0.11.2) 56(84) bytes of data.
From 10.0.11.20 icmp_seq=1 Destination Host Unreachable

--- 10.0.11.2 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms


False
</code></pre>

<p>10.0.11.1 is an up and running host (as you can see from the first print statement) but it's saying <code>False</code> - am I actually doing it right?</p>

<p>If I actually print the <code>results</code> this way:</p>

<pre><code>for tx in results:
    print "Result: ", tx
</code></pre>

<p>I get the <code>Result</code> like this:</p>

<pre><code>Result:  P
Result:  I
Result:  N
Result:  G
Result:   
Result:  1
Result:  0
......
......
</code></pre>

<p>So, it's actually doing rather something else (??). Or maybe, I didn't able to follow your script at all. Any comment or suggestion? Cheers!!</p>
