Need a script or batch to ping across WAN and log results

murryc
murryc used Ask the Experts™
on
We have a very relaxed policy WAN between two offices that needs to remain clear of heavy traffic.  Every so often I might have an application exceed what it should use and end up slowing everything to a crawl.  Unless there is another free solution that somewhere is aware of, I could use a simple script or batch to ping across the WAN (I am able to ping a computer) and then write the findings to a log if the time=>300ms for more than 5 replies (or something similar that could be tweaked).  I could then copy the solution and have it check for even worse, say time=>1000 and then use blat to email me the log.  The problem now is that I am unaware of any issues until users start complaining about slowness in certain areas.  By then it looks bad on me.  Thanks for the help!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Nagios.
MRTG.

They will do what you want at specified intervals, and log it, and notify you.

They might require a little time to setup, but you should be able to get it to work.

Ss
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
Sure, this is relatively easy, I will build you a script in the next hour or two.

Q
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
waiting for my laptop to complete windows updates, I already have the script partially formed in my hrad, itmay be possible to make it a single-line command given enough time, but I will build for batch file instantiation instead.....if...windows 7......will...ever....finish....configuring........updates.......!

Q
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer
Commented:
Here we are, please see the attached batch file.

I always believed that the timeout could never be set to less than 500 (1/2 second) but it appears 1000 (1 second) may be the true lower limit.  I can't find a poor enough network connection to test.

Since you really are looking for greater than 1000 MS (1 Second) according to your post, I figure that doesn't really matter for you.

I can obviously go through more hoops in order to do actual comparisons of the values of the time of the ping in Milliseconds, but if you just want an alert after 5 of any value greater than 1000 MS this should work correctly.
:: Script Name=PingGreaterThan1000Ms.bat
:Begin-Script
	@ECHO OFF
	
	:Start-Variables
		SET "TestIP=4.2.2.2"
		SET "MaxTime=1000"
		SET "Retry=5"
		SET "BlatCmd=C:\Admin\Blat.exe"
		SET "MailServer=your smtp server's FQDN or IP address"
		SET "MailTo=YourName@yourDomain.com"
		SET "Sender=YourName@yourDomain.com"
		SET "Subject=Your Subject"
		SET "Body=Your Text for the main part of the email goes here"
	:End-Variables

	SET "COUNT=0"
	:Start-Ping
		SET /A "COUNT=%COUNT%+1"
		IF %COUNT% GTR %Retry% CALL :Start-Blat
		PING %TestIP% -W %MaxTime% | Find /I "Request time Out"&&GOTO Start-Ping
		SET "COUNT=0"
		GOTO Start-Ping
	:End-Ping

	:Start-Blat
		::-  Blat command with variables as defined above
		"%BlatCmd%" - -body "%Body%" -subject "%Subject%" -to %MailTo% -Mailfrom %Sender% -Server %MailServer%
		SET "COUNT=0"
		GOTO :EOF
	:End-Blat

:End-Script
ECHO Some sort of Error must have caused the script to Exit at this point ON %DATE% AT %TIME% !
GOTO :EOF

Open in new window

Author

Commented:
Great solution!  I am testing it tomorrow when at the office.  I also would like for it to record the ping results to a log.  I actually will be trying to have more than one batch file.  The first file would detect smaller delays and just write them consecutive to a local log.  I could then have that log emailed to me once a day or even once a week to review a pattern of traffic at a certain time of the day or certain day of the week.  This is almost like a simple poor mans way of graphing WAN traffic for analysis.

The second patch, more specific to the one you have created, would detect a much larger problem during the time it is occuring, then emails me a notice.  I would have these batches executing several times an hour to get a good sample of the WAN during all hours of the day.
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
I can certainly create one which does that as well, it's just a bit more complex, and would be a separate Question.

Let me know if there are any issues with the batch I created here; I could not find a slow enough network to run tests with the batch file myself.

Author

