Search code examples
windowscmdping

Manipulate cmd ping color based on time


My internet is not always working properly and I'd like to check the quality based on the cmd windows tool. I believe it's a task simple enough for it to handle.
I've begun by making a shortcut so I can have easy access to the command:

    C:\Windows\System32\PING.EXE 8.8.8.8 -t

Now I was trying to transform the cmd ping command into a visually responsive one based on the output. I'd like to make the color change according to the time response.
After looking and not finding anything related, I believe it's either impossible or no one has ever tried.

Thank you very much :)
PD: (In case there was anything unclear just ask and I'll gladly answer)


Solution

  • Based on Magoo's post, I wrote this little batch program. It asks for the target, the number of requests to make, the max time allowed and the time between requests and then prints in red if the request is over the time max, otherwise it sums the number of requests. It includes timestamp to be more accurate.

    Copy and paste in a text file and name it with extension ".bat" (But don't name it "ping.bat" otherwise the program will enter in an infinite loop).

    REM CMD PING TOOL
    REM By Daweb
    REM https://stackoverflow.com/users/3779294/daweb    
    
    @ECHO OFF
    
    REM Needed for Line colored
    SETLOCAL EnableDelayedExpansion
    FOR /F "tokens=1,2 delims=#" %%a IN ('"PROMPT #$H#$E# & echo on & for %%b in (1) do rem"') do (
      SET "DEL=%%a"
    )
    for /f %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"
    
    
    ECHO *****************
    ECHO * CMD PING TOOL *
    ECHO *****************
    
    REM Start
    :start
    
    ECHO.
    ECHO Set yours values
    
    REM SET Target
    SET /p hostInput=" - Target (ip or hostname): "
    If "%hostInput%"=="" ECHO.&GOTO start
    
    REM SET loops
    SET /p loopsInput=" - Requests number: "
    SET /a loops=loopsInput
    
    REM SET time limit
    SET /p maxmsInput=" - Maximum Time Limit (ms): "
    SET /a maxms=maxmsInput
    
    REM Value used for sleep between loops
    SET /p sleepInput=" - Delay between requests (s): "
    SET /a sleepDelay=sleepInput+1
    
    REM Variables
    SET displayText=""
    SET /a countRequestsOk=0
    SET /a countRequestsKo=0
    SET /a totalRequests=0
    SET /a maxTime=0
    
    ECHO.
    ECHO START at %TIME% [target: %hostInput%, requests: %loops%, time limit: %maxms% ms, delay: %sleepInput% s]
    ECHO.
    
    
    REM Loop
    :loop
    
    REM Set time
    FOR /f "tokens=1-3 delims=/:" %%a IN ("%TIME%") DO (SET mytime=%%ah%%bm%%cs)
    
    REM Get ping value
    FOR /f "tokens=3delims==" %%a IN ('PING -n 1 %hostInput%') DO FOR /f "delims=m" %%b IN ("%%a") DO (
    
        SET /a timems=%%b
        SET /a totalRequests+=1
    
        REM Check result
        IF !timems! GTR %maxms% ( GOTO failed ) ELSE (  GOTO success )
    
    )
    
    
    REM Request success
    :success
    SET /a countRequestsOk+=1
    IF !timems! GTR !maxTime! ( SET /a maxTime=timems )
    <nul set /P "=!countRequestsOk! requests [Max !maxTime! ms]!CR!"
    
    GOTO next
    
    
    REM Request failed
    :failed
    IF !countRequestsOk! GTR 0 ECHO.
    
    SET /a countRequestsOk=0
    SET /a countRequestsKo+=1
    
    SET displayText=" %mytime% - !timems!ms"
    CALL :ColorText 0c !displayText!
    
    GOTO next
    
    
    REM Next loop
    :next
    
    REM Sleep a little bit
    IF %sleepDelay% GTR 1 ( ping -n %sleepDelay% localhost > nul )
    
    REM Check continue
    SET /a loops-=1
    IF %loops% gtr 0 GOTO loop
    
    
    REM Display result
    IF !countRequestsOk! GTR 0 ECHO.
    ECHO.
    ECHO STOP at %TIME%
    ECHO.
    
    if !countRequestsKo! GTR 0 (
        SET displayText="FAILED - !countRequestsKo! requests over %maxms% ms on !totalRequests! requests in total"
        CALL :ColorText 0c !displayText!
    ) ELSE (
        SET displayText="SUCCESS - No request over %maxms% ms on !totalRequests! requests in total"
        CALL :ColorText 02 !displayText!
    )
    
    
    REM Ask if restart
    ECHO.&ECHO *********************
    SET /p restartInput="Do it again ? (Y/N): "
    If "%restartInput%"=="" ECHO *********************&GOTO start
    If /I "%restartInput%"=="y" ECHO *********************&GOTO start
    If /I "%restartInput%"=="n" ECHO *********************&GOTO end
    
    
    REM End
    :end
    PAUSE
    GOTO :EOF
    
    
    REM Line color
    :ColorText
    ECHO off
    ECHO %DEL% > "%~2"
    FINDSTR /v /a:%1 /R "^$" "%~2" NUL
    DEL "%~2" > NUL 2>&1