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

Nikunj Badjatya nikunjbadjatya at gmail.com
Sat Jan 21 19:29:40 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 bytes=32 time=3ms TTL=64"

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

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.??



*7*Switch off as you go |*q*Recycle always | P Save Paper - Save Trees | Go

More information about the BangPypers mailing list