我正在尝试使用batch file来确认使用ping的networking连接。 我想做批处理运行,然后打印,如果ping成功或没有。 问题是,当按批次运行时,它总是显示“失败”。 这里是代码:
@echo off cls ping racer | find "Packets: Sent = 4, Received = 4, Lost = 0 (0% loss)," if not errorlevel 1 set error=success if errorlevel 1 set error=failure cls echo Result: %error% pause
“赛车手”是我电脑的名字。 我有我的电脑本身,所以我可以消除一个糟糕的连接variables。 正如我之前所说的,批次总是导致失败。 奇怪的是,如果我将代码复制到命令提示符下,程序工作正常。 有谁知道为什么程序在命令提示符工作正常,但不作为批处理? 谢谢
我不确定FIND
和设置错误级别之间的相互作用是什么,但是你可以很容易地做到这一点:
@echo off for /f %%i in ('ping racer ^| find /c "(0%% loss)"') do SET MATCHES=%%i echo %MATCHES%
如果ping失败,则打印0
如果成功,则打印1
。 我让它看起来只是“0%的损失”(不是特别的4点),所以可以定制ping的数量。
百分号已经加倍,所以它不会被误认为应该被替换的变量。
FOR
技巧只是将命令的输出设置为环境变量的值。
更可靠的ping
错误检查方法:
@echo off set "host=192.168.1.1" ping -n 1 "%host%" | findstr /r /c:"[0-9] *ms" if %errorlevel% == 0 ( echo Success. ) else ( echo FAILURE. )
这是通过检查是否打印一个字符串,如69 ms
或314ms
69 ms
通过ping
。
(Windows的翻译版本可能会打印42 ms
(与空间),因此我们检查。)
原因:
其他的建议,比如匹配time=
或者TTL
,并不可靠,因为Ping的IPv6地址不会显示TTL
(至少在我的Windows 7机器上没有),Windows的翻译版本可能会显示字符串time=
的翻译版本。 另外,不仅可以翻译time=
,而且有时可能是time<
而不是time=
,例如time<1ms
。
如果你是
echo "Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),"
你会看到%
被剥离。 您需要将其转义为%
在批处理文件中具有特殊含义:
"Packets: Sent = 4, Received = 4, Lost = 0 (0%% loss),"
然而,使用TTL作为成功的指示更简单。
.. | find "TTL"
在这种情况下,测试0%的损失可能会导致错误的结果:假设您通常在some_IP-address上有一个网络驱动器,并且您想知道它是否处于开启状态。
如果该驱动器处于关闭状态,并且ping ping some_IP-address,则ping的IP地址将会响应:
Answer from your_own_IP-address: target host not reachable
... 0% loss
if exist
或者if not exist
于网络位置, if exist
可能会更好。
我能想到的最简单的解决方案是:
set error=failure ping racer -n 1 -w 100>nul 2>&1 && set error=success
当然,-w需要调整,如果在一个慢速链接(100毫秒可能是太短的拨号;-))
问候
另一个变种没有使用任何变量
ping racer -n 1 -w 100>nul || goto :pingerror ... :pingerror echo Host down goto eof :eof exit /b
ping 198.168.57.98 && echo Success || echo failed
首先
>@echo off >for /f %%i in ('ping racer ^| find /c "(0%% loss)"') do SET MATCHES=%%i >echo %MATCHES%
不起作用。 如果不会失败,则会检测到0%,因为它有0%。 如果失败了,也不管用,因为它会有100%的损失,也就是说,它发现10%(0%损失)后面的0%损失部分,
有没有发现像这样100%的损失:
>for /f %%i in ('ping -n 1 -l 1 %pc% ^| find /c "(100%% loss)"') do SET check=%%i
错误级别可能会有点混乱,但它的作用就像一个魅力:
>if '%check%'=='1' goto fail >if '%check%'=='0' echo %pc% is online.&goto starting
1表示失败0表示成功
在我的脚本是使用链接。 转到失败将去:失败在我的脚本,这将会告诉我%pc%(我将在开始时的用户输入)是脱机,将去另一次运行。
>:fail >color 0c >title %pc% is offline >echo %pc% is offline >PING -n 6 127.0.0.1>nul >goto choice
我希望这有帮助。
ping具有错误级别的输出值。 成功是0,失败是1.只要这样做:
C:\>ping 4.2.2.2 Pinging 4.2.2.2 with 32 bytes of data: Reply from 4.2.2.2: bytes=32 time=28ms TTL=57 Reply from 4.2.2.2: bytes=32 time=29ms TTL=57 Reply from 4.2.2.2: bytes=32 time=30ms TTL=57 Reply from 4.2.2.2: bytes=32 time=29ms TTL=57 Ping statistics for 4.2.2.2: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 28ms, Maximum = 30ms, Average = 29ms C:\>echo %errorlevel% 0 C:\>ping foo.bar Ping request could not find host foo.bar. Please check the name and try again. C:\>echo %errorlevel% 1
正如你所看到的,不需要所有这些脚本矫枉过正。
基于Alex K的说明,这适用于Windows 7上的我:
@echo off setlocal enableextensions enabledelayedexpansion for /f %%i in (PCS.TXT) do ( SET bHOSTUP=0 ping -n 2 %%i |find "TTL=" > NUL && SET bHOSTUP=1 IF !bHOSTUP! equ 1 ( CALL :HOSTUP %%i ) else ( CALL :HOSTDOWN %%i ) ) GOTO EOF :HOSTUP echo Host UP %1 GOTO EOF :HOSTDOWN echo Host DOWN %1 GOTO EOF :EOF exit /B
set ErrorLevel=1 (ping example.com -n 1 && set ErrorLevel=0)>nul
如果example.com在线, ErrorLevel
为0
,否则为1
。
注意:仅在Win8上测试!
是的ping无法返回正确的错误级别。 要检查网络连接和计算机,我使用“网络视图计算机名”,然后选中%errorlevel% – 简单和容易