创建一个不断 ping google 并测试响应时间的批处理文件

Creating a Batch file that pings google constantly and testing the response time

我正在尝试创建一个批处理文件,它将不断 ping google.com 并检查响应时间 - "time=Xms"。

我现在有这个批次,如果响应失败,每 3 秒 ping google 将背景从绿色变为红色:

    @echo off
:color 97

:start
PING -n 1 www.google.com 
call :color
goto :start

:color
    IF %ERRORLEVEL% EQU 0 (
        COLOR 27
    ) else (
        COLOR 47
    ping -n 1 127.0.0.1 >nul
    COLOR 74
    ping -n 1 127.0.0.1 >nul
    COLOR 47
    )
    ping -n 3 127.0.0.1 >nul
    GOTO:EOF

这工作正常,但我不知道如何测试响应时间。

这样试试:

@echo off
Title Echo Ping Reply
mode con cols=57 lines=6
::Choose how many seconds you must wait for the refresh
Set MyPause=3
:color 97

:start
CLS
echo.
ping www.google.com | findstr /i "TTL"
Timeout /Nobreak /t %MyPause% > Nul
call :color
goto :start

:color
    IF %ERRORLEVEL% EQU 0 (
        COLOR 27
    ) else (
        COLOR 47
    ping -n 1 127.0.0.1 >nul
    COLOR 74
    ping -n 1 127.0.0.1 >nul
    COLOR 47
    )
    ping -n 3 127.0.0.1 >nul
    GOTO:EOF

有一些怪癖。

a) 你必须将 ping 的期望值赋给一个变量。使用 for 获取它。

b) 你不能直接比较它,因为 if 比较的是字符串,而不是数字(210 大)。在字符串中添加前导零(然后将其剪切为固定长度)

c) cmd 没有给单行(或字符)着色的原生方法。可以使用纯 cmd 来完成,但我认为 powershell 是更好的方法。

@echo off
:loop
set "tim=unreachable"
for /f "tokens=7 delims== " %%i in ('PING -n 1 www.google.com ^|find "TTL"') do set "tim=%%i"
set "ti=0000%tim%"
set "ti=%ti:~-6,-2%"
if %ti% leq 0040 powershell write-host -foreground green %tim% & goto :loop
if %ti% leq 0080 powershell write-host -foreground yellow %tim% & goto :loop
powershell write-host -foreground red %tim% & goto :loop

因为有时 PowerShell 不是一个选项,我想我会把一些东西拼凑在一起并作为批处理脚本来完成。
Writing a batch file to detect ping anomalies
How to have multiple colors in a Windows batch file?

:: usage: badpings-color.bat [ip adress | hostname] 

@echo off
set /a warnlimit=79 :: greater than this value is red
                    :: between the two values is yellow
set /a goodlimit=39 :: less than or equal to this value is green

if "%1"=="" (
    set pingdest=google.com
    ) else (
    set pingdest=%1
    )

echo Pinging %pingdest%.
::echo Logging replies over %limit%ms.
echo Press Ctrl+C to end.

:Loop
for /f "usebackq tokens=1-6" %%a in (`ping -n 1 %pingdest% ^| findstr "Request Reply request"`) do (
    set var=%%a %%b %%c %%d %%e %%f
    set pingtimestr=%%e
    )

if "%pingtimestr%"=="find" (
    echo Ping request could not find host %pingdest%. Please check the name and try again.
    goto End
    ) 
if "%pingtimestr%"=="host" (
    set /a pingtime=%warnlimit%+1   
    ) 
if not defined pingtimestr (
    set /a pingtime=%warnlimit%+1
    )
if "%pingtimestr:~0,4%"=="time" (
    set /a pingtime=%pingtimestr:~5,-2% 
    )

if %pingtime% LEQ %goodlimit% (
    call :c 02 "[%time%] %var%" /n
    goto EndOfLoop
    )
if %pingtime% LEQ %warnlimit% (
    call :c 0E "[%time%] %var%" /n
    goto EndOfLoop
    )
if %pingtime% GTR %warnlimit% (
    call :c 0C "[%time%] %var%" /n
    goto EndOfLoop
    )

:EndOfLoop
timeout /t 1 /nobreak >nul
Goto Loop

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: this section is from 
:: 

:c
setlocal enableDelayedExpansion

:colorPrint Color  Str  [/n]
setlocal
set "s=%~2"
call :colorPrintVar %1 s %3
exit /b

:colorPrintVar  Color  StrVar  [/n]
if not defined DEL call :initColorPrint
setlocal enableDelayedExpansion
pushd .
':
cd \
set "s=!%~2!"
:: The single blank line within the following IN() clause is critical - DO NOT REMOVE
for %%n in (^"^

^") do (
  set "s=!s:\=%%~n\%%~n!"
  set "s=!s:/=%%~n/%%~n!"
  set "s=!s::=%%~n:%%~n!"
)
for /f delims^=^ eol^= %%s in ("!s!") do (
  if "!" equ "" setlocal disableDelayedExpansion
  if %%s==\ (
    findstr /a:%~1 "." "\'" nul
    <nul set /p "=%DEL%%DEL%%DEL%"
  ) else if %%s==/ (
    findstr /a:%~1 "." "/.\'" nul
    <nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%"
  ) else (
    >colorPrint.txt (echo %%s\..\')
    findstr /a:%~1 /f:colorPrint.txt "."
    <nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
  )
)
if /i "%~3"=="/n" echo(
popd
exit /b

:initColorPrint
for /f %%A in ('"prompt $H&for %%B in (1) do rem"') do set "DEL=%%A %%A"
<nul >"%temp%\'" set /p "=."
subst ': "%temp%" >nul
exit /b

:cleanupColorPrint
2>nul del "%temp%\'"
2>nul del "%temp%\colorPrint.txt"
>nul subst ': /d
exit /b

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:End