简体   繁体   English

批量ERRORLEVEL ping响应

[英]Batch ERRORLEVEL ping response

I'm trying to use a batch file to confirm a network connection using ping. 我正在尝试使用批处理文件来确认使用ping的网络连接。 I want to do batch run and then print if the ping was successful or not. 我想批量运行然后打印如果ping成功与否。 The problem is that it always displays 'failure' when run as a batch. 问题是它作为批处理运行时始终显示“失败”。 Here is the code: 这是代码:

@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' is the name of my computer. 'racer'是我电脑的名字。 I'm having my computer ping itself so I can eliminate the variable of a poor connection. 我正在让我的计算机ping自己,所以我可以消除连接不良的变量。 As I said before, the batch always results in failure. 正如我之前所说,批次总是导致失败。 Oddly enough, the program works fine if I copy the code into the command prompt. 奇怪的是,如果我将代码复制到命令提示符中,程序运行正常。 Does anyone know why the program works fine in the command prompt but doesn't work as a batch? 有谁知道为什么程序在命令提示符下正常工作但不能作为批处理工作? Thanks 谢谢

A more reliable ping error checking method: 更可靠的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.
)

This works by checking whether a string such as 69 ms or 314ms is printed by ping . 这通过检查是否通过ping打印诸如69 ms314ms 69 ms的字符串来工作。

(Translated versions of Windows may print 42 ms (with the space), hence we check for that.) (Windows的翻译版本可能会打印42 ms (带空格),因此我们会检查它。)

Reason: 原因:

Other proposals, such as matching time= or TTL are not as reliable, because pinging IPv6 addresses doesn't show TTL (at least not on my Windows 7 machine) and translated versions of Windows may show a translated version of the string time= . 其他提议,例如匹配time=TTL不可靠,因为ping IPv6地址不显示TTL (至少不在我的Windows 7机器上),Windows的翻译版本可能会显示字符串time=的翻译版本。 Also, not only may time= be translated, but sometimes it may be time< rather than time= , as in the case of time<1ms . 此外,不仅可以翻译time= ,而且有时可能是time<而不是time= ,如在time<1ms的情况下。

Testing for 0% loss may give a false positive, in this scenario: Let's say you normally have a network drive on some_IP-address, and you want to find out whether or not it's on. 在这种情况下,测试0%的丢失可能会产生误报:假设您通常在some_IP-address上有一个网络驱动器,并且您想知道它是否打开。

If that drive is off, and you ping some_IP-address, the IP address from which you ping, will respond: 如果该驱动器已关闭,并且ping了some_IP-address,则ping的IP地址将响应:
Answer from your_own_IP-address: target host not reachable
... 0% loss

You might be better off using if exist or if not exist on that network location. if exist或者if not exist于该网络位置,您可能最好使用。

If you were to 如果你是

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

you would see the % is stripped. 你会看到%被剥离。 You need to escape it as % has a special meaning within a batch file: 您需要转义它,因为%在批处理文件中具有特殊含义:

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

However its simpler to use TTL as the indication of success; 然而,使用TTL作为成功的指示更简单;

.. | find "TTL"

I 'm not exactly sure what the interaction between FIND and setting the error level is, but you can do this quite easily: 我不确定FIND和设置错误级别之间的交互是什么,但是你可以很容易地做到这一点:

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

This prints 0 if the ping failed, 1 if it succeeded. 如果ping失败则打印0 ,如果成功则打印1 I made it look for just "0% loss" (not specifically 4 pings) so that the number of pings can be customized. 我只是寻找“0%损失”(不是特别是4个ping),以便可以定制ping的数量。

The percent sign has been doubled so that it's not mistaken for a variable that should be substituted. 百分号已加倍,因此不会误认为应该替换的变量。

The FOR trick serves simply to set the output of a command as the value of an environment variable. FOR技巧仅用于将命令的输出设置为环境变量的值。

The most simple solution to this I can think of: 我能想到的最简单的解决方案:

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

Of course, -w needs to be adjusted if on a slow link (100ms might be too short over Dialup ;-)) 当然,如果在慢速链接上(-100毫秒可能比拨号太短,则需要调整;-))

regards 问候

Another variation without using any variable 不使用任何变量的另一种变体

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

:pingerror
echo Host down
goto eof

:eof
exit /b

Yes ping fails to return the correct errorlevel. 是ping无法返回正确的错误级别。 To check the network connection and the computer I used "net view computername" then checked %errorlevel% - simple and easy 要检查网络连接和我使用“net view computername”的计算机,然后选中%errorlevel% - 简单易用

ping 198.168.57.98 && echo Success || echo failed

Based on Alex K's note, this works for me on Windows 7: 根据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

First of all 首先

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

Does not work. 不行。 If it won't fail, it will detect 0%, because it has 0%. 如果它不会失败,它将检测到0%,因为它有0%。 If it fails, does not work either, because it will have 100% loss, which means, it found the 0% loss part behind the 10 10(0% loss) 如果失败,也不起作用,因为它会有100%的损失,这意味着,它发现0%损失部分落后于10 10(0%损失)

Have it detect for 100% loss like so: 让它检测到100%的损失如下:

>for /f %%i in ('ping  -n 1 -l 1 %pc% ^| find /c "(100%% loss)"') do SET check=%%i

Errorlevel might be a bit messed up, but it works like a charm: Errorlevel可能有点混乱,但它的作用就像一个魅力:

>if '%check%'=='1' goto fail
>if '%check%'=='0' echo %pc% is online.&goto starting

1 means it failed 0 means it succeeded 1表示失败0表示成功

In my script is use links. 在我的脚本中使用链接。 Goto fail will go to :fail in my script which will message me that %pc% (which I'll have the user input in the beginning) is offline and will go for another run. 转到失败将转到:在我的脚本中失败,它会告诉我%pc%(我将在开头有用户输入)离线并将进行另一次运行。

>:fail
>color 0c
>title %pc% is offline
>echo %pc% is offline
>PING -n 6 127.0.0.1>nul
>goto choice

I hope this helps. 我希望这有帮助。

ping has an errorlevel output value. ping具有错误级别输出值。 Success is 0, failure is 1. Just do this: 成功为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

As you can see there is no need for all this scripting overkill. 正如您所看到的,不需要所有这些脚本过度杀伤。

I needed to reset a wifi connection because it has issues. 我需要重置wifi连接,因为它有问题。 This was my quick solution. 这是我的快速解决方案。

@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

I liked the concept of the FIND in the ping results but why not just FIND the Reply from the Address being pinged? 我在ping结果中喜欢FIND的概念,但为什么不只是找到地址被ping的回复?

In the example below I enter an IP address as a variable, PING that IP, then look for that variable in the reply string, using the FIND Command. 在下面的示例中,我输入IP地址作为变量,PING表示IP,然后使用FIND命令在回复字符串中查找该变量。 If the Reply String contains anything other than the correct IP it reports failure. 如果回复字符串包含除正确IP之外的任何内容,则报告失败。

If you want you can just read the value of ERRORLEVEL from the FIND. 如果您愿意,您可以从FIND中读取ERRORLEVEL的值。 That will give you a reliable value to work with. 这将为您提供可靠的价值。

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM