使用批处理文件,能否做到以下操作:
ping google.com
如果返回成功,则输出“您已连接到互联网”
否则,输出“您未连接到互联网”
使用批处理文件,能否做到以下操作:
ping google.com
如果返回成功,则输出“您已连接到互联网”
否则,输出“您未连接到互联网”
您可以使用以下代码片段:
@echo off
Ping www.google.de -n 1 -w 1000
if errorlevel 1 echo Not connected
@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
注意:如果您的系统不是英语,您将需要修改脚本中使用find
命令过滤ping输出中的Reply from
到系统语言对应字符串的行。
@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
@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
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
@echo off
:loop
ping www.google.com -n 1 -w 5000 >NUL
if errorlevel 1 echo Not connected
goto Loop
@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
我知道这是一个旧帖子,但我还是想做出我的贡献。
@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