使用批处理检查互联网连接
checking internet connectivity with batch
所以最近我的互联网连接真的不能令人满意,所以我正在尝试收集尽可能多的关于中断持续时间和持续时间的数据。我尝试了一些 "connectivity monitoring" 程序,但它们无法正常工作,所以我决定制作一个。
我是一个完全的菜鸟,但是在过去一个小时的谷歌搜索中我想到了这个:
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 来检查互联网连接,并在每次互联网连接状态发生变化时写一条信息
但问题是 它不起作用 我不知道为什么,当我尝试 运行 它时,控制台打开,通过前几行并自行关闭。
帮助
编辑:
它现在可以工作了,这就是文件现在的样子(就像 Mofi 所说的那样):
@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
上的答案在第一章中解释了如何调试批处理文件 而不是 双击它,而是打开命令提示符 window 和 运行 此 window 中的批处理文件,以查看由导致退出批处理文件执行的语法错误引起的错误消息。
SET status="
这一行定义了环境变量status
,值为"
。
我想你想使用:
SET "status="
此行删除了一个可能存在的环境变量 status
,但是您通过省略第一个双引号将其编码错误。
命令 PING 将 ERRORLEVEL 设置为 0
成功时设置 1
错误时 target 可以不被 ping。
但由于某些未知原因,您不想评估 PING 的退出代码,而是通过 PING 解析输出=78=]FIND 并评估 FIND.
的退出代码
在我看来,FIND 的用法在这里完全没有必要。
FIND 的退出代码的评估已经足以继续取决于此退出代码,但是两个不同的带空格的字符串根据值分配给环境变量ERRORLEVEL.
另一个IF用于分支,或多或少取决于PING.
的退出代码
这是您的错误,由于语法错误导致退出批处理。
IF %status%==%internet%(
在与
的第一次比较中扩展
IF " == Connected to the internet. (
或到
IF " == Not connected to the internet. (
Windows命令解释器无法正确处理这两行,由输出到控制台的语法错误消息指示,当从命令提示符中执行批处理文件时可以读取该消息window .
比较包含命令分隔符 SPACE 的字符串需要用双引号将要比较的两个字符串括起来,即使用:
IF "%status%" == "%internet%" (
但即使这样也无济于事,因为环境变量 status
是用单个双引号定义的,这导致左字符串最终在第一次比较 """
中再次导致语法错误.
注意:双引号字符包含在字符串比较中。
下面的方块也很有趣:
IF %status%==%internet%(
goto :start_test
) ELSE (
goto :teller
)
:teller
ELSE 分支用于跳转到如果 ELSE 分支根本不存在则在任何情况下都会执行的行.所以完整的 ELSE 分支是完全没有必要的。
鉴于上述所有原因,我建议将批处理代码简化为:
@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
这里可以省略所有双引号并使用:
@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
要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
echo /?
goto /?
if /?
ping /?
set /?
timeout /?
另请阅读有关 using command redirection operators 的 Microsoft 文章。
所以最近我的互联网连接真的不能令人满意,所以我正在尝试收集尽可能多的关于中断持续时间和持续时间的数据。我尝试了一些 "connectivity monitoring" 程序,但它们无法正常工作,所以我决定制作一个。
我是一个完全的菜鸟,但是在过去一个小时的谷歌搜索中我想到了这个:
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 来检查互联网连接,并在每次互联网连接状态发生变化时写一条信息
但问题是 它不起作用 我不知道为什么,当我尝试 运行 它时,控制台打开,通过前几行并自行关闭。
帮助
编辑: 它现在可以工作了,这就是文件现在的样子(就像 Mofi 所说的那样):
@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
SET status="
这一行定义了环境变量status
,值为"
。
我想你想使用:
SET "status="
此行删除了一个可能存在的环境变量 status
,但是您通过省略第一个双引号将其编码错误。
命令 PING 将 ERRORLEVEL 设置为 0
成功时设置 1
错误时 target 可以不被 ping。
但由于某些未知原因,您不想评估 PING 的退出代码,而是通过 PING 解析输出=78=]FIND 并评估 FIND.
的退出代码在我看来,FIND 的用法在这里完全没有必要。
FIND 的退出代码的评估已经足以继续取决于此退出代码,但是两个不同的带空格的字符串根据值分配给环境变量ERRORLEVEL.
另一个IF用于分支,或多或少取决于PING.
的退出代码这是您的错误,由于语法错误导致退出批处理。
IF %status%==%internet%(
在与
的第一次比较中扩展IF " == Connected to the internet. (
或到
IF " == Not connected to the internet. (
Windows命令解释器无法正确处理这两行,由输出到控制台的语法错误消息指示,当从命令提示符中执行批处理文件时可以读取该消息window .
比较包含命令分隔符 SPACE 的字符串需要用双引号将要比较的两个字符串括起来,即使用:
IF "%status%" == "%internet%" (
但即使这样也无济于事,因为环境变量 status
是用单个双引号定义的,这导致左字符串最终在第一次比较 """
中再次导致语法错误.
注意:双引号字符包含在字符串比较中。
下面的方块也很有趣:
IF %status%==%internet%(
goto :start_test
) ELSE (
goto :teller
)
:teller
ELSE 分支用于跳转到如果 ELSE 分支根本不存在则在任何情况下都会执行的行.所以完整的 ELSE 分支是完全没有必要的。
鉴于上述所有原因,我建议将批处理代码简化为:
@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
这里可以省略所有双引号并使用:
@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
要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
echo /?
goto /?
if /?
ping /?
set /?
timeout /?
另请阅读有关 using command redirection operators 的 Microsoft 文章。