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

10

4

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.

Doron

Posted 2012-03-23T08:55:52.563

Reputation: 111

Answers

8

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

user354000

Posted 2012-03-23T08:55:52.563

Reputation: 81

Can anyone confirm that this is valid for all use cases? – user1853181 – 2015-01-09T14:11:22.793

2I tested setting the -w switch to 2999 against a valid and invalid IP adding the -n 1 switch as well. It seems to work. – fyrye – 2015-01-22T22:41:47.410

yes... this sets the errorlevel if no host at the specified address... thanks – ZEE – 2017-05-02T20:19:11.350

7

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.

Bob

Posted 2012-03-23T08:55:52.563

Reputation: 51 526

Ugh... well that's stupid... Spent half my day trying to figure out why my system was reporting a subsystem as online - while it was clearly not... – Matt Clark – 2016-04-24T20:44:37.117

+1 for alive, used that many years ago too, for this very reason. – Bryan – 2012-03-29T07:08:06.467

If the response is "Ping request could not find host madeup04. Please check the name and try again" this does not work, because it is looking for "host unreachable", so your result is "madeup04 is responding" which is wrong. – James Jenkins – 2014-05-01T13:02:35.743

1@JamesJenkins I did make the assumption that, as in the question, the ping attempt is against an IP address. Even then, it's a trivial change (add another /C search string). Hardly worth a downvote. As it is, I have amended the answer, so thanks for pointing that out. – Bob – 2014-05-01T13:29:58.603

2

This works for me on Windows 7:

@echo off
setlocal enableextensions enabledelayedexpansion

for /f %%i in (PCS.TXT) do (
   SET bHOSTUP=0
   ping -n 2 %%i |find "TTL=" && SET bHOSTUP=1
   IF !bHOSTUP! equ 1 (
      CALL :HOSTUP %%i
   ) else (
      CALL :HOSTDOWN %%i 
   )
)

:HOSTUP
echo Host UP %1
GOTO EOF

:HOSTDOWN
echo Host DOWN %1
GOTO EOF

:EOF
exit /B

Andrew S

Posted 2012-03-23T08:55:52.563

Reputation: 129