[Tutor] checking return status of 'ping' in windows

Nikunj Badjatya nikunjbadjatya at gmail.com
Sat Jan 21 19:47:39 CET 2012


Hi All,

I am using the following snippet to check the availability of an IP
address. If that IP addr is found free than it can be used later on for
further operations.
Python ver 3.2
Windows OS

{{{
pingret = subprocess.Popen('ping {0}'.format(IPaddr),
shell=True,universal_newlines=True, \
                    stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
status = pingret.wait()
if status == 0:
    print("WARN  The IP given in the input is not free")
.....
.....
}}}

Normal ping operation on windows cmd prompt can give 3 outputs.
1) "destination host unreachable
2) "request timed out"
3) "Reply from 192.168.1.1: bytes=32 time=3ms TTL=64"

Now,
I was expecting the "status" in above snippet to hold '0' only in case of
no. 3)
But even when we have case 1), 'status' is holding '0'.
i.e. The exit status of ping is 0, even when destination host is
unreachable.!

How do I make my snippet to work as desired. i.e even if destination host
is unreachable, 'status' should hold '1'  and hold '0' only when it gets
reply from that ip address.??


Thanks,

Nikunj


--
*7*Switch off as you go |*q*Recycle always | P Save Paper - Save Trees | Go
Green
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120122/0250ad3d/attachment.html>


More information about the Tutor mailing list