我正在使用windows批处理脚本功能ping 192.0.2.2 -n 1 -w 10000>在我的本地计算机(Windows 7)上,在命令提示符下测试其功能。函数的解释如下:"How to wait in a batch script?"。
我使用192.0.2.2因为它被认为是一个保留的IP地址,所以应该没有响应。但相反,我的结果是不一致的 - 有时我得到一个错误“目标网络无法访问”的响应。
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>
不幸的是,该功能仅在ping丢失时等待10秒。接收响应的ping不会等待。这导致我的脚本中需要等待命令的一些令人沮丧的功能缺失。我怎样才能解决这个问题?为什么会这样?
答案 0 :(得分:2)
这不是链接本地或LAN保留的IP范围,但它是可路由的(尽管没有人应该路由到它)。 See this whois info了解详情。链路本地169.254范围IP将提供更一致,可靠的结果。 See this answer进一步解释。
编辑:我明白你的意思了。即使我之前的169.254解决方案也存在同样的不一致。嗯,真的,使用-w
开关ping伪造IP的最大好处是可以暂停几毫秒。如果您在整秒内暂停(例如上面示例中的十秒),只需
ping -n 11 localhost >NUL
...其中11是N + 1秒暂停。该命令将暂停10秒钟。
或者,如果您的脚本将在Vista或更新版本上运行,则可以使用timeout /t 10 /NOBREAK >NUL
命令。
如果你需要一秒钟的暂停,你可以随时使用VBScript或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);
答案 1 :(得分:-1)
Windows有许多等待命令,我使用waitfor
。但也有choice
和timeout
。
也许停止搜索互联网。 Ping是一个愚蠢的想法。如果我工作的所有175,000台计算机都同时开始ping,会发生什么。
waitfor /t 99999 Fred