Ping from windows 7 get no reply but sets errorlevel to 0

From a Windows 7 machine, I ping an IP address of a turned-off machine.

C:\>ping 192.168.1.222
Pinging 192.168.1.222 with 32 bytes of data:
Reply from 192.168.1.222: Destination host unreachable.
Reply from 192.168.1.222: Destination host unreachable. 
Reply from 192.168.1.222: Destination host unreachable.

Ping statistics for 192.168.1.222:
    Packets: Sent = 3, Received = 3, Lost = 0 (0% loss)

Even though there is no reply, the errorlevel is set to 0.

What I am trying to do, is figure out if a remote machine is replying to ping. One of my tests is to turn off the machine and ping it. For some reason, ping sets errorlevel to 0.


I also faced the problem and it resolved with key "-w 2000". Ping supplemented with key "-w" and number of milliseconds returns 1 when host in local subnet is unreachable. AFAIK, number of milliseconds must be smaller than timeout after that Windows' network subsystem returns "Destination host unreachable".

On my PC (Windows 7):
C:>ping -w 3000 192.168.10.22

Pinging 192.168.10.22 with 32 bytes of data:
Reply from 192.168.10.2: Destination host unreachable.
Reply from 192.168.10.2: Destination host unreachable.
Reply from 192.168.10.2: Destination host unreachable.
Reply from 192.168.10.2: Destination host unreachable.

Ping statistics for 192.168.10.22:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

C:>echo %errorlevel%
0

but

C:>ping -w 2999 192.168.10.22

Pinging 192.168.10.22 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 192.168.10.22:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

C:>echo %errorlevel%
1


This is because the destination is on your local subnet. If the destination is outside your local subnet, the expected Request timed out. is seen. However, even non-existent destinations within your local subnet will report Reply from x.x.x.x: Destination host unreachable.. As this whirlpool post explains it, it all depends on whether its the first hop that timed out - and pinging your local subnet is only a single hop. Apparently, it's by design that the errorlevel is set to 0 when any reply is received, and a host unreachable counts as a reply.


Workarounds

Parsing ping output with findstr

Source: http://forums.techguy.org/7318331-post3.html

The following will ping it once (ping -n 1). The %1 is the first param passed to the batch file. The variable attrib is set depending on whether the destination replies or not.

echo Scanning %1
set attrib=responding
ping -n 1 %1|Findstr /I /C:"timed out" /C:"host unreachable" /C:"could not find host"
if %errorlevel%==0 set attrib=nonresponsive

Separate utility

There's an interesting utility called alive. I haven't tried it, and it was released in 2002, so it may not even work in Windows 7. However, if it does work, it will set the errorlevel to 2 when the destination host is unreachable. I wouldn't really recommend this, though.