#!/bin/sh
check() {
    ok=1
    firstfail=
    lastfail=
    while read data
    do
        case "$data" in
            *"no answer yet"*)
                #fail
                if [ "$ok" = 1 ]; then
                    ok=0
                    firstfail=$(time "$data")
                    lastfail=
                    echo -n "Fail: $firstfail"
                fi
                #echo fail
                lastfail=$(time "$data")
                ;;
            *"bytes from"*)
                #ok
                if [ "$ok" = 0 ]; then
                    echo " to $lastfail"
                    firstfail=
                    ok=1
                fi
                ;;
            PING*)
                echo "ping fail test start"
                echo "$data"
                ;;
        esac
        #echo $(time "$data")
    done
}

time() {
    echo "$1"\
    | cut -b2-11\
    | xargs -I '%TSTAMP%' date -u --date="1970-01-01 %TSTAMP% sec GMT" "+%H:%M:%S"
}

#cat failtestdata | check
ping -nOD $1 2>&1 | check
