summaryrefslogtreecommitdiff
path: root/dot/local/bin/pingwatch
blob: c487c2f6d10ea27fad9bd36dba530f51b2aa569f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
loud=${VERBOSE}
default_target=marble.tj5tzswz7isfavggdjsiwxdjswrg6tadlzuf3j3q.ed25519.cryptonomic.net

if [ $# = 1 ]
then
        target=$1
else
        target=$(dig -taaaa +short +answer $default_target || echo 2601:401:8200:2d4c:84f4:6bdc:963c:fde2)
fi

if [ "$VERBOSE" ]
then
        echo "target: $target" >&2
fi


process()
{
        state=
        while true
        do
                if read -t 5 line
                then
                        case "$line" in
                                'PING'* | *'Address unreachable') continue ;;
                        esac
                        if [ "$loud" ]
                        then
                                printf '%s\n' "$line"
                        fi
                        if [ "$state" = on ]
                        then
                                continue
                        else
                                date +'%c Online.'
                                state=on
                        fi
                elif [ $? -gt 128 ] # read timeout
                then
                        if [ "$state" = off ]
                        then
                                continue
                        else
                                date +'%c Offline.'
                                state=off
                        fi
                else # non-timeout read error
                        break
                fi
        done
}

ping -n "$target" | process