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

Batch Script to Ping and Tracert

I have written a Batch Script to constantly ping a router IP Address and then when it returns ErrorLevel 1 it will run a traceroute to that IP and then once completed it starts over again.

I would like to change this so that it outputs the results of the ping into a temporary text file (temp.txt) then scans the "temp.txt" for the reply "Request TImed Out" if one of the four replies have this string I would like the batch file to display the  contents of "temp.txt" on screen and also write them to a log file (report.txt). It would then perform the trace route which would output the results of the trace to "trace.txt" once the trace route has completed the batch file would loop until another "Request Timed Out" is detected. Obviously the "temp.txt" file would need to be wiped each time the loop is repeated.

I have come up with the following for the ping command:

ping -w 1000 -n 1 192.168.1.9 >>temp.txt

Now I just need help with creating a command to scan "temp.txt" for "Request Timed Out" and if true then to output "temp.txt" to screen and to "Report.txt" then call the trace route routine; if FALSE then output "temp.txt" to screen and continue loop.

Hope this describes my problem well enough for you to be able to help me. Many thanks in advance.
0
Bulldog98
Asked:
Bulldog98
  • 6
  • 4
1 Solution
 
MSE-dwellsCommented:
Try something like this -

[BEGIN FILE]

@echo off

:LOOP
ping -w 1000 -n 1 192.168.1.9 >%TEMP%\temp.txt

type %TEMP%\temp.txt
type %TEMP%\temp.txt | find /i "request timed out" && (
  copy %TEMP%\temp.txt %USERPROFILE%\Desktop\Report.txt
  call :TRACERT
)

goto :LOOP

:TRACERT
tracert 192.168.1.9 >%USERPROFILE%\Trace.txt
goto :EOF

[END FILE]

* to ensure the temp.txt file contains only this iterations output, replace the double >> with a single >
* personally, I'd append the tracert results to the end of the Result.txt file rather than create another log
0
 
Bulldog98Author Commented:
Okay, that helps a little, I really need to append to the report.txt and trace.txt files rather than overwrite though. I am using %yymmdd% variable to write the file name with a datestamp so that I am able to build a history.

Thanks for the fast reply.
0
 
MSE-dwellsCommented:
If you need any finalizing assistance, post your whole script as it is with my additions incorporated and we'll finish it off together ...
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Bulldog98Author Commented:

@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=192.168.1.1

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

:: CB01: 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.                     *
:: **************************************************************

:: CB01: Adds 1 to pingcount variable.
SET /a pingcount=%pingcount%+1

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

type %TEMP%\temp.txt
type %TEMP%\temp.txt | find /i "request timed out" && (
  copy %TEMP%\temp.txt %USERPROFILE%\Desktop\%pinglog%.txt
  call :TRACE
)

:: 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.
 
::  PING %machine% -n 6

:: CB01: Checks if pingcount has reached 1200 and if true skips to
:: CB01: INTTRACE Routine, if false then current routine continues.
IF %pingcount% EQU 1100 GOTO :INTTRACE

:: CB01: 1100 should approximately be 60 minutes.

:: Repeats the ping process.
GOTO PING

: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.                        *
:: **************************************************************

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

:: 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% Checking if communication has been restored.
ECHO.

:: Skips to TIMEOUT Routine to check if communication has been restored.
GOTO TIMEOUT

:INTTRACE

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

:: CB01: Writes message to the pinglog file.
ECHO. >>%pinglog%.txt
  ECHO ********************************************************* >>%pinglog%.txt
  ECHO Executing Interval Trace Route at %TIME%                  >>%pinglog%.txt
  ECHO ********************************************************* >>%pinglog%.txt
ECHO. >>%pinglog%.txt

:: CB01: Writes message to the tracelog file.
ECHO. >>%tracelog%.txt
  ECHO ********************************************************* >>%tracelog%.txt
  ECHO Executing Interval Trace Route to %Machine%               >>%tracelog%.txt
  ECHO ********************************************************* >>%tracelog%.txt

:: CB01: Displays message on the screen.
ECHO.
  ECHO *********************************************************
  ECHO Executing Interval Trace Route to %Machine%
  ECHO *********************************************************
ECHO.

  ECHO Please wait, trace route is processing...
 
:: CB01: Performs Trace Route command on the target machine and writes
:: CB01: results to the tracelog file.
    TRACERT %machine% >>%tracelog%.txt

:: CB01: 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 *********************************************************
  ECHO Interval Trace Route Completed at %TIME%                
  ECHO *********************************************************
ECHO.

SET pingcount=0

:: CB01: Repeats the ping process.
GOTO PING

:TIMEOUT

:: Checks if communication to %Machine% is still down and repeats
:: until communication is re-established.
  PING %Machine% -n 4
      IF %ERRORLEVEL% NEQ 0 GOTO TIMEOUT
     
ECHO. >>%tracelog%.txt
  ECHO ********************************************************** >>%tracelog%.txt
  ECHO Communication restored to %Machine% at %TIME%              >>%tracelog%.txt
  ECHO ********************************************************** >>%tracelog%.txt
ECHO. >>%tracelog%.txt

  GOTO PING    

:EOF

:: Program will loop until CTRL+C is pressed or window is closed.

EXIT /B
0
 
Bulldog98Author Commented:
It's my first attempt at batch file programming so you will have to bare with me on a few things. I am trying to achieve a %tracelog%.txt file which contains the results of the intermittent trace routes completed and trace routes that have been completed due to a ping timeout.

With %pinglog%.txt I am trying to record a log of succesful pings (without the actual results of the ping) and unsuccesful pings (with the actual results of the pings)

This script will then be left running 24x7 generating logs throughout the week and also generating a visual notice of a sucess and failure.

I would also like to add a housekeeping routine that checks for log files older than 7 days and deletes them. But that's another story. :)  
0
 
MSE-dwellsCommented:
Woo ... that's a wee bit larger than I'd expected ;0)  Out of interest, where did you learn to comment using double-colons ... your general structure and formatting reminds me very much of my own script formatting?

It's late for me now ... so I'll pick this up in the morning if nobody else has chipped in.

PS - you didn't mention any additional requirements ... is it functioning as desired?
0
 
Bulldog98Author Commented:
I used to be an Analyst Programmer in RPG400 for IBM AS400 Servers. Been a long time though, just comment out of habbit now and the script will be used for others so good comments help them understand if they need to change things.

It functions okay, just need to append to the log files rather than overwrite them. It will be used to monitor our Internet Router at our Datacentre in the US, so one copy will run from our HQ in England and another copy will run on a management server within the IDC pointing back to HQ. This is so we can monitor timeouts and drops that affect our customers so when the Common Backbone Team for our ISP asks us for trace routes at the time of the dropouts we will be able to provide them.

Thanks for your help so far. Speak to you in the morning.
0
 
Bulldog98Author Commented:
I have managed to figure out how to make everything work now, will begin the next part of my project which is to add command line switches. :)

This is how I acheived the results I needed in the end:

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
 
0
 
Bulldog98Author Commented:
The full script looks like this now:

@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=12.130.14.131

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

:: Adds 1 to pingcount variable.
SET /a pingcount=%pingcount%+1

:: 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.
 
:: Checks if pingcount has reached 1100 and if true skips to
:: INTTRACE Routine, if false then current routine continues.
IF %pingcount% EQU 1100 GOTO :INTTRACE

:: 1100 should be approximately 60 minutes.

:: Repeats the ping process.
GOTO PING

: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% Checking if communication has been restored.
ECHO.

:: Skips to TIMEOUT Routine to check if communication has been restored.
GOTO TIMEOUT

:INTTRACE

:: **************************************************************
:: * INTTRACE 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.                 *
:: **************************************************************

:: Writes message to the pinglog file.
ECHO. >>%pinglog%.txt
  ECHO ********************************************************* >>%pinglog%.txt
  ECHO Executing Interval Trace Route at %TIME%                  >>%pinglog%.txt
  ECHO ********************************************************* >>%pinglog%.txt
ECHO. >>%pinglog%.txt

:: Writes message to the tracelog file.
ECHO. >>%tracelog%.txt
  ECHO ********************************************************* >>%tracelog%.txt
  ECHO Executing Interval Trace Route to %Machine%               >>%tracelog%.txt
  ECHO ********************************************************* >>%tracelog%.txt

:: Displays message on the screen.
ECHO.
  ECHO *********************************************************
  ECHO Executing Interval Trace Route to %Machine%
  ECHO *********************************************************
ECHO.

  ECHO Please wait, trace route is processing...
 
:: Performs Trace Route command on the target machine and writes
:: results to the tracelog file.
    TRACERT %machine% >>%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 *********************************************************
  ECHO Interval Trace Route Completed at %TIME%                
  ECHO *********************************************************
ECHO.

SET pingcount=0

:: Repeats the ping process.
GOTO PING

:TIMEOUT

:: Checks if communication to %Machine% is still down and repeats
:: until communication is re-established.
  PING %Machine% -n 4
      IF %ERRORLEVEL% NEQ 0 GOTO TIMEOUT
     
ECHO. >>%tracelog%.txt
  ECHO ********************************************************** >>%tracelog%.txt
  ECHO Communication restored to %Machine% at %TIME%              >>%tracelog%.txt
  ECHO ********************************************************** >>%tracelog%.txt
ECHO. >>%tracelog%.txt

  GOTO PING    

:EOF

:: Program will loop until CTRL+C is pressed or window is closed.

EXIT /B
0
 
MSE-dwellsCommented:
Excellent, nice job.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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