View unanswered posts | View active topics It is currently 17 Sep 2014 07:33



Post new topic Reply to topic  [ 12 posts ] 
Batch-Script for ping test with colour change and logging 
Author Message

Joined: 23 Apr 2013 08:50
Posts: 2
Post Batch-Script for ping test with colour change and logging
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 ^^^^


23 Apr 2013 08:57
Profile

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Batch-Script for ping test with colour change and loggi
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?


23 Apr 2013 15:34
Profile

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Batch-Script for ping test with colour change and loggi
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:
@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



23 Apr 2013 17:00
Profile
Online

Joined: 10 Feb 2012 02:20
Posts: 4176
Post Re: Batch-Script for ping test with colour change and loggi
This seems to work here. It also prints the time and ping result on the screen.


Code:
@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


23 Apr 2013 23:00
Profile

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Batch-Script for ping test with colour change and loggi
Great Job Foxidrive, it works perfectly :D


24 Apr 2013 00:25
Profile

Joined: 23 Apr 2013 08:50
Posts: 2
Post Re: Batch-Script for ping test with colour change and loggi
Thank you very much guys........It works. :) :) I really appreciate your effort on this.......
I got what i exactly need......


24 Apr 2013 02:27
Profile
Expert

Joined: 06 Dec 2011 22:15
Posts: 723
Location: México City, México
Post Re: Batch-Script for ping test with colour change and loggi
I like this version! :D
Code:
@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


24 Apr 2013 18:15
Profile

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Batch-Script for ping test with colour change and loggi
Quote:
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:
@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:
Quote:
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:


24 Apr 2013 20:30
Profile
Online

Joined: 10 Feb 2012 02:20
Posts: 4176
Post Re: Batch-Script for ping test with colour change and loggi
Good work, guys. :)


24 Apr 2013 22:17
Profile

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Batch-Script for ping test with colour change and loggi
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.


24 Apr 2013 22:28
Profile
Online

Joined: 10 Feb 2012 02:20
Posts: 4176
Post Re: Batch-Script for ping test with colour change and loggi
If you just close the loop after the logging command it will work that way too.

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


25 Apr 2013 00:16
Profile

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Batch-Script for ping test with colour change and loggi
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:
@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


30 Apr 2013 23:24
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 


Who is online

Users browsing this forum: No registered users and 24 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.