批量检查互联网连接 [英] checking internet connectivity with batch

查看:67
本文介绍了批量检查互联网连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,最近我的互联网连接确实不尽人意,因此我正在尝试收集尽可能多的有关中断持续时间和持续时间的数据.我尝试了一些连接监视"程序,但是它们并不能按我希望的那样工作,所以我决定制作一个.

So recently my internet connection have been really unsatisfactory so I'm trying to gather as much data as I can about when and for how much time the outages last. I tried some "connectivity monitoring" programs but they don't work as I want them to, so I decided to make one.

我是一个完整的菜鸟,但在过去一个小时的搜索中,我想到了这一点:

I am a complete noob in batch but from googling stuff for the past hour I came up with this:

SET status="

:start_test
timeout 5
ping -n 2 -w 700 www.google.com | find "bytes="
IF %ERRORLEVEL% EQU 0 (
    SET internet=Connected to the internet.

) ELSE (
    SET internet=Not connected to the internet.
)
IF %status%==%internet%(
goto :start_test
) ELSE (
goto :teller 
)

:teller
echo ------------------------------------------
echo %internet%
@echo %time%
echo ------------------------------------------
SET status=%internet%
goto :start_test

tl; dr-它通过固定google.com来检查互联网连接,并在每次互联网连接状态发生变化时写一条消息

tl;dr- it checks the internet connection by pining google.com and writes a massage every time the internet connection status changes

但是问题是它不起作用,我不知道为什么,当我尝试运行它时,控制台会打开,经过前几行并自行关闭.

but the problem is that it doesn't work and I don't know why, when I try to run it the console opens, goes through the first few lines and closes itself.

帮助

现在可以正常工作,这就是文件现在的外观(就像Mofi所说的一样):

it works now, this is how the file looks now (just like what Mofi said):

@echo off
SET "status="

:start_test
timeout 5
ping -n 2 -w 700 www.google.com | find "bytes="
IF %ERRORLEVEL% EQU 0 (
    SET internet=Connected to the internet.

) ELSE (
    SET internet=Not connected to the internet.
)
IF "%status%"=="%internet%" (
goto :start_test
) ELSE (
goto :teller 
)

:teller
echo ------------------------------------------
echo %internet%
@echo %time%
echo ------------------------------------------
SET "status=%internet%"
goto :start_test

推荐答案

批处理变量与常量的比较文件的答案失败在第一章中介绍了如何通过不是双击调试批处理文件,而是打开命令提示符窗口并从该窗口中运行批处理文件以查看由语法错误引起的错误消息,从而调试批处理文件导致退出批处理文件执行.

The answer on batch file comparison of variable with constant fails explains in first chapter how to debug a batch file by not double clicking on it, but by opening a command prompt window and running the batch file from within this window to see error messages caused by syntax errors which result in exiting batch file execution.

SET status="

此行用值"定义环境变量 status .

This line defines the environment variable status with the value ".

我想您想使用:

SET "status="

此行删除了一个可能存在的环境变量 status ,但是您通过省略第一个双引号将其编码为错误.

This line deletes a perhaps existing environment variable status, but you coded this wrong by omitting the first double quote.

命令 PING 将成功的 ERRORLEVEL 设置为 0 ,如果无法将目标设置为错误,则将 1 设置为 1 ping通.

The command PING sets ERRORLEVEL to 0 on success and 1 on an error like target could not be pinged.

但是由于某些未知的原因,您不想评估 PING 的退出代码,而是通过 Find 解析 PING 的输出,然后评估 FIND 的退出代码.

But for some unknown reason you don't want to evaluate the exit code of PING and instead parse output of PING by FIND and evaluate the exit code of FIND.

在我看来,完全不需要使用 Find .

The usage of FIND is in my point of view completely unnecessary here.

根据此退出代码,对 FIND 退出代码的求值已经足够继续,但是根据 ERRORLEVEL的值,将两个带空格的不同字符串分配给环境变量.

The evaluation of the exit code of FIND would be already enough to continue depending on this exit code, but two different strings with spaces are assigned to an environment variable depending on value of ERRORLEVEL.

再使用一个 IF 进行分支,或多或少地取决于 PING 的退出代码.

One more IF is used to branch depending more or less on exit code of PING.

这是您的错误,由于语法错误导致退出批处理.

IF %status%==%internet%(

在第一次比较时扩展

IF " == Connected to the internet. (

IF " == Not connected to the internet. (

Windows命令解释器无法正确处理这两行,这是由输出到控制台的语法错误消息指示的,当从命令提示符窗口中执行批处理文件时可以读取该错误消息.

Both lines can't be correct processed by Windows command interpreter indicated by a syntax error message output to console which can be read when the batch file is executed from within a command prompt window.

比较包含命令分隔符SPACE的字符串需要将两个字符串都括起来以用双引号进行比较,即使用:

Comparing strings containing command separator character SPACE requires enclosing both strings to compare in double quotes, i.e. use:

IF "%status%" == "%internet%" (

但是由于环境变量 status 被定义为单引号,所以即使这样做也无济于事,这导致左字符串最终在第一次比较时出现""再次导致语法错误.

But even this would not help here because of environment variable status is defined with a single double quote which results in left string being finally on first comparison """ which results again in a syntax error.

注意:字符串比较中包含双引号字符.

Note: The double quote characters are included in the string comparison.

下面的代码块也很有趣:

The block below is also interesting:

IF %status%==%internet%(
goto :start_test
) ELSE (
goto :teller 
)

:teller

ELSE 分支用于跳转到如果根本不存在 ELSE 分支的情况下将要执行的行.因此,完全不需要 ELSE 分支.

The ELSE branch is for jumping to the lines which would be executed in any case if the ELSE branch would not exist at all. So the complete ELSE branch is completely unnecessary.

出于上述所有原因,我建议将批处理代码简化为:

For all the reasons above, I suggest to simplify the batch code to:

@echo off
set "Status=2"
:ConnectionTest
%SystemRoot%\System32\ping.exe -n 2 -w 700 www.google.com >nul
if errorlevel 1 (
    if not "%Status%" == "1" echo %TIME%  Not connected to the internet.
    set "Status=1"
) else (
    if not "%Status%" == "0" echo %TIME%  Connected to the internet.
    set "Status=0"
)
%SystemRoot%\System32\timeout.exe /T 5 >nul
goto ConnectionTest

在这里可以省略所有双引号并使用:

It would be possible here to omit all double quotes and use:

@echo off
set Status=2
:ConnectionTest
%SystemRoot%\System32\ping.exe -n 2 -w 700 www.google.com >nul
if errorlevel 1 (
    if not %Status% == 1 echo %TIME%  Not connected to the internet.
    set Status=1
) else (
    if not %Status% == 0 echo %TIME%  Connected to the internet.
    set Status=0
)
%SystemRoot%\System32\timeout.exe /T 5 >nul
goto ConnectionTest

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo/?
  • 转到/?
  • 如果/?
  • ping/?
  • 设置/?
  • 超时/?
  • echo /?
  • goto /?
  • if /?
  • ping /?
  • set /?
  • timeout /?

还要阅读有关 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