Batch file 简单的Pinger批处理脚本,带有;“工作”;及;“不工作”;没有互联网,结果无限运行

Batch file 简单的Pinger批处理脚本,带有;“工作”;及;“不工作”;没有互联网,结果无限运行,batch-file,Batch File,在返回肯定消息框或否定消息框之前,我制作了一个简单的批处理脚本来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

在返回肯定消息框或否定消息框之前,我制作了一个简单的批处理脚本来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

在您通知用户internet不工作后,您的: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)

带有一些颜色的@Filipus解决方案;)

@echo关闭
标题检查互联网连接
模式Con Cols=60行=10行
设置pingIP=8.8.8.8
呼叫:init
:兴奋剂
cls
调用:颜色0E“Pinging….%pingIP%”1
回音(
ping-n 3-w 100%pingIP%>nul
设置结果=%ERRORLEVEL%
如果“%result%”等于“1”(调用:失败),则为其他(调用:成功)
后藤:兴奋剂
:成功
电话:Color 0A“目前互联网正在运行。”1
呼叫:颜色0A“您现在可以关闭控制台窗口”,1
呼叫:颜色0A“或让它运行更长一点以确保绝对可靠。”1
回音(
设置/p响应=停止监控?(是/否)
如果/i“%response%”==“y”(退出)其他(转到:eof)
:失败
cls
回音(
呼叫:Color 0C“互联网目前不工作。”1
超时/t 5/nobreak>nul
后藤:兴奋剂
:init
提示$g
对于/F中的“delims=。”%%a(“'prompt$H.%%b in(1)do rem””)请设置“BS=%%a”
退出/b
:颜色
设置nL=%3
如果未定义nL echo,则需要第三个参数&pause>nul&goto:eof
如果%3==0(
%2&findstr/v/a:%1/r“^$%2编号和删除%2>&1转到:eof
)如果%3==1,则为else(
回显%bs%>%2&findstr/v/a:%1/r“^$%2编号和删除%2>&1&goto:eof
)
退出/b

批处理文件的名称是什么?嗯。“pinger-Copy.bat”为什么?如果你经常运行它,谷歌会阻塞你的ICMP流量。如果你打算使用ICMP来测试互联网连接,你应该考虑Puring周界或边缘路由器。一般来说,ping洪水不是检查连通性的好方法。从角度来看,如果你的接收器被阻塞了,你会注入更多的水来测试它是否被阻塞了?在交通方面,派更多的车去堵塞的街道看看街道是否畅通是个好主意吗?我如何ping周界或边缘路由器?
errorlevel
ping
不可靠。最好使用
ping…;查找“TTL=”
相反。虽然:GOOD标记也有作用,但它仍能按预期工作。它们是故意这样做的。我希望在用户点击OK或消息框超时后脚本继续。我将尝试删除goto,看看这是否解决了问题。好的,遇到了新问题。我关闭了Wi-Fi,删除了最后一条goto语句,然后测试了脚本,它给了我一个“工作”结果。我删除了@echo以查看它哪里出了问题,结果发现一些失败的ping返回的ErrorValue为0。在你看来,这是怎么发生的/为什么发生的?对不起,我刚刚指出了我脑海中最明显的问题,但有一个更明显的问题:无论何时测试ERRORLEVEL值,你都必须按相反的顺序进行。所以c首先检查1的值,然后检查0的值。实际上,整个脚本可以(应该?)被重写。你真的需要让它像现在一样在一个循环中工作吗?或者你想让它在某个时候退出吗?因为现在设置的方式,它将永远运行…只是在我的原始答案中添加了我自己版本的同一脚本,以防你想尝试。这也不能解决它。我相信问题是不一致/不准确的pingerrorlevel结果(“0”,当它应该是“1”时,反之亦然)。我通过专门检查结果中是否存在延迟值来解决这个问题。另外,是的,我希望脚本能够持续运行,因为这是我正在编写的另一个脚本的一部分(我现在已经完成了),它在后台运行,检查连接,并在它停机时“修复”它,因为我使用我的电脑不断地传输数据,不能全天候地在它面前捕捉停机时间。
@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