Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I'd like to keep a log of my connection by sending ping's output to a log file after having prepended each line with a time stamp.
The only problem is that unlike for example cat, I can't get sed to output stuff before ping exits (which is not an option because I want to have ping running indefinitely).
How can I make sed or any other tool output whatever it's getting from ping when it gets it?
Something like this:
ping somehost | cat -n
but then prepending a timestamp instead of a line number.
You want to run ping indefinately? As in you will be continuosly pinging remote machines? I don't think the sys. admins of those boxes will appreciate that at all! Anyway, as for your actual problem - I think the following should work (although it's not been tested - no Linux available here yet):
date > pinglog
ping [machine] [options] 2>&1 >> pinglog
I think this should send both standard output and standard error of ping to a file called pinglog - with a timestamp preceding that output.
Originally posted by ca9mbu You want to run ping indefinately? As in you will be continuosly pinging remote machines? I don't think the sys. admins of those boxes will appreciate that at all!
The reason why I want to do this is because I want to gather some statistics on my connection. It's not at all stable lately. Sometimes there just isn't a connection, and sometimes it's very laggy (>1000ms). The results will enable me to report to the helpdesk when the problems are occurring and how long they last. Also I was not going to do more than one ping every 2 mins and the server to be pinged is the first hop after my own firewall.
Quote:
date > pinglog
ping [machine] [options] 2>&1 >> pinglog
I think this should send both standard output and standard error of ping to a file called pinglog - with a timestamp preceding that output.
This will just output the date when the process was started. In order to see when the interruptions occurred I need a timestamp in front of every line (or at least once every few lines).
Originally posted by crabboy How about a script like this:
Here is another version if you want each query to the host a different line.
If the host is not found the output is not so good.
Thanx. I modified it a little and this is what it is now:
Code:
#!/bin/sh
PING_HOST="somehost"
LOG_FILE="/var/log/ping.log"
while [ 1 ]; do
DATE=`date +'%x %X'`
PING_OUT=`ping -c 1 $PING_HOST`
RESULT=not_ok
echo $PING_OUT | grep 'bytes from' > /dev/null && RESULT=ok
if [ $RESULT == ok ]
then
PING_TIMES=`echo $PING_OUT | cut -d'/' -f5`
echo "$DATE - $PING_TIMES" >> $LOG_FILE
sleep 120
else
echo "$DATE - Timed out" >> $LOG_FILE
sleep 60
fi
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.