-1

I'm having trouble with my internet service provider.

Despite having set up a smoke ping server that clearly shows that there are intermittent spikes of latency with packet loss, they have asked for traceroutes to each of the smokeping servers targets; one set when there is no latency/loss and another when there is.

I understand why they are asking, they want to see if there is some kind of routing issue.

I've created a script that traceroutes all targets at once and outputs to a file, but I need to develop a script that calls the traceroute script when latency exceeds a certain value.

To this end, I have made the following script, but it's failing as cut includes not just a number but also "ms". How can I further filter its output to include just the number?

#/bin/bash

while :
    if [ ping | cut -f5 d" " -gl 400 ]
    then
        wait 15
  else
    ./path/script
    fi
done
John1024
  • 109,961
  • 14
  • 137
  • 171
jerk
  • 1
  • 1
  • 1
    Check the editor when you post questions, you can format your code so it stands out and keeps carriage returns. – Nic3500 May 30 '18 at 01:07

2 Answers2

0

Using sed to get just the integer part and limiting the ping count to 1

#/bin/bash

while :
    ttl=$(ping -n -c1 www.google.com | sed -nre 's/^.* time=([0-9]+)\.[0-9]+ ms$/\1/p')
    if [ "$ttl" -gl 400 ]
    then
        wait 15
else
    ./path/script
    fi
done
LMC
  • 10,453
  • 2
  • 27
  • 52
0

You really just need something like this:

ping_res="64 bytes from ya-in-f104.1e100.net (173.194.219.104): icmp_seq=7 ttl=36 time=14.5 ms"
echo $ping_res | awk '{print $7}' | cut -d'=' -f2
36

BUT I would use CURL instead of ping and traceroute which can be blocked etc..( I have example of how to get the ms below as well)

#!/usr/bin/env bash

    # -- BASH4+ example
    # -- if latency is exceeds max_lat, do something . 

    readonly max_lat="3.1415" # made up number
    readonly url="http://www.google.com/"
    readonly curl_timeout="60"
    readonly delay=120 # seconds

    while [ 1 ] ; do

        val=$(curl -o /dev/null -L -s -w "%{time_total}\n" --max-time ${curl_timeout} "${url}" )

        if [ 1 -eq "$(echo "${val} <= ${max_lat}" | bc)" ] ; then
            echo "${val} <= ${max_lat}, doing something ... "
            ./path/script
        else
            echo "just going to wait $delay seconds and test again .."
            sleep $delay
        fi

    done

More awk/cut examples:

echo $ping_res
64 bytes from ya-in-f104.1e100.net (173.194.219.104): icmp_seq=7 ttl=36 time=14.5 ms
$ echo $ping_res | awk '{print $5}'
(173.194.219.104):
$ echo $ping_res | awk '{print $5}' | cut -d'(' -f2
173.194.219.104):
$ echo $ping_res | awk '{print $5}' | cut -d'(' -f2 | cut -d')' -f1
173.194.219.104

OR

$ echo $ping_res | awk '{print $8}' | cut -d'=' -f2
 14.5

See:

How do I measure request and response times at once using cURL?

PS> I may have it reversed (-gl doesn't exist) from your example but you get the point.

Mike Q
  • 6,716
  • 5
  • 55
  • 62