批量ERRORLEVEL ping响应

时间:2012-02-17 14:12:48

标签: windows batch-file command-prompt ping

我正在尝试使用批处理文件来确认使用ping的网络连接。我想批量运行然后打印如果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

'racer'是我的电脑名称。我正在让我的计算机ping自己,所以我可以消除连接不良的变量。正如我之前所说,批次总是导致失败。奇怪的是,如果我将代码复制到命令提示符中,程序运行正常。有谁知道为什么程序在命令提示符下正常工作但不能作为批处理工作? 谢谢

14 个答案:

答案 0 :(得分:21)

更可靠的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打印314msping等字符串来实现。

(Windows的翻译版本可能会打印42 ms(带空格),因此我们会检查它。)

<强>原因:

其他提案(例如匹配time=TTL)并不可靠,因为ping IPv6地址不显示TTL(至少不在我的Windows 7计算机上)并已翻译Windows版本可能会显示字符串time=的翻译版本。此外,不仅可以翻译time=,有时也可以time<而不是time=,就像time<1ms一样。

答案 1 :(得分:4)

如果你要

echo "Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),"

您会看到%被剥离。您需要转义它,因为%在批处理文件中具有特殊含义:

"Packets: Sent = 4, Received = 4, Lost = 0 (0%% loss),"

然而,使用TTL作为成功的指示更简单;

.. | find "TTL"

答案 2 :(得分:4)

在这种情况下,测试0%的损失可能会产生误报: 假设您通常在some_IP-address上有一个网络驱动器,并且您想知道它是否打开。

如果该驱动器已关闭,并且ping了some_IP-address,则ping的IP地址将响应:
Answer from your_own_IP-address: target host not reachable
... 0% loss

最好在该网络位置使用if existif not exist

答案 3 :(得分:2)

我不确定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),以便可以定制ping的数量。

百分号已加倍,因此不会误认为应该替换的变量。

FOR技巧仅用于将命令的输出设置为环境变量的值。

答案 4 :(得分:1)

我能想到的最简单的解决方案:

set error=failure
ping racer -n 1 -w 100>nul 2>&1 && set error=success

当然,如果在慢速链接(100ms可能太短而不是拨号; - ),则需要调整-w)

问候

答案 5 :(得分:1)

不使用任何变量的另一种变体

ping racer -n 1 -w 100>nul || goto :pingerror
...

:pingerror
echo Host down
goto eof

:eof
exit /b

答案 6 :(得分:1)

setNewLine

答案 7 :(得分:0)

根据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

答案 8 :(得分:0)

首先

>@echo off
>for /f %%i in ('ping racer ^| find /c "(0%% loss)"') do SET MATCHES=%%i
>echo %MATCHES%

不起作用。如果它不会失败,它将检测到0%,因为它有0%。 如果它失败了,也不起作用,因为它会有100%的损失,这意味着,它发现0%的损失部分落后于10 10(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

我希望这会有所帮助。

答案 9 :(得分:0)

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

正如您所看到的,不需要所有这些脚本过度杀伤。

答案 10 :(得分:0)

set ErrorLevel=1
(ping example.com -n 1 && set ErrorLevel=0)>nul
如果exam​​ple.com在线,则

ErrorLevel0,否则为1

注意:仅在Win8上测试过!

答案 11 :(得分:0)

是ping无法返回正确的错误级别。检查网络连接和我使用的计算机&#34; net view computername&#34;然后检查%errorlevel% - 简单易用

答案 12 :(得分:0)

我需要重置wifi连接,因为它有问题。这是我的快速解决方案。

@echo off
Rem Microsoft Windows 10 ping test to gateway.
Rem Run batch file from an administrative command prompt.

cls
:starting
Rem Send one ping to the gateway.  Write the results to a file.
ping 192.168.1.1 -n 1 > pingtest.txt

Rem Search for unreachable in the file. 
c:\windows\system32\findstr.exe "unreachable" pingtest.txt

Rem errorlevel 0 reset the adapter if 1 then wait 10 minutes and test again
if %errorlevel%==1 goto waiting

Rem unreachable was found reset the adapter.

Rem write the date and time the reset was done.
echo Reset date: %date% time: %time% >> resettimes.txt

Rem issue netsh interface show interface to find your adapter's name to reset
Rem my adapter is "wi-fi"

netsh interface set interface "wi-fi" disable
timeout /t  5
netsh interface set interface "wi-fi" enable
:waiting
echo "It is online waiting 10 minutes"
timeout /t  600
goto starting

答案 13 :(得分:0)

我喜欢在ping结果中使用FIND的概念,但为什么不只是查找来自ping地址的回复?

在下面的示例中,我输入一个IP地址作为变量,PING该IP,然后使用FIND命令在回复字符串中查找该变量。 如果回复字符串包含除正确IP以外的任何内容,则报告失败。

如果需要,您可以从FIND中读取ERRORLEVEL的值。 这样可以为您提供可靠的价值。

@echo off
Set /P IPAdd=Enter Address:
cls
ping %IPAdd% | find "Reply from %IPAdd%:"
if not errorlevel 1 set error=success
if errorlevel 1 set error=failure
cls
echo Result: %error%
pause