LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to process ping output with sed? (http://www.linuxquestions.org/questions/linux-general-1/how-to-process-ping-output-with-sed-12741/)

Griffon26 01-26-2002 08:30 AM

How to process ping output with sed?
 
What I want is the following:

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.

ca9mbu 01-26-2002 03:18 PM

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.

crabboy 01-26-2002 04:14 PM

How about a script like this:
Code:

#!/bin/sh

while [ 1 ]; do
  date >> logfile
  ping [machine] > logfile
  sleep 5
done

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

#!/bin/sh

PING_HOST="dreadnought"
LOG_FILE="ping.log"

if [ -f $LOG_FILE ]; then
  mv $LOG_FILE $LOG_FILE.bak
fi

touch $LOG_FILE

while [ 1 ]; do

  DATE=`date +'%x %X'`
  PING_OUT=`ping -c1 $PING_HOST`

  PING_TIMES=`echo $PING_OUT | cut -d'=' -f5`

  echo "$DATE $PING_HOST $PING_TIMES" >> ping.log

  sleep 20

done


Griffon26 01-26-2002 08:20 PM

Quote:

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).

Griffon26 01-26-2002 09:13 PM

Quote:

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


sudo_it 08-30-2009 04:16 PM

doing it the awk way
 
I realize this is an old thread, but for those of you who made it here via Search Engine like I did - here is the one-line answer:

Code:

ping -t hostname | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }'
or in a script

Code:

#!/bin/bash

ping -t $1 | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }'

Very useful for prepending time-stamp to just about anything.


All times are GMT -5. The time now is 03:56 AM.