windows 批处理文件 - 使用 ping 测试网络连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6357515/
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
Batch Files - Using ping to test network connectivity
提问by Peter
Using a batch file would it be possible to do something like:
使用批处理文件可以执行以下操作:
ping google.com
ping google.com
if return success do ECHO You are connected to the internet
如果返回成功执行 ECHO 您已连接到互联网
else return ECHO You are not connected to the internet
否则返回 ECHO 你没有连接到互联网
回答by CSchulz
You can use following snippet:
您可以使用以下代码段:
@echo off
Ping www.google.de -n 1 -w 1000
if errorlevel 1 echo Not connected
回答by jfgoodhew1
Here's a script that will repeatedly check, and write the time (from system clock) and "internet offline" to a log file at C:\Internet.txt each time the internet goes offline. Unfortunately the latest line in the log file will appear at the end - I don't know how to make it appear at the top ;)
这是一个脚本,它会在每次互联网脱机时反复检查和写入时间(从系统时钟)和“互联网脱机”到 C:\Internet.txt 的日志文件中。不幸的是,日志文件中的最新行会出现在最后——我不知道如何让它出现在顶部;)
BTW: I set the wait time (-w) to 20 seconds, because I was using a 3G dongle (with 2G internet) so 20s was often the only way to be sure if the internet was really down or something else was the problem... Feel free to change it to 5000 for 5s, or delete "-w 20000" altogether to leave it at default.
顺便说一句:我将等待时间 (-w) 设置为 20 秒,因为我使用的是 3G 加密狗(带有 2G 互联网),因此 20 秒通常是确定互联网是否真的出现故障或其他问题的唯一方法。 .. 随意将其更改为 5000 5 秒,或完全删除“-w 20000”以保留默认值。
@echo off
:START
ping -n 4 4.2.2.2 -w 20000 >nul
if %errorlevel% == 1 (
echo Internet offline >> C:\Internet.txt
Time /t >> C:\Internet.txt
)
Timeout /t 30
@set errorlevel = 0
GOTO START
回答by Ozair Kafray
Here is a script to help you start with it:
这是一个帮助您开始的脚本:
NOTE: If your system is not in English, you will have to modify the lines in the script where find
command is being used to filter Reply from
from the ping's output to the corresponding string in the system's language.
注意:如果您的系统不是英文,则必须修改脚本中的行,其中find
命令用于Reply from
将 ping 的输出过滤为系统语言的相应字符串。
回答by 12345
@echo off
echo Checking connection
ping -n 1 www.google.com >nul
if errorlevel 1 (
cls
echo Failed
pause>nul
exit
)
cls
echo Success!
pause>nul
exit
回答by user
Based on the answer from @CShulz, here's a script that'll print "Not connected" only when there's no connection, else it'll silently loop through the test every 30 seconds. First ping tests for connectivity & prints an error message if there's a problem. The second ping adds a 30 second wait by pinging the localhost.
根据@CShulz 的回答,这里有一个脚本,只有在没有连接时才会打印“未连接”,否则它将每 30 秒静默循环一次测试。首先对连接进行 ping 测试并在出现问题时打印错误消息。第二个 ping 通过 ping 本地主机增加了 30 秒的等待时间。
@echo off
:loop
ping www.google.com -n 1 -w 5000 > nul
if errorlevel 1 echo Not connected
ping -n 30 127.0.0.1 > nul
goto loop
回答by TOasT
@echo off
:loop
ping www.google.com -n 1 -w 5000 >NUL
if errorlevel 1 echo Not connected
goto Loop
回答by askuse7
echo Testing Internet Connection of google.com
@echo off
:loop
ping google.com -n 1 -w 5000 > nul
if errorlevel 1 echo %date% - %time% Not connected >> pingtestlog.txt
ping -n 30 127.0.0.1 > nul
goto loop