Commented:
Understood.  I tested the batch and it seems to stay in a loop, no matter the latency of the WAN.  I copied a file across the WAN that slowed the PING to over 1000ms for seveal minutes.  Can we have the batch create a log file with each PING so that we can see the results of the test?
Commented:
Added log file

 
:: Script Name=PingGreaterThan1000Ms.bat
:Begin-Script
	@ECHO OFF
	
	:Start-Variables
		SET "TestIP=4.2.2.2"
		SET "LogFile=%~n0.log" & REM Will take the name of the batch file with the ".log" extension
		SET "MaxTime=1000"
		SET "Retry=5"
		SET "BlatCmd=C:\Admin\Blat.exe"
		SET "MailServer=your smtp server's FQDN or IP address"
		SET "MailTo=YourName@yourDomain.com"
		SET "Sender=YourName@yourDomain.com"
		SET "Subject=Your Subject"
		SET "Body=Your Text for the main part of the email goes here"
	:End-Variables

	SET "COUNT=0"
	:Start-Ping
		PING -n 5 127.0.0.1>NUL & REM WILL PAUSE FOR ARROUND 5 SECONDS
		SET /A "COUNT=%COUNT%+1"
		IF %COUNT% GTR %Retry% CALL :Start-Blat
		FOR /F "delims=" %%A IN ('PING -n 1 -w %MaxTime% %TestIP%') DO SET PingLine=%%A 
		ECHO %date% %time% %TestIP% [%COUNT%] %PingLine%
		ECHO %date% %time% %TestIP% [%COUNT%] %PingLine%>>%LogFile%
		ECHO %PingLine% | Find /I "Request time Out">NUL||SET "COUNT=0"
		GOTO Start-Ping
	:End-Ping

	:Start-Blat
		::-  Blat command with variables as defined above
		"%BlatCmd%" - -body "%Body%" -subject "%Subject%" -to %MailTo% -Mailfrom %Sender% -Server %MailServer%
		SET "COUNT=0"
		GOTO :EOF
	:End-Blat

:End-Script
ECHO Some sort of Error must have caused the script to Exit at this point ON %DATE% AT %TIME% !
GOTO :EOF

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Just a note for the contributing Experts:
   set /a count += 1
is sufficient. Further you do not need to use % in set /a around a variable.
It's only a nuance, but would you please refrain from using tabs? Two or four spaces are really enough for indention ...
And of course    ping -n 5 127.0.0.1   does only wait 4 seconds.
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
After testing (with maxtime=1, which will always fail for external targets) I can see some flaws.

The output of ping needs to be filtered for keywords to know for timeouts (line 23), since it consists of several lines.
The call of BLAT needs to be done AFTER the ping test, to skip an unnecessary additional "sleep".
We need to check for "Request timed out".

And that is above code with my adjustments:
:: Script Name=PingGreaterThan1000Ms.bat
:Begin-Script
  @ECHO OFF
  
  :Start-Variables
    SET "TestIP=1.1.1.1"
    SET "LogFile=%~n0.log" & REM Will take the name of the batch file with the ".log" extension
    SET "MaxTime=1000"
    SET "Retry=5"
    SET "BlatCmd=C:\Admin\Blat.exe"
    SET "MailServer=your smtp server's FQDN or IP address"
    SET "MailTo=YourName@yourDomain.com"
    SET "Sender=YourName@yourDomain.com"
    SET "Subject=Your Subject"
    SET "Body=Your Text for the main part of the email goes here"
  :End-Variables

  SET "COUNT=0"
  :Start-Ping
    PING -n 6 127.0.0.1>NUL & REM "sleep" 5 seconds
    SET /A COUNT += 1
    FOR /F "delims=" %%A IN ('PING -n 1 -w %MaxTime% %TestIP% ^| findstr /i /L /c:"Reply from" /c:"Request timed out"') DO set PingLine=%%A 
    ECHO %date% %time% %TestIP% [%COUNT%] %PingLine%
    ECHO %date% %time% %TestIP% [%COUNT%] %PingLine%>>%LogFile%
    ECHO %PingLine% | findstr /i /L /c:"not reachable" /c:"Request timed Out" >nul || SET COUNT=0
    IF %COUNT% GEQ %Retry% CALL :Start-Blat
    GOTO Start-Ping
  :End-Ping

  :Start-Blat
  ::-  Blat command with variables as defined above
    "%BlatCmd%" - -body "%Body%" -subject "%Subject%" -to %MailTo% -Mailfrom %Sender% -Server %MailServer%
    SET COUNT=0
    exit /b
  :End-Blat

:End-Script
ECHO Some sort of Error must have caused the script to Exit at this point ON %DATE% AT %TIME% !
exit /b

Open in new window

Author

Commented:
Just got back from vacation.  Will be finalizing this tomorrow.  Updates to follow.

Author

Commented:
Great work everyone!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial