dev.kanngard.net make sure you visit my new blog at: johankanngard.net

Ping with time and date from bash

by Johan Känngård / [Misc] / 2002-11-01 / #14


Here is a little .sh-script to use in bash to ping a server and showing the date and time. (This "hack" probably only works in Windows.) Great if you want to get a log of the pings that does not reach the destination. I am a beginner in bash, so please let me know if there is another way of accomplishing this... Save the code in a file named pingu.sh and link it to a bin directory. Look at Cygwin if you want to use bash in Windows.

echo
if [ $# -ne 2 ]
then
        echo 'Missing arguments. Usage:'
        echo '    pingi ADDRESS TIMEOUT'
        echo
        echo ' i.e:'
        echo '    pingi wwww.w3.org 10'
        exit 1
fi

echo 'Pinging' $1 'with' $2 'seconds as timeout...'
date +%Y-%m-%d

while sleep $2
do
	echo
	date +%H:%M:%S > out.txt
	echo ' ' >> out.txt
	ping $1 -n 1 | grep -r 'Reply\|Request' >> out.txt
	tr -d '\n' < out.txt
done