我做了一个简单的批处理脚本,在返回正面或负面消息框之前几次ping 8.8.8.8。当互联网工作时,它很好。但是当它不是时,脚本会无限地ping而不生成消息框。谁能指出我哪里出错? 此外,如果脚本看起来非常简陋,那是因为它可能是。我是新手。
@echo off
:PING
ping 8.8.8.8
IF ERRORLEVEL 0 goto GOODCHECKER
IF ERRORLEVEL 1 goto BADCHECKER
:GOODCHECKER
ping 8.8.8.8
IF ERRORLEVEL 0 (ping 8.8.8.8)
IF ERRORLEVEL 1 goto BADCHECKER
IF ERRORLEVEL 0 (ping 8.8.8.8)
IF ERRORLEVEL 1 goto BADCHECKER
IF ERRORLEVEL 0 (ping 8.8.8.8)
IF ERRORLEVEL 1 goto BADCHECKER
IF ERRORLEVEL 0 goto GOOD
IF ERRORLEVEL 1 goto BADCHECKER
:BADCHECKER
ping 8.8.8.8
IF ERRORLEVEL 1 (ping 8.8.8.8)
IF ERRORLEVEL 0 goto GOODCHECKER
IF ERRORLEVEL 1 (ping 8.8.8.8)
IF ERRORLEVEL 0 goto GOODCHECKER
IF ERRORLEVEL 1 goto BAD
IF ERRORLEVEL 0 goto GOODCHECKER
:GOOD
echo set WshShell = WScript.CreateObject("WScript.Shell") > %tmp%\tmp.vbs
echo WScript.Quit (WshShell.Popup( "The internet is working at the moment. You can close out of the console window now, or let it run a little longer to be absolutely sure." ,10 ,":)", 0)) >> %tmp%\tmp.vbs
cscript /nologo %tmp%\tmp.vbs
goto PING
:BAD
echo set WshShell = WScript.CreateObject("WScript.Shell") > %tmp%\tmp.vbs
echo WScript.Quit (WshShell.Popup( "The internet is not working at the moment." ,5 ,":(", 0)) >> %tmp%\tmp.vbs
cscript /nologo %tmp%\tmp.vbs
goto PING
编辑:
根据进一步的研究,完成了对脚本的重新编写。现在按预期工作。 这是任何有兴趣的人:
@echo off
TITLE = Pinger
:INITIAL
echo Please wait while I check your connection.
ping 8.8.8.8 | findstr /r /c:"[0-9] *ms" > nul
if %errorlevel% == 0 (goto GOODCHECKER) ELSE (goto BADCHECKER)
:GOODCHECKER
ping 8.8.8.8 | findstr /r /c:"[0-9] *ms" > nul
if %errorlevel% == 0 (ping 8.8.8.8 | findstr /r /c:"[0-9] *ms" > nul) ELSE goto BADCHECKER
echo Still checking.
if %errorlevel% == 0 (ping 8.8.8.8 | findstr /r /c:"[0-9] *ms" > nul) ELSE goto BADCHECKER
if %errorlevel% == 0 (ping 8.8.8.8 | findstr /r /c:"[0-9] *ms" > nul) ELSE goto BADCHECKER
if %errorlevel% == 0 goto GOOD
:BADCHECKER
ping 8.8.8.8 | findstr /r /c:"[0-9] *ms" > nul
if %errorlevel% == 1 (ping 8.8.8.8 | findstr /r /c:"[0-9] *ms" > nul) ELSE goto GOODCHECKER
echo Still checking.
if %errorlevel% == 1 (ping 8.8.8.8 | findstr /r /c:"[0-9] *ms" > nul) ELSE goto GOODCHECKER
if %errorlevel% == 1 (ping 8.8.8.8 | findstr /r /c:"[0-9] *ms" > nul) ELSE goto GOODCHECKER
if %errorlevel% == 1 goto BAD
:GOOD
color 2F
echo The internet is working! :)
echo set WshShell = WScript.CreateObject("WScript.Shell") > %tmp%\tmp.vbs
echo WScript.Quit (WshShell.Popup( "The internet is working. You can close out of the green window now, or hit OK to have me check again." ,30 ,":)", 0)) >> %tmp%\tmp.vbs
cscript /nologo %tmp%\tmp.vbs
echo I'll check again.
goto GOODCHECKER
:BAD
color 4F
echo The internet is not working :(
echo set WshShell = WScript.CreateObject("WScript.Shell") > %tmp%\tmp.vbs
echo WScript.Quit (WshShell.Popup( "The internet is not working at the moment. You can close out of the red window now, or hit OK to have me check again." ,20 ,":(", 0)) >> %tmp%\tmp.vbs
cscript /nologo %tmp%\tmp.vbs
echo Alright, I'll check again.
goto BADCHECKER
答案 0 :(得分:1)
您的:在您通知用户互联网无法正常工作后,BAD标记会返回到PING标记。只需删除最后一个转到PING 语句。
以下是使用更简洁方法的脚本的不同版本:
@echo off
set pingIP=8.8.8.8
:doPing
cls
echo Pinging %pingIP%
ping -n 3 -w 100 %pingIP% > nul
set result=%ERRORLEVEL%
if %result%==1 (call :failure) ELSE (call :success)
goto :doPing
:success
Echo The internet is working at the moment.
Echo You can close out of the console window now, or let it run a little longer to be absolutely sure.
set /p response=Stop monitoring? (Y/N)
if /i "%response%"=="y" (exit) else (goto :eof)
:failure
Echo The internet is not working at the moment.
set /p response=Stop monitoring? (Y/N)
if /i "%response%"=="y" (exit) else (goto :eof)
答案 1 :(得分:0)
@Filipus的解决方案,带有一些颜色;)
@echo off
Title Check Internet Connexion
Mode Con Cols=60 lines=10
set pingIP=8.8.8.8
Call:init
:doPing
cls
Call:Color 0E "Pinging..... %pingIP%" 1
echo(
ping -n 3 -w 100 %pingIP% > nul
set result=%ERRORLEVEL%
if '%result%' EQU '1' (call :failure) ELSE (call :success)
goto :doPing
:success
Call:Color 0A "The internet is working at the moment." 1
Call:Color 0A "You can close out of the console window now," 1
Call:Color 0A "Or let it run a little longer to be absolutely sure." 1
echo(
set /p response=Stop monitoring? (Y/N)
if /i "%response%"=="y" (exit) else (goto :eof)
:failure
cls
echo(
Call:Color 0C "The internet is not working at the moment." 1
timeout /t 5 /nobreak >nul
goto :doPing
:init
prompt $g
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
exit /b
:color
set nL=%3
if not defined nL echo requires third argument & pause > nul & goto :eof
if %3 == 0 (
<nul set /p ".=%bs%">%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
) else if %3 == 1 (
echo %bs%>%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
)
exit /b