2

I am making a batch script that detects High latency from a pinging reply and to notify me with my connection. My script detects 300ms to 700ms numbers by saving pinging reply to log file.

ping -n 15 192.206.238.4 | find /i "Reply" > nul > %cd%\acttracing.log

Then use findstr to search for 300ms to 700ms content and if thus it will go to :sound where it plays a warning notification.

nul findstr /c:"time=680" acttracing.log && (
  echo "High Latency" was found.
) || (
  echo "High Latency" was NOT found.
)
If %ERRORLEVEL% equ 0 goto :sound

nul findstr /c:"time=685" acttracing.log && (
  echo "High Latency" was found.
) || (
  echo "High Latency" was NOT found.
)
If %ERRORLEVEL% equ 0 goto :sound

nul findstr /c:"time=690" acttracing.log && (
  echo "High Latency" was found.
) || (
  echo "High Latency" was NOT found.
)
If %ERRORLEVEL% equ 0 goto :sound

nul findstr /c:"time=695" acttracing.log && (
  echo "High Latency" was found.
) || (
  echo "High Latency" was NOT found.
)
If %ERRORLEVEL% equ 0 goto :sound

nul findstr /c:"time=700" acttracing.log && (
  echo "High Latency" was found.
) || (
  echo "High Latency" was NOT found.
)
If %ERRORLEVEL% equ 0 goto :sound

Now the problem, it's frequently notifying me even in one high latency number found from the saved log file which obviously not affecting my connection. How to make it go to :sound only if it detects 10 times ERRORLEVEL? is there a way to write my script shorter than writing it from 300 to 700?

thanks in advance....

pp_
  • 3,435
  • 4
  • 19
  • 27
Leojay Alfara
  • 43
  • 1
  • 5

1 Answers1

0

Use a for command to get the output of ping, tokenize/split/explode it, get the response time, and finally compare.

set ip=google.com
for /f "skip=2 delims=" %%a in ('ping %ip% -n 1') do (
set "var=%%a"
goto next
)
:next
for /f "tokens=1-6" %%a in ("%var%") do set var=%%e
set time=%var:~5,-2%
if %time% GTR 300 (
if %time% LSS 700 (
echo high latency
))

It's very straightforward, but quite a bit lengthy (hope that's not a problem?).

Mee
  • 207
  • 5
  • 11
  • thank you very much! this code help my script shorter and more effective! – Leojay Alfara Feb 12 '16 at 04:38
  • but still the alarm notify me even in one single latency found. Is there a way to make a separate script to detect at-least 10 frequent high latency before going to :sound? – Leojay Alfara Feb 12 '16 at 04:56
  • @LeojayAlfara yep! set a counter variable and increment when the latency is high. when its 10, goto sound. – Mee Feb 12 '16 at 14:56
  • I'm setting up a counter variable but still I'm having a trouble. what went wrong with my script..? :repeat set ip=google.com for /f "skip=5 delims=" %%a in ('ping %ip% -n 1') do ( set var=%%a goto next ) :next for /f "tokens=1-6" %%a in ("%var%") do set var=%%e set time=%var:~5,-2% if %time% GTR 300 ( if %time% LSS 700 ( echo %time% > %cd%\HIGHLATENCYTEST.log ) set /A %time%=0 IF "%time%" gtr 300 ( set /A time+1=10 goto :sound ) ELSE ( GOTO repeat ) – Leojay Alfara Feb 13 '16 at 08:53