windows 批量 ERRORLEVEL ping 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9329749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:14:53  来源:igfitidea点击:

Batch ERRORLEVEL ping response

windowsbatch-filecommand-promptping

提问by LastStar007

I'm trying to use a batch file to confirm a network connection using ping. I want to do batch run and then print if the ping was successful or not. The problem is that it always displays 'failure' when run as a batch. Here is the code:

我正在尝试使用批处理文件来确认使用 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' is the name of my computer. I'm having my computer ping itself so I can eliminate the variable of a poor connection. 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

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

采纳答案by Jon

I 'm not exactly sure what the interaction between FINDand 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 0if the ping failed, 1if it succeeded. I made it look for just "0% loss" (not specifically 4 pings) so that the number of pings can be customized.

0如果 ping 失败,1如果成功,则打印。我让它只寻找“0% 损失”(不是特别是 4 个 ping),以便可以自定义 ping 的数量。

The percent sign has been doubled so that it's not mistaken for a variable that should be substituted.

百分号已加倍,以免被误认为是应该替换的变量。

The FORtrick serves simply to set the output of a command as the value of an environment variable.

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

回答by Jelle Geerts

A more reliable pingerror 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 msor 314msis printed by ping.

这通过检查诸如69 ms或 之类的字符串是否由314ms打印ping

(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 TTLare 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=. Also, not only may time=be translated, but sometimes it may be time<rather than time=, as in the case of time<1ms.

其他建议,例如匹配time=TTL不那么可靠,因为 ping IPv6 地址不显示TTL(至少在我的 Windows 7 机器上不显示)并且 Windows 的翻译版本可能会显示字符串的翻译版本time=。此外,不仅可以time=翻译,而且有时可能是time<而不是time=,例如time<1ms

回答by Alex K.

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"

回答by RolfBly

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 地址上有一个网络驱动器,并且您想知道它是否打开。

If that drive is off, and you ping some_IP-address, the IP address from which you ping, will respond:
Answer from your_own_IP-address: target host not reachable
... 0% loss

如果该驱动器已关闭,并且您 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 existor if not existon that network location.

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

回答by Martin Binder

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 ;-))

当然,如果连接速度较慢,则需要调整 -w(拨号 100 毫秒可能太短;-))

regards

问候

回答by Chandan

ping 198.168.57.98 && echo Success || echo failed

回答by Toni Lazazzera

Another variation without using any variable

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

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

:pingerror
echo Host down
goto eof

:eof
exit /b

回答by Brian

Yes ping fails to return the correct errorlevel. To check the network connection and the computer I used "net view computername" then checked %errorlevel% - simple and easy

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

回答by NickS

I needed to reset a wifi connection because it has issues. This was my quick solution.

我需要重置 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

回答by Rod

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. If the Reply String contains anything other than the correct IP it reports failure.

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

If you want you can just read the value of ERRORLEVEL from the FIND. That will give you a reliable value to work with.

如果您愿意,您可以从 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