0

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


Update-1 @Jakob: Error message

Am I doing something wrong? Running the script, I get this:

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

The very same thing happens on v2.6 as well. Cheers!!


Update-2 @Jakob: Still not working

So, this is the script now, with some minor modification:

#!/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])

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

$ ./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

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

If I actually print the results this way:

for tx in results:
    print "Result: ", tx

I get the Result like this:

Result:  P
Result:  I
Result:  N
Result:  G
Result:   
Result:  1
Result:  0
......
......

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

MacUsers
  • 2,091
  • 3
  • 35
  • 56
  • possible duplicate of [Ping a site in Python?](http://stackoverflow.com/questions/316866/ping-a-site-in-python) – S.Lott Mar 08 '11 at 10:53
  • 1
    I'd recommend against ping. I know quite some hosts blocking ICMP packets, but allowing SSH connections. Just try to connect -- it will fail if the host is down. – Sven Marnach Mar 08 '11 at 10:55
  • @S.Lott: already looked in the post you mention - too many suggestions to get easily confused. And another thing, as Sven said, ping could be blocked as well. Cheers!! – MacUsers Mar 08 '11 at 11:04
  • @Sven: What I actually want is not to execute ssh command at all in the first place if there is no response. Cheers!! – MacUsers Mar 08 '11 at 11:16
  • @MacUsers: "too many suggestions"? What can that mean? Please **update** this question to list the choices and explain what's wrong with each one. Otherwise, we have no idea why you don't simply choose one. – S.Lott Mar 08 '11 at 11:18

1 Answers1

0

As sven says. I think there is a method to set a new timeout on socket. So just connect and if it times out consider it offline.

EDIT:

Code examples
import subprocess
def ping(target):
    process = subprocess.Popen('ping %s' % target, stdout = subprocess.PIPE)
    results = process.communicate()[0]
    return "Reply from %s" % target in results
Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
  • You can change how the ping program is called by changing the Popen line, it takes quite a time for unreachable targets because it is literately waiting for the ping program to time out. Here is me using it, `In [4]: ping('192.168.0.1') Out[4]: False In [5]: ping('74.125.230.144') Out[5]: True` – Jakob Bowyer Mar 08 '11 at 11:44
  • @Jakob: I get `OSError: [Errno 2] No such file or directory` for subprocess.py while run the script. I've updated my original post with the details. Cheers!! – MacUsers Mar 08 '11 at 12:15
  • @Jakob: Didn't quite follow you - where should I set that? – MacUsers Mar 08 '11 at 13:09
  • `process = subprocess.Popen('ping %s' % target, stdout = subprocess.PIPE, shell = True)` – Jakob Bowyer Mar 08 '11 at 13:14
  • @Jakob: Sorry for not updating. I'm still not sure if it's actually working - "False" is always being returned regardless of node status. Updated my original post with details. cheers!! – MacUsers Mar 10 '11 at 11:18