• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 805
  • Last Modified:

Ping Script & automatic trace route

Hi,

I have a number of devices that loose connectivity, they are connected through various ways and 3rd party clouds. The connections drop at random times and i do not have the time to monitor them all and initiate a trace route once i discover one down.

I would like to write a ping script that will ping a selection of devices every 10 seconds or so and if the ping fails to initiate a trace route to the device.
I also need the outputs to be logged to a txt file.

Thanks,
0
-P-Henderson
Asked:
-P-Henderson
  • 4
  • 4
1 Solution
 
Ernie BeekCommented:
Looks like someone allready made that:
http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_22882736.html

I hope it helps you as well.
0
 
-P-HendersonAuthor Commented:
I'm new to this kind of thing and to be honest the whole script doesn't make much sense,
0
 
-P-HendersonAuthor Commented:
-do i need to create a pinglog.txt if so, where does it need to be.
-where does the script source its IP's to ping, i have a range of 15 to ping if possible.
0
The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

 
Ernie BeekCommented:
ok.

At the moment I have some leakage problems at home so I'm a bit busy right now :-~

I'll have a look as soon as I am able to.
0
 
-P-HendersonAuthor Commented:
Leakage,,,, not good.
I've go the ping script working with one Destination IP, just need to know how to get it to work with multiple ip's.
0
 
Ernie BeekCommented:
Right, I have dry feet again :)

So back to business. I made some changes to the original script:

 
@ECHO OFF

:VARIABLES

:: **************************************************************
:: * SET VARIABLES                                              *
:: **************************************************************

:: Retrieves a useable date from the system date.
SET yymmdd=%DATE:~8,2%-%DATE:~3,2%-%DATE:~0,2%

:: Change the below variable to desired target name or IP Address.
SET Machine=%1

:: Sets the logfile names and adds date stamp.
SET pinglog=pinglog_%yymmdd%
SET tracelog=tracelog_%yymmdd%

:: Sets the pingcount variable to zero.
SET pingcount=0

:PING

:: **************************************************************
:: * PING ROUTINE: Continuous loop to ping the machine name     *
:: * and write the results of the ping to the pinglog file.     *
:: * If ping fails to communicate with the server the loop will *
:: * exit and move on to the TRACE ROUTINE.                     *
:: **************************************************************

IF EXIST %TEMP%\temp.txt DEL %TEMP%\temp.txt

:: Displays status information on screen.
ECHO.
  ECHO Please wait, pinging %Machine%
ECHO.

:: Performs ping command on the target machine and outputs the
:: results to the pinglog file. If ping fails then will skip
:: to TRACE routine.

ping -w 1000 -n 4 %Machine% >>%TEMP%\temp.txt

for /f "tokens=1,* delims=," %%a in ('type %TEMP%\temp.txt ^| findstr /i "Request timed out"') do GOTO TRACE

  type %TEMP%\temp.txt
  
:: Writes completion message to the pinglog file.
ECHO. >>%pinglog%.txt
  ECHO ========================================================= >>%pinglog%.txt
  ECHO Ping Completed Succesfully at %TIME%                      >>%pinglog%.txt
  ECHO ========================================================= >>%pinglog%.txt
ECHO. >>%pinglog%.txt

:: Displays completion message on the screen.
ECHO.
  ECHO =========================================================
  ECHO Ping Completed Succesfully at %TIME%
  ECHO =========================================================
ECHO.
  
GOTO EOF

:TRACE

:: **************************************************************
:: * TRACE ROUTINE: Will perform a trace route command on the   *
:: * target machine and write the results to the tracelog file. *
:: * Once the trace has completed the routine will exit and go  *
:: * back to executing the PING ROUTINE.                        *
:: **************************************************************

IF EXIST %TEMP%\temp2.txt DEL %TEMP%\temp2.txt

type %TEMP%\temp.txt >>%pinglog%.txt

:: Writes ping failure message to the pinglog file.
ECHO. >>%pinglog%.txt
  ECHO ********************************************************* >>%pinglog%.txt
  ECHO Ping failed at %TIME% executing Trace Route.              >>%pinglog%.txt
  ECHO ********************************************************* >>%pinglog%.txt
ECHO. >>%pinglog%.txt

:: Writes ping failure message to the tracelog file.
ECHO. >>%tracelog%.txt
  ECHO ========================================================= >>%tracelog%.txt
  ECHO %Machine% did not respond at %TIME%                       >>%tracelog%.txt
  ECHO ========================================================= >>%tracelog%.txt

:: Displays ping failure message on the screen.
ECHO.
  ECHO =========================================================
  ECHO %Machine% did not respond at %TIME% 
  ECHO =========================================================
ECHO.

  ECHO Please wait, trace route is processing...

ECHO.
  
:: Performs Trace Route command on the target machine and writes
:: results to the tracelog file.
::   TRACERT %machine% >>%tracelog%.txt
     TRACERT %Machine% >>%TEMP%\temp2.txt
     
     type %TEMP%\temp2.txt >>%tracelog%.txt
     
:: Writes trace route completion message to the tracelog file.
ECHO. >>%tracelog%.txt
  ECHO Trace Route Completed at %TIME% on %DATE%. >>%tracelog%.txt
ECHO. >>%tracelog%.txt  

ECHO.
  ECHO Trace Route Completed at %TIME%
ECHO.

GOTO EOF

:EOF

EXIT /B

Open in new window


And called this one pping.bat

Then I created another little script:

 
@ECHO OFF
:DOPING
for /f %%z in (myhosts.txt) do call pping.bat %%z
GOTO DOPING

Open in new window


And called that runpping.bat

Third I made a txt file:
 myhosts.txt
In which you can put the ip adresses to be pinged.

Put this all together in a directory and run runpping.bat This will continue to run until CTRL-C is pressed.

Have a look to see if this is workable for you.
0
 
-P-HendersonAuthor Commented:
My appologies for the delay, this worked a treat.... Thanks
0
 
Ernie BeekCommented:
Never mind, glad it's working.

And thx for the points :)
0

Featured Post

When ransomware hits your clients, what do you do?

MSPs: Endpoint security isn’t enough to prevent ransomware.
As the impact and severity of crypto ransomware attacks has grown, Webroot fought back, not just by building a next-gen endpoint solution capable of preventing ransomware attacks but also by being a thought leader.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now