Ping with date and time stamp logging

Summary

A script to ping a host and write the results to a log with date and time stamps prefixed.

Issue

Often when troubleshooting network related issues the first command everyone is asked to run is a variation of ping. A typical ping trace looks as below:

PingwDateTime_ping_11072016

While this is an extremely useful tool it has its limitations in that it is a snapshot for a period of time so issues that are intermittent tend to be masked in a “time block”.

Resolution:

A useful addition to ping would be a date and time stamp, this would allow sysadmins to collate issues based on date and time to aid in troubleshooting and resolving issues quicker.

Implementation

The following code can be pasted in notepad and then saved as a .cmd file

@echo off

REM # Script to ping an address with date & time stamp
REM # Copyright (C) 2015  Adil Dean
REM # http://www.configmonkey.co.uk
REM # Script Name: PingwDateTime.bat
REM # Script Version: 1.0
REM #
REM # This program is free software: you can redistribute it and/or modify
REM # it under the terms of the GNU General Public License as published by
REM # the Free Software Foundation, either version 3 of the License, or
REM #(at your option) any later version.
REM #
REM # This program is distributed in the hope that it will be useful,
REM # but WITHOUT ANY WARRANTY; without even the implied warranty of
REM # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
REM # GNU General Public License for more details.
REM #
REM # You should have received a copy of the GNU General Public License
REM # along with this program.  If not, see .

REM # Batch Script ############################################################
REM #
REM # AUTHOR: Adil Dean
REM # 
REM # UPDATES:
REM # 11/07/2016 - Adil Dean
REM #	- Clean-up script for publishing
REM #
REM ###########################################################################
 
REM .SYNOPSIS
REM		Script to ping an address with date & time stamp
REM .Description
REM		Script is used to ping a network address and log results
REM		with Date and time to a plaintext file.
REM	.Requirements
REM		n/a
REM .EXAMPLE
REM 	.\PingwDateTime.bat
REM .Notes
REM 
REM ==================================================
REM Declare variables
REM ==================================================
set /p strHostAddress= Please enter a Host Address:
set strLogFile=%strHostAddress%_PingTest.log

echo ********************************************************
echo Script with ping the following address: %strHostAddress%
echo Log file will be saved in the current directory under the
echo name: %strLogFile%
echo Press Ctrl+C to stop script at any time
echo ********************************************************
echo Running ping...
ping -t %strHostAddress%|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!date! !time! !data!)&ping -n 2 %strHostAddress%>nul" > %strLogFile%

You can then use your favourite log parsing tool such as cmtrace to get a live view of the log.

Output should look similar to the below:

PingwDateTime

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s