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 afor
to get it.b) you can't compare it directly, because
if
compares strings, not numbers (2
is bigger than10
). 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 purecmd
, but I think,powershell
is a much better way to do it.Try like this :
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?