How to use OS X’s terminal to ping an IP using the latency as a stopping condition for a loop

Networkpingterminal

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.

Best Answer

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

#!/bin/bash

TRESHOLD=10

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
    else
        echo "More than $TRESHOLD ($time), stop"
        exit
    fi

done

Outputs:

~ $ ./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
~ $