Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
| 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-10-2009, 06:53 AM
|
#1
|
|
LQ Newbie
Registered: Nov 2009
Location: Valencia, Spain
Posts: 11
Rep:
|
Ping shell script to report high rtt
Hi,
I'm trying to write a shell script that makes a ping, with three parameters (IP, count and max rtt), to print the echo response that exceeds the max rtt put as parameter, with a time stamp.
i.e. if I make a ping to 192.168.85.125, with a rtt of 100ms, only prints the echo response that exceeds 100ms:
64 bytes from 192.168.85.125: icmp_seq=4 ttl=128 time=238.39 ms 13:48:34
64 bytes from 192.168.85.125: icmp_seq=12 ttl=128 time=102.19 ms 13:48:54
Really I'm not good at shell scripting, and I'm a few days thinking on it, without a good solution.
any ideas?
Thanks
|
|
|
|
11-10-2009, 03:16 PM
|
#2
|
|
Moderator
Registered: May 2001
Posts: 24,779
|
Quote:
Originally Posted by celembor
Really I'm not good at shell scripting, and I'm a few days thinking on it, without a good solution.
any ideas?
|
Post whatever (pseudo)code you got?
|
|
|
|
11-19-2009, 05:03 AM
|
#3
|
|
LQ Newbie
Registered: Nov 2009
Location: Valencia, Spain
Posts: 11
Original Poster
Rep:
|
Well, I was trying to make something like this (pseudo-code):
make a ping with IP and RTT
read IP and RTT
take time value from each line of echo reply
if time is higher than RTT
print line
---------
In concept it's easy, but I don't know how to create it. I'm not good at shell scripting
|
|
|
|
11-21-2009, 10:26 PM
|
#4
|
|
LQ Newbie
Registered: Nov 2008
Location: Washington State
Distribution: Mint
Posts: 28
Rep:
|
try something like this then:
ping google.com -i .1 -c 3 | tail -1
This will give you the final line back from three quick pings.
rtt min/avg/max/mdev = 83.148/84.529/85.310/0.979 ms
Now use awk / sed / cut whatever and grab the data you want.
Then just use a if statment and say something along the lines of this maybe..
if [ $RETURN_VALUE -gt <insert threshold here> ]; then
do something
else
everything is good
fi
If you are still learning the basics, try this to format the data in a way that you can use..
run the return data through sed, and change the / character to a space. Now use the awk '{print $#}' to grab the data that you want to compare to your threshold value. If you go about this in this way your print # should be 7-10 depending on how you look at it. Post back if you need more details..
Last edited by Guyverix; 11-21-2009 at 10:31 PM.
|
|
|
|
11-21-2009, 11:22 PM
|
#5
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
An alternative solution
Code:
#!/bin/bash
echo "$(ping -c3 linuxquestions.org )" | while read
do
case $REPLY in
*'time=2'[0-9][0-9]* )
echo "$REPLY"
esac
done
EDIT:
Oops! That only catches 200-299 ms outputs
This is better
Code:
#!/bin/bash
shopt -s extglob
echo "$(ping -c3 linuxquestions.org )" | while read
do
case $REPLY in
*'time='[2-9][0-9][0-9]* | *'time='+([0-9])[0-9][0-9][0-9]*)
echo "$REPLY"
esac
done
Last edited by catkin; 11-22-2009 at 02:55 AM.
|
|
|
|
11-22-2009, 03:02 AM
|
#6
|
|
LQ Newbie
Registered: Nov 2008
Location: Washington State
Distribution: Mint
Posts: 28
Rep:
|
Grin, thats a good example, but for someone who is learning scripting, how is he going to make sense of that?
|
|
|
|
11-23-2009, 10:38 AM
|
#7
|
|
LQ Newbie
Registered: Nov 2009
Location: Valencia, Spain
Posts: 11
Original Poster
Rep:
|
Thank you for this posts.
You're right Guyverix, I don't undertand that script.
Maybe my point of view to perform this script is not right, but I was thinking in something different
command should be something like this:
ping 192.168.1.25 100
and take 100 as max time for echo response. This should make a ping to that IP, and when one of those has a response higher than 100 ms, then it is printed in screen (or file), like this:
64 bytes from 192.168.1.25: icmp_seq=4 ttl=128 time=238.39 ms
thanks for your time
|
|
|
|
11-24-2009, 12:04 AM
|
#8
|
|
LQ Newbie
Registered: Nov 2008
Location: Washington State
Distribution: Mint
Posts: 28
Rep:
|
So you are thinking more like a continuous ping for an IP and if time is greater than 100 then echo it.
If I were going to code it, I would do something like this then: (this code will not work out of the box, but gives you an idea of the way I am looking at the problem)
# if you do not want this to just run forever, use the -c switch in ping (counts)
#check takes the first arg as an IP address.
#threshold is your second commandline arg.
threshold=$2
check=`ping $1`
while read $check; do
#clean will pull time=###, and sed will strip all A-Z chars, and the =.
clean=`echo $check | awk '{print $7}' | sed 's/[A-Z a-z =]//g'`
if [ $clean -gt $threshold ]; then
echo "Threshold crossed RTT $clean"
fi
done
|
|
|
|
11-24-2009, 02:24 AM
|
#9
|
|
Member
Registered: Sep 2009
Distribution: Slackware
Posts: 39
Rep:
|
i was thinking of something a little simpler
what if you tried to incorporate grep for part of your code...
Quote:
|
ping google.com | grep time=[1-9][0-9][0-9]
|
but it fails at 4 digit time values
|
|
|
|
11-24-2009, 02:38 AM
|
#10
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Quote:
Originally Posted by celembor
Thank you for this posts.
You're right Guyverix, I don't undertand that script.
Maybe my point of view to perform this script is not right, but I was thinking in something different
command should be something like this:
ping 192.168.1.25 100
and take 100 as max time for echo response. This should make a ping to that IP, and when one of those has a response higher than 100 ms, then it is printed in screen (or file), like this:
64 bytes from 192.168.1.25: icmp_seq=4 ttl=128 time=238.39 ms
thanks for your time
|
Change linuxquestions.org to 192.168.1.25 100 and the script I posted does exactly that.
If you want an explanation of how the script works please ask.
|
|
|
|
12-02-2009, 07:12 AM
|
#12
|
|
LQ Newbie
Registered: Nov 2009
Location: Valencia, Spain
Posts: 11
Original Poster
Rep:
|
Hello,
finally I got what I wanted:
Code:
#!/bin/bash
IP=$1 #Destination address
MAX=$2 #Max time in milisecons to record
SEG="5" #number of seconds for each ping
ping="/bin/ping"
###########################################################
# FUNCTIONS
#
function float_cond()
{
local cond=0
if [[ $# -gt 0 ]]; then
cond=$(echo "$*" | bc -q 2>/dev/null)
if [[ -z "$cond" ]]; then cond=0; fi
if [[ "$cond" != 0 && "$cond" != 1 ]]; then cond=0; fi
fi
local stat=$((cond == 0))
return $stat
}
###########################################################
while true
do
RET=`${ping} -c 1 ${IP} | grep 'time=' | awk '{print $7}' | cut -d '=' -f 2`
if float_cond "${RET} > ${MAX}"; then
echo "["`date '+%T'`"]: echo reply from ${IP} time=${RET} ms"
#echo "["`date '+%Y%m%d%k%M%S'`"] TIME: ${RET}"
fi
sleep ${SEG}
done
thanks for your help.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:14 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|