Linux - SoftwareThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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..
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
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
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.
#!/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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.