I'm trying to create a batch file that will constantly ping google.com and check the response time - "time=Xms".
- If the time <= 39ms the text of that ping(or the background) should
be green.
- If the time > 40ms and < 80ms the text of that ping(or the
background) should turn orange.
- If the time >= 80ms the text of that ping(or the background) should
turn red.
I have this batch at the moment which pings google every 3 seconds changes the background from green to red if the response fails:
@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
This works fine but I don't know how to test response times.
There are some quirks.
a) you have to get the desired value of ping
to a variable. Use a for
to get it.
b) you can't compare it directly, because if
compares strings, not numbers (2
is bigger than 10
). Add leading zeros to the string (and afterwards cut it to a fixed length)
c) cmd
has no native way of coloring single lines (or characters). It can be done with pure cmd
, but I think, powershell
is a much better way to do it.
@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
Try like this :
@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
Because sometimes PowerShell isn't an option, I figured I'd piece some things together and do this all as a batch script.
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
:: https://stackoverflow.com/questions/4339649/how-to-have-multiple-colors-in-a-windows-batch-file/10407642#10407642
: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