What I'd like to do is to ping an IP while the latency is above an specific value. I think an example will help:

Let's suppose I have the following result to the "ping *IP here*" command:

PING *IP here* (*IP here*): 56 data bytes
64 bytes from *IP here*: icmp_seq=0 ttl=53 time=127.238 ms
64 bytes from *IP here*: icmp_seq=1 ttl=53 time=312.762 ms
64 bytes from *IP here*: icmp_seq=2 ttl=53 time=251.475 ms
64 bytes from *IP here*: icmp_seq=3 ttl=53 time=21.174 ms
64 bytes from *IP here*: icmp_seq=4 ttl=53 time=27.953 ms

I'd like a way to make the ping stop after the latency drops below a given value. Let's say 100, so in the example above it'd stop after the 4th result.


I guess there might be easier way, but it works for me:



while $(true); do 

    time=$(ping -c 1 google.com | grep time | cut -d ' ' -f 7 | cut -d '=' -f 2 | cut -d '.' -f 1) 

    if [ $time -lt $TRESHOLD ]; then
        echo "Less than $TRESHOLD ($time), continue"
        sleep 1
        echo "More than $TRESHOLD ($time), stop"



~ $ ./1.sh
Less than 10 (8), continue
Less than 10 (9), continue
Less than 10 (9), continue
Less than 10 (9), continue
Less than 10 (9), continue
More than 10 (11), stop
~ $
  • I used to cut on '.' to get whole part of float since bash doesn't do floats, only integers :/ - you would need to use sed/awk or bc to evaluate that – Marek Bettman Oct 22 '14 at 2:14
  • Ah, just realized, you want to script stop AFTER the time is less than TRESHOLD, just change '-lt $TRESHOLD' to '-gt TRESHOLD' and change "Less" <-> "More" appropriately. – Marek Bettman Oct 22 '14 at 2:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .