1

I'm using the windows batch script function ping 192.0.2.2 -n 1 -w 10000 > nul on my local machine (Windows 7) in a command prompt to test its functionality. The explanation of the function is here: "How to wait in a batch script?".

I've used 192.0.2.2 because it is considered a reserved IP address, so there should be no response. But instead, my results are inconsistent - sometimes I get a response with the error "Destination net unreachable."

C:\Users\MrF>ping 192.0.2.2 -n 1 -w 10000

Pinging 192.0.2.2 with 32 bytes of data:
Reply from 10.10.251.2: Destination net unreachable.

Ping statistics for 192.0.2.2:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),

C:\Users\MrF>ping 192.0.2.2 -n 1 -w 10000

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

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

C:\Users\MrF>ping 192.0.2.2 -n 1 -w 10000

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

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

C:\Users\MrF>ping 192.0.2.2 -n 1 -w 10000

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

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

C:\Users\MrF>ping 192.0.2.2 -n 1 -w 10000

Pinging 192.0.2.2 with 32 bytes of data:
Reply from 10.10.251.2: Destination net unreachable.

Ping statistics for 192.0.2.2:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),

C:\Users\MrF>ping 192.0.2.2 -n 1 -w 10000

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

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

C:\Users\MrF>

Unfortunately, the function only waits 10 seconds when the ping is lost. Pings that receive a response don't wait at all. This has lead to some frustrating lack of functionality in my scripts that require wait commands. How can I fix this? Why is it happening?

Community
  • 1
  • 1
Vincent Vance
  • 117
  • 1
  • 4
  • 13

2 Answers2

2

That is not a link local or LAN reserved IP range, but it is routable (although no one should ever route to it). See this whois info for full details. A link local 169.254 range IP would give more consistent, reliable results. See this answer for further explanation.

Edit: I see what you mean. Even my previous 169.254 solution is subject to the same inconsistency. Well, really, the biggest benefit to pinging a bogus IP with the -w switch is so you can pause in milliseconds. If you're pausing in whole seconds (such as ten seconds in your example above), just

ping -n 11 localhost >NUL

... where 11 is N+1 seconds to pause. That command will pause for 10 seconds.

Alternatively, you could use the timeout /t 10 /NOBREAK >NUL command if your script will be run on Vista or newer.

If you need fraction-of-a-second pausing, you could always employ VBScript or JScript.

@if (@CodeSection == @Batch) @then

@echo off
setlocal

set /P "=Pausing 1.5 seconds."<NUL
call :sleep 500
set /P "=."<NUL
call :sleep 500
set /P "=. "<NUL
call :sleep 500
echo Done.

goto :EOF

:sleep <milliseconds>
cscript /nologo /e:JScript "%~f0" "%~1"
goto :EOF

@end // end Batch / begin JScript
WSH.Sleep(WSH.Arguments(0) * 1);
Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101
-1

Windows has many wait commands, I use waitfor. But there is choice and timeout as well.

Maybe stop searching the internet. Ping is a silly idea. What would happen if all 175,000 computers at my work all started pinging at the same time.

waitfor /t 99999 Fred
Serenity
  • 24
  • 2
  • 2
    *"What would happen if all 175,000 computers at my work all started pinging at the same time."* Then you'd have 11 megabytes of traffic. Oh, how would you ever recover? :) – rojo Mar 19 '15 at 19:36
  • Can waitfor be used to wait exactly 10 seconds without requiring a signal? – Vincent Vance Mar 19 '15 at 23:17
  • Yes. It waits for a signal or the time specified. Just ignore the signal part and specify something random for it. `Choice` is when you want the user to be able to interupt it. – Serenity Mar 19 '15 at 23:30
  • Maybe the question should be why you require waits? – Serenity Mar 19 '15 at 23:31