On Sat, Jan 21, 2012 at 7:47 PM, Nikunj Badjatya <nikunjbadja...@gmail.com> wrote: > 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.! >
This appears to be windows specific. Linux ping will return an exit code of 1 if either zero responses are received or a packetcount and deadline are specified and not met. I'm not sure why it doesn't work that way on windows, but testing a bit for myself it seems to be the platform's fault and not python's, since calling EXIT 1 will correctly return a status code of 1. > 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.?? > rather than using the wait() call, use Popen.communicate() which returns the output of the program, and check that directly for your three cases. HTH, Hugo _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor