Page 1 of 1

Batch-Script for ping test with colour change and logging

Posted: 23 Apr 2013 08:57
by durairajkumar
Hi Everyone, I am Network Guy..I am new to this Batch script.....I am in need of script that works as this way>>>>>>>>>>>>>>>>>>>>>>>
Continuous ping to an ip, it should check the latency time in
"Reply from 8.8.8.8: bytes=32 time=49ms TTL=50" when the latency time spikes beyond specific value, lets say if it goes beyond 200ms, the screen should change the color to read, if it is below the threshold the screen should be green.. The Whole output should be directed to a log in a text file.....


could some one help to get me batch script for this above ^^^^

Re: Batch-Script for ping test with colour change and loggi

Posted: 23 Apr 2013 15:34
by abc0502
so you don't want any other thing on screen but the colors red and green ?
and What Exactly what you need to log? The Pings?

Re: Batch-Script for ping test with colour change and loggi

Posted: 23 Apr 2013 17:00
by abc0502
This is supposed to make cmd screen red when it exceeds a certain time latency and make it green as long as it less than the latency set by user.
Also, log the ping in a file set by the user.

But for some reason, comparing the times is not working right ? :?
Here is The code, if any one else can fix it

Code: Select all

@ECHO OFF
TITLE Ping latency

Rem Settings
SET "IP=www.google.com"         %= IP or Address =%
SET "LOG=C:\%IP%.log"           %= Log File Location, name equal %IP%.log =%
SET "TempFile=%temp%\LT.tmp"    %= Temporarily Hold Last Result =%
SET "LTime=3000"                %= IF Exceeded this limit will turn color to RED =%

:Loop

Rem Ping The IP One Time
SETLOCAL EnableDelayedExpansion
For /F "delims=" %%A IN (' Ping "%IP%" -n 1 ') DO (
   SET "Line=%%A"
   IF "!Line:~0,10!" == "Reply from" (
      Rem Log The Result Temporarily
      Echo !Line!>"%TempFile%"
      Rem Log The Result Permanently
      Echo !Line!>>"%LOG%"
   )
)


Rem Read The Last Input Of The File and Set Color
For /F "tokens=1-6 delims= " %%A In (' TYPE "%TempFile%" ') Do (
   SET "OutResult=%%E"
   Rem Below line is just for testing, remove it when every thing work fine
   ECHO !OutResult:~5,-2!
   Rem Allowed Range "green"
   IF "!OutResult:~5,-2!" LEQ "%LTime%" color a0
   Rem Exceeded Allowed range "red"
   IF "!OutResult:~5,-2!" GTR "%LTime%" color c0
)

ENDLOCAL

Goto :LOOP


Re: Batch-Script for ping test with colour change and loggi

Posted: 23 Apr 2013 23:00
by foxidrive
This seems to work here. It also prints the time and ping result on the screen.


Code: Select all

@ECHO OFF
TITLE Ping latency

Rem Settings
SET "IP=www.google.com"         %= IP or Address =%
SET "LOG=%IP%.log"              %= Log File name and Location, name equal %IP%.log =%
SET "LTime=200"                 %= If OutResult Exceeds this time in ms it will turn the screen RED =%

:Loop

Rem Ping The IP One Time
set "var="
For /F "delims=" %%A IN (' Ping "%IP%" -n 1 ^|find "Reply from" ') DO (
      >>"%LOG%" Echo %date% %time% - %%A
      set "var=%%A"
)

Rem if the site does not respond then indicate that
if not defined var (
echo %date% %time% - "%IP%" not responding
>>"%log%" echo %date% %time% - "%IP%" not responding
goto :loop
)

Rem Get the time result
For /F "tokens=5 delims==m" %%A In ("%var%") Do SET "OutResult=%%A"

   Rem Allowed Range "green"
   IF %OutResult% LEQ %LTime% color a0
   Rem Exceeded Allowed range "red"
   IF %OutResult% GTR %LTime% color c0
   echo %date% %time%: IP %var:~11%

Rem delay for 10 seconds
ping -n 10 localhost >nul

Goto :LOOP

Re: Batch-Script for ping test with colour change and loggi

Posted: 24 Apr 2013 00:25
by abc0502
Great Job Foxidrive, it works perfectly :D

Re: Batch-Script for ping test with colour change and loggi

Posted: 24 Apr 2013 02:27
by durairajkumar
Thank you very much guys........It works. :) :) I really appreciate your effort on this.......
I got what i exactly need......

Re: Batch-Script for ping test with colour change and loggi

Posted: 24 Apr 2013 18:15
by Aacini
I like this version! :D

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Settings
set "IP=www.google.com"         // IP or Address
set "LOG=%IP%.log"              // Log File name and Location, name equal %IP%.log
set "LTime=200"                 // If OutResult Exceeds this time in ms it will turn the screen RED

set bar=
for /L %%i in (1,1,66) do set bar=!bar!@
set lastTime=0
set lastColor=0
mode con: lines=3

:Loop

rem Ping the IP, get the time
for /F "delims=" %%a in ('ping "%IP%" -n 1 ^| findstr /C:"Reply from"') do (
   set "line=%%a"
   for /F "tokens=7 delims== " %%b in ("%%a") do set time=%%b
)

rem If the time changed: update log and screen
if %time% neq %lastTime% (
   echo %line%>> "%LOG%"
   set /A barLen=%time:~0,-2% * 66 / LTime
   if !barLen! lss 66 (
      set color=0A
   ) else (
      set color=0C
      set barLen=66
   )
   for %%l in (!barLen!) do set newBar=!bar:~0,%%l!
   title Ping latency: !newBar!
   cls
   echo/
   echo Time: %time%  !newBar!
   if !color! neq %lastColor% (
      color !color!
      set lastColor=!color!
   )
   set lastTime=%time%
)

goto :Loop

Antonio

Re: Batch-Script for ping test with colour change and loggi

Posted: 24 Apr 2013 20:30
by abc0502
I Like it too, nice idea :)
if you Add a Time and date stamp to the Log file like Foxidrive did, and it will be perfect :wink:


Edited:
I changed the code a littlebit and add what i said above

You can choose to log the Reply by setting the variable "LoggingMode" to yes or set it to any thing else (no) to not log any thing.
Also added the time and date foxidrive used when logging the reply.
And it display the latency time instead of the bar in the title bar

Based on Aacini code

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Settings
set "IP=www.google.com"         // IP or Address
set "LOG=%IP%.log"              // Log File name and Location, name equal %IP%.log
set "LTime=200"                 // If OutResult Exceeds this time in ms it will turn the screen RED
set "LoggingMode=no"         // if yes it will log the events, any thing else will not log

set bar=
for /L %%i in (1,1,66) do set bar=!bar!@
set lastTime=0
set lastColor=0
mode con: lines=3

:Loop

rem Ping the IP, get the time
for /F "delims=" %%a in ('ping "%IP%" -n 1 ^| findstr /C:"Reply from"') do (
   set "line=%%a"
   for /F "tokens=7 delims== " %%b in ("%%a") do set LatencyTime=%%b
)

rem If the time changed: update log and screen
if %LatencyTime% neq %lastTime% (
   if /i "%LoggingMode%" == "yes" echo %date% %time% - %line%>> "%LOG%"
   set /A barLen=%LatencyTime:~0,-2% * 66 / LTime
   if !barLen! lss 66 (
      set color=0A
   ) else (
      set color=0C
      set barLen=66
   )
   for %%l in (!barLen!) do set newBar=!bar:~0,%%l!
   title Ping latency: !newBar!
   cls
   echo/
   echo Time: %LatencyTime%  !newBar!
   if !color! neq %lastColor% (
      color !color!
      set lastColor=!color!
   )
   set lastTime=%LatencyTime%
)

goto :Loop


Changes:
Added variable in line 8
Changed the name of the variable "time" to "LatencyTime" to avoid conflict with the %time% variable in line 26
Changed "!newbar!" in line 35 to "!LatencyTime!" to show the numbers instead of the bar as it doesn't display all of it
Modified line 26 to check logging Mode variable to log or not


I'm gonna use it instead of my old script, and might add few things too :wink:

Re: Batch-Script for ping test with colour change and loggi

Posted: 24 Apr 2013 22:17
by foxidrive
Good work, guys. :)

Re: Batch-Script for ping test with colour change and loggi

Posted: 24 Apr 2013 22:28
by abc0502
you can remove this line "if %time% neq %lastTime% (" and the close parentheses before the Goto command.
And you will get the bar changes in real-time.
but it's better to disable the logging or you ill get a very big log file at the end.

Re: Batch-Script for ping test with colour change and loggi

Posted: 25 Apr 2013 00:16
by foxidrive
If you just close the loop after the logging command it will work that way too.

Code: Select all

if %LatencyTime% neq %lastTime% (
   if /i "%LoggingMode%" == "yes" echo %date% %time% - %line%>> "%LOG%"
)

Re: Batch-Script for ping test with colour change and loggi

Posted: 30 Apr 2013 23:24
by abc0502
This is An Update of Aacini code.

The First Section "settings" is the only section you might need to modify to suite you.
> In This you can make it notify you on any connection loosing & can make turn the alarm On or OFF.

Code: Select all

@Echo OFF
SETLOCAL EnableDelayedExpansion

REM =======[ Settings ]========================================================================================
set "IP=www.google.com"         // IP or Address
set "LOG=%IP%.log"              // Log File name and Location, name equal %IP%.log
set "LTime=250"                 // If OutResult Exceeds this time in ms it will turn the screen RED
set "ErrorSensitivity=10"       // How many error/timeout it should wait till it notify the user on connection loosing
set "VoiceAlarm=on"             // Set it to Off or any thing else if you don't want any alarm
set "LoggingMode=no"            // if yes it will log the events, any thing else will not log

REM =======[ DON'T MODIFY ANY THING FROM DOWN HERE ]============================================================
set bar=
for /L %%i in (1,1,66) do set bar=!bar!@
set lastColor=0

SET "Flag="                   // Used for checking to see if it will call the voice function or not, DON'T CHANGE
SET "ErrorCount=0"            // Used for calculating the time out times before reporting it, DON'T CHANGE
mode con: lines=3

Rem On The First Run
IF "!Flag!" == "" (
   CALL :Voice "Connecting"   // Only on the first run
   SET "Flag=On"
)

:Loop
SET "LatencyTime="            // To Reset The Latency Time, without it won't detect loosing the connection, DON'T CHANGE

rem Ping the IP, get the time
for /F "delims=" %%a in ('ping "%IP%" -n 1 ^| findstr /C:"Reply from"') do (
   set "line=%%a"
   for /F "tokens=7 delims== " %%b in ("%%a") do set LatencyTime=%%b
)

Rem Check Connectivity and Alart The user
IF "%latencytime:~-2%" == "ms" (
   IF "!Flag!" == "On" (
      IF /I "%VoiceAlarm%" == "on" (
         CALL :Voice "Connection Established"
      )
      SET "Flag=Off"
   )
   REM Here we Reste the error count to zero
   SET "ErrorCount=0"
) Else (
   IF "!Flag!" == "Off" (
      SET /A ErrorCount = ErrorCount + 1
      Rem This to prevent any fals alarm, like on single time outs.
      IF "!ErrorCount!" GTR "%ErrorSensitivity%" (
         IF /I "%VoiceAlarm%" == "on" (
            CALL :Voice "Connection Lost"
         )
         SET "Flag=On"
      )
   )
)

Rem If the time changed: update log and screen
if /i "%LoggingMode%" == "yes" echo %date% %time% - %line%>> "%LOG%"
set /A barLen=%LatencyTime:~0,-2% * 66 / LTime
if !barLen! lss 66 (
  set color=0A
) else (
  set color=0C
  set barLen=66
)
for %%l in (!barLen!) do set newBar=!bar:~0,%%l!
title Ping latency: !LatencyTime!
CLS
echo/
Rem This is to prevent displaying an empty full bar when Latency Time is empty
IF NOT "%LatencyTime%" == "" (
   echo Time: %LatencyTime%  !newBar!
)
IF !color! neq %lastColor% (
  color !color!
  set lastColor=!color!
)

goto :Loop

Exit /B

:Voice <Words_to_Read>
rem Create the VBScript, if not exist
(   FOR /F "tokens=1*" %%a IN (' findstr "^:Voice: " ^< "%~F0" ') DO Echo.%%b    )>"%TEMP%\Voice.vbs"
Cscript //nologo "%TEMP%\Voice.vbs" "%~1"
GOTO :EOF
:Voice: Dim message, sapi
:Voice: Set sapi=CreateObject("sapi.spvoice")
:Voice: sapi.Speak chr(34) & WScript.Arguments(0) & chr(34)

Rem LEAVE EMPTY LINE BELOW THIS ONE



For more voices for your system, I found this: Link

Edited The Batch
Make Sure to leave Empty line at the end of your batch