简体   繁体   English

批处理文件 - 使用 ping 测试网络连接

[英]Batch Files - Using ping to test network connectivity

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 你没有连接到互联网

You can use following snippet:您可以使用以下代码段:

@echo off
Ping www.google.de -n 1 -w 1000
if errorlevel 1 echo Not connected

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.这是一个脚本,它会在每次互联网脱机时反复检查和写入时间(从系统时钟)和“互联网脱机”到 C:\\Internet.txt 的日志文件中。 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 ;)不幸的是,日志文件中的最新行会出现在最后——我不知道如何让它出现在顶部;)

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
@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

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.根据@CShulz 的回答,这里有一个脚本,只有在没有连接时才会打印“未连接”,否则它将每 30 秒静默循环一次测试。 First ping tests for connectivity & prints an error message if there's a problem.首先对连接进行 ping 测试并在出现问题时打印错误消息。 The second ping adds a 30 second wait by pinging the localhost.第二个 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

Here is a script to help you start with it:这是一个帮助您开始的脚本:

http://www.techimo.com/forum/networking-internet/73769-handy-batch-file-check-network-connectivity.html http://www.techimo.com/forum/networking-internet/73769-handy-batch-file-check-network-connectivity.html

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 的输出过滤为系统语言的相应字符串的行。

@echo off
:loop
ping www.google.com -n 1 -w 5000 >NUL
if errorlevel 1 echo Not connected
goto Loop
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

I understand this is an old thread, but here's my contribution anyways.我知道这是一个旧线程,但无论如何这是我的贡献。

@echo off

TITLE Network Connection Watchdog

:: Use Windows "Task Scheduler".
:: Set to run at "Startup" with a delay and interval of your choice.
:: Remember to tick "Run with highest privileges".

:: Last command will only be executed if all pings fail.

set /a pc=0
set /a total=0

:: Your preferences
set if="Wi-fi" &:: Find your interface name with [netsh interface show interface].
set ip=192.168.0.1
set /a pt=10 &:: Set amount of pings to be executed.

:loop
set /a pc+=1
ping %ip% -n 1 -w 100 | find "TTL=" >NUL 2>&1
for /f %%a in ('echo %errorlevel%') do set p%pc%=%%a

set /a total+=p%pc%
if not %pc%==%pt% goto loop

if not %total%==%pt% (
goto eof
) else (
netsh interface set interface %if% disable
ping localhost -n 5 >NUL 2>&1
netsh interface set interface %if% enable
)

:eof
exit

This basic script can ping google.com continuously and test your internet connection.这个基本脚本可以持续 ping google.com 并测试您的互联网连接。 script will run in a loop until you close the window.脚本将循环运行,直到您关闭窗口。

@echo off
:loop
ping www.google.com -n 1 -w 5000 > null
    if not errorlevel 1 set msg=Your are connected with internet...
    if errorlevel 1 set msg=No Internet...
cls
color 0a
echo %msg%
goto loop

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

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