LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2009, 10:57 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 542

Rep: Reputation: 32
Rolling display of ping


Hello,

I need to display the most recent 5 pings each second continuously, as in the following example:
1st second Ping 1 - 5
2nd second Ping 2 - 6
3rd second Ping 3 - 7
.... and so on

I've written the following script to:
1) ping continuously to a specific address
2) format the output

I believe I need to make use of the Hold Buffer functionality of "sed" to buffer the latest 5 ping samples (FIFO - push the oldest sample out when a new sample is pushed in), and the print out the buffer. However, I'm not sure how to do this. Any ideas? Thanks.

#!/bin/sh
ping -s 172.16.114.210 | sed '
/icmp_seq=0/ {
s/ from /,/
s/: icmp.*time=/,/
s/PING.*data bytes//
}
/icmp_seq=0/ !{
s/ from /,/
s/: icmp.*time=/,/
s/PING.*data bytes//
}'
 
Old 08-14-2009, 09:21 AM   #2
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
How's this?

Code:
#! /bin/bash
PINGS="1 2 3 4 5"

echo "$PINGS"
while [ 1 ]
do
  PING=$(ping -c 1 localhost | grep "icmp_seq" | sed "s/.*time=//;" | cut -f1 -d" " | tr "\n" " ")
  PING="${PING%%\ }"
  PINGS="${PINGS#* } $PING"
  echo "$PINGS"
done

Last edited by JulianTosh; 08-14-2009 at 09:57 AM. Reason: was a little premature
 
Old 08-15-2009, 01:57 AM   #3
grob115
Member
 
Registered: Oct 2005
Posts: 542

Original Poster
Rep: Reputation: 32
Hello,

Thanks. Just tried it but it gave the following, and kept adding a new line until I press Ctrl C.
./ping_CASE_STBY.sh
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5

I think "ping -c 1 localhost" is just to ping local host once. But this command doesn't quite work on my UNIX because no matter what number I specified, it only ping once. In addition, it only states it's alive without the latency info.
bash-3.00$ ping -c 1 localhost
localhost is alive
bash-3.00$ ping -c 2 localhost
localhost is alive
bash-3.00$


Not quite sure what the rest of the lines are for. Can you explain the following:
PING="${PING%%\ }"
PINGS="${PINGS#* } $PING"
 
Old 08-15-2009, 02:16 AM   #4
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
Code:
PING="${PING%%\ }"
This removes an extra space from the ping time. i.e. changes "0.043 " to "0.043"

Code:
PINGS="${PINGS#* } $PING"
This removes the first column and appends the last ping time to the end. You can read about string searching and expansion here:
http://www.tldp.org/LDP/abs/html/str...ipulation.html
 
Old 08-15-2009, 02:19 AM   #5
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
BTW, this is the output from my ping command:
Code:
$ ping -c 1 localhost
PING localhost.localdomain (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=0 ttl=64 time=0.128 ms

--- localhost.localdomain ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.128/0.128/0.128/0.000 ms, pipe 2
The grep just isolates the ping response and the rest of the sed stuff isolates the actual ping time, which is then stored in PING.
 
  


Reply


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange Ping Issue - Can't ping localhost but can ping others on LAN code_slinger Linux - Networking 15 03-30-2015 02:39 PM
rolling your own saimike Linux - Distributions 2 08-28-2007 11:56 AM
cursor keeps rolling happy78 Linux - General 3 11-15-2005 12:24 PM
Need to get necessary services rolling digity Linux - Newbie 1 04-14-2005 07:44 AM
CVS: rolling back? mikeshn Linux - Software 2 10-21-2003 01:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:24 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration