2

我正在制作一个批处理脚本,用于检测来自 ping 回复的高延迟并通过我的连接通知我。我的脚本通过将 ping 回复保存到日志文件来检测 300 毫秒到 700 毫秒的数字。

ping -n 15 192.206.238.4 | find /i "Reply" > nul > %cd%\acttracing.log

然后使用 findstr 搜索 300 毫秒到 700 毫秒的内容,如果这样,它将转到 :sound 并在其中播放警告通知。

nul findstr /c:"time=680" acttracing.log && (
  echo "High Latency" was found.
) || (
  echo "High Latency" was NOT found.
)
If %ERRORLEVEL% equ 0 goto :sound

nul findstr /c:"time=685" acttracing.log && (
  echo "High Latency" was found.
) || (
  echo "High Latency" was NOT found.
)
If %ERRORLEVEL% equ 0 goto :sound

nul findstr /c:"time=690" acttracing.log && (
  echo "High Latency" was found.
) || (
  echo "High Latency" was NOT found.
)
If %ERRORLEVEL% equ 0 goto :sound

nul findstr /c:"time=695" acttracing.log && (
  echo "High Latency" was found.
) || (
  echo "High Latency" was NOT found.
)
If %ERRORLEVEL% equ 0 goto :sound

nul findstr /c:"time=700" acttracing.log && (
  echo "High Latency" was found.
) || (
  echo "High Latency" was NOT found.
)
If %ERRORLEVEL% equ 0 goto :sound

现在的问题是,即使从保存的日志文件中找到一个高延迟数字,它也经常通知我,这显然不会影响我的连接。仅当它检测到 10 次 ERRORLEVEL 时,如何使其进入:声音?有没有办法让我的脚本比从 300 写到 700 更短?

提前致谢....

4

1 回答 1

0

使用for命令获取ping的输出,tokenize/split/explode,获取响应时间,最后比较。

set ip=google.com
for /f "skip=2 delims=" %%a in ('ping %ip% -n 1') do (
set "var=%%a"
goto next
)
:next
for /f "tokens=1-6" %%a in ("%var%") do set var=%%e
set time=%var:~5,-2%
if %time% GTR 300 (
if %time% LSS 700 (
echo high latency
))

它非常简单,但有点冗长(希望这不是问题?)。

于 2016-02-11T13:02:12.067 回答