11

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.

3 Answers 3

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

3
  • Can anyone confirm that this is valid for all use cases? Jan 9, 2015 at 14:11
  • 2
    I tested setting the -w switch to 2999 against a valid and invalid IP adding the -n 1 switch as well. It seems to work.
    – Will B.
    Jan 22, 2015 at 22:41
  • yes... this sets the errorlevel if no host at the specified address... thanks
    – ZEE
    May 2, 2017 at 20:19
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.

4
  • +1 for alive, used that many years ago too, for this very reason.
    – Bryan
    Mar 29, 2012 at 7:08
  • 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. May 1, 2014 at 13:02
  • 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
    May 1, 2014 at 13:29
  • 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
    Apr 24, 2016 at 20:44
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .