Ping Batch File Color Error Levels

2.2k views Asked by At

Below is the batch file I am currently using, but I would like to chage it so that it is not just green if the link is good and red if it times out. I would like it to turn yellow if response is not within a particular range. So if my RTT range is not within 130-190 ms it would turn yellow. Thanks in advance

I want the screen to be green if it falls in a range, yellow if it falls out of the range, and red it the request times out.

echo off & cls

TITLE = Test

:top

ping -n 1 8.8.8.8 | FIND "TTL="

IF ERRORLEVEL 1 (SET OUT=4F & echo Request timed out.) ELSE (SET OUT=2F)

color %OUT%

ping -n 3 -w 1000 127.0.0.1 >nul

GoTo top
2

There are 2 answers

13
MC ND On BEST ANSWER

edited to adapt to comments

@echo off
    setlocal enableextensions enabledelayedexpansion

    rem Get address from command line
    set "address=%~1"
    if not defined address set "address=127.0.0.1"

    rem Configure levels and colors 
    rem The format is initialValue:color in value descending format
    set "levels=9000:4f 500:5f 130:e0 0:a0"

    rem infinite loop
    for /l %%i in () do (
        rem retrieve information from ping command
        set "rtt=9999"
        set "ttl=?"
        for /f "tokens=3,4 delims==^<" %%a in (
            'ping -n 1 "%address%" ^| find "TTL="'
        ) do for /f "tokens=1 delims=m" %%c in ("%%a") do (
            set /a "rtt=%%c"
            set "ttl=%%b"
        )

        rem retrieve color
        set "color="
        for %%z in (%levels%) do for /f "tokens=1,2 delims=:" %%a in ("%%z") do (
            if not defined color if !rtt! geq %%a set "color=%%b"
        )

        rem show information
        if defined color color !color!
        echo(!time! - %address% - rtt[!rtt!] ttl[!ttl!]

        rem save to log
        for /f "tokens=1-4 delims=.:-/ " %%a in ("!date!") do (
            >> "pingLog_%%a%%b%%c%%d.txt" echo(!time! - %address% - rtt[!rtt!] ttl[!ttl!]
        )

        rem wait and repeat the process
        ping -n 3 localhost >nul 2>nul 
    )

It just repeat an infinite loop checking the indicated address (readed from command line in this code).

In each iteration it is determined the current rtt from the ping command, the color selected according to the rtt and the information echoed to console with the colors changed.

To get the rtt, a ping is executed. If the host is active it will be a TTL= string in the output. If the line is found, it is tokenized using the characters =< as delimiters to get the third token (where the rtt is located) and then the m from ms is used to separate the numeric value of the rtt.

With the rtt time, the values (the pair level:color) in the list of levels is iterated. For each value, the level and color are separated and the level tested agains the rtt. If the rtt is greater or equal to the level, we have found the adecuated color.

Color is changed, information printed and the code waits before starting a new iteration

0
Justin On

I do believe this ties in with Batch ERRORLEVEL ping response, so I'll just repost the answer from there.

Credit to Jon for answer~

@echo off
for /f %%i in ('ping racer ^| find /c "(0%% loss)"') do SET MATCHES=%%i
echo %MATCHES%

This prints 0 if the ping failed, 1 if it succeeded. I made it look for just "0% loss" (not specifically 4 pings) so that the number of pings can be customized.

The percent sign has been doubled so that it's not mistaken for a variable that should be substituted.

The FOR trick serves simply to set the output of a command as the value of an environment variable.

You need to edit "racer" to the ip. You can also change "Matches" if you'd like a different variable.