使用批处理脚本检测延迟

时间:2016-02-11 08:49:43

标签: batch-file connection latency pinging

我正在创建一个批处理脚本,可以检测ping响应的高延迟并通过我的连接通知我。我的脚本通过保存对日志文件的ping响应来检测300ms到700ms的数字。

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

然后使用findstr搜索300毫秒到700毫秒的内容,如果这样,它将转到:声音,它会播放警告通知。

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更短?

提前感谢....

1 个答案:

答案 0 :(得分: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
))

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