0

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
phifer2088
  • 15
  • 1
  • 6

2 Answers2

1

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

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • I have not done this much but from that I am reading the (address=%~1) should read address=8.8.8.8 – phifer2088 Nov 12 '14 at 12:03
  • @phifer, just coded to be reused giving the address as an argument to the batch file. It should be called something like `rttMonitor.cmd 8.8.8.8`, or, of course, the line in the file can be changed to `set "address=8.8.8.8"` – MC ND Nov 12 '14 at 12:09
  • Ok, now I am running it, but the TTL is currently 251 so it should be yellow but I am showing green. Also is there a way to make the response to show the current response time. – phifer2088 Nov 12 '14 at 12:16
  • @phifer, the TTL is not the RTT. The value being used is the `time=...ms` before the TTL information. – MC ND Nov 12 '14 at 12:17
  • I see. My response time is alternating from 9999 to a more correct number around 308. And it is red but should be green. Also Thanks for the help! – phifer2088 Nov 12 '14 at 12:21
  • @phifer2088, code updated to include the ttl. In the code, for each test, the rtt is initialized to 9999. If there is no response from the host, this value is not changed. So, 9999 is a timeout. – MC ND Nov 12 '14 at 12:23
  • One last part. If the time is greater than the range it is turning red. How can I make this turn Yellow or at least not red? – phifer2088 Nov 12 '14 at 13:15
  • @phifer2088, adjust the levels. `set "levels=9000:4f 130:e0 0:a0"` – MC ND Nov 12 '14 at 13:19
  • @phifer2088, or `set "levels=9000:4f 500:5f 130:e0 0:a0"` – MC ND Nov 12 '14 at 13:21
  • How would I go about adding a feature to log the results that are displayed in this batch file? I have tried a few methods but have had no success. Thanks – phifer2088 Dec 02 '14 at 06:03
  • Currently the end of my batch file is " rem wait and repeat the process ping -n 3 localhost >nul 2>nul ) >>H:\netmon\AUAB\somelogfile.txt" – phifer2088 Dec 02 '14 at 06:22
  • This gives me a log, but does display the ping information anymore. I would also like the batch file to automatically have the date in the filename. So at midnight it would start a new file – phifer2088 Dec 02 '14 at 06:28
  • @phifer2088, answer updated. Adapt log file as needed. If you redirect all the output, you will have nothing on console. It is necessary to output the information twice or use some `tail` utility. Next time, if the problem is not directly related to the problem in the original question (ping & color vs log redirection), it is better to start a new question so anyone searching for the same problem could find it. – MC ND Dec 02 '14 at 06:59
0

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.

Community
  • 1
  • 1
Justin
  • 3
  • 2