LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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

Reply
 
Search this Thread
Old 01-26-2002, 08:30 AM   #1
Griffon26
Member
 
Registered: Sep 2001
Location: The Netherlands
Distribution: Gentoo, Debian, Mandrake, LFS
Posts: 182

Rep: Reputation: 30
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.
 
Old 01-26-2002, 03:18 PM   #2
ca9mbu
LQ Newbie
 
Registered: Nov 2001
Location: Sunderland, UK
Distribution: Mandrake 8.1
Posts: 21

Rep: Reputation: 15
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.
 
Old 01-26-2002, 04:14 PM   #3
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
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
 
Old 01-26-2002, 08:20 PM   #4
Griffon26
Member
 
Registered: Sep 2001
Location: The Netherlands
Distribution: Gentoo, Debian, Mandrake, LFS
Posts: 182

Original Poster
Rep: Reputation: 30
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).

Last edited by Griffon26; 01-27-2002 at 04:56 AM.
 
Old 01-26-2002, 09:13 PM   #5
Griffon26
Member
 
Registered: Sep 2001
Location: The Netherlands
Distribution: Gentoo, Debian, Mandrake, LFS
Posts: 182

Original Poster
Rep: Reputation: 30
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
 
Old 08-30-2009, 04:16 PM   #6
sudo_it
LQ Newbie
 
Registered: Aug 2009
Posts: 1

Rep: Reputation: 0
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.
 
  


Reply

Tags
awk, ping, timestamp


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regaining output from a background process the_holysmoke Linux - Newbie 4 03-11-2011 06:48 PM
weird sed output schneidz Programming 3 05-19-2005 11:16 AM
insert output of sed into a variable hwouters Linux - General 3 11-06-2004 07:54 PM
Problem in output to a process in perl saneax Linux - Software 2 08-31-2004 06:18 AM
Redirecting process output to shell mhelles Linux - Newbie 1 04-30-2003 09:30 AM


All times are GMT -5. The time now is 02:41 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration