summaryrefslogtreecommitdiff
path: root/ssh-check
blob: b6bfbd32f88bf6b04b30a8b3605a4c93dc7b25b7 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash

CFG_FILE=$HOME/.config/ssh-check.list
CFG_FILE_MAX=$((2**31-1))

read SSH_OPTIONS <<END
-o ConnectTimeout=3
-o ControlPath=none
-o PasswordAuthentication=no
-o StrictHostKeyChecking=yes
END

quietly()
{
    "$@" </dev/null >/dev/null 2>&1
}

c ()
{
    local h hosts
    read -r -N "$CFG_FILE_MAX" hosts < "$CFG_FILE"
    if [ $# = 0 ]; then
        set -- -4
    fi
    while true; do
        {
            for h in $hosts
            do
                ( if p=$(quietly ssh $SSH_OPTIONS -n "$h" -- true)
                then
                    echo "Succeeded: $h"
                else
                    echo "Failed:    $h"
                fi ) &
            done
            wait
        } | column -t -s'$'
        tty -s && break
        sleep 5
    done
}

edit_config()
{
    set -e
    f=$(tempfile)
    if [ -e "$CFG_FILE" ]
    then
        cp "$CFG_FILE" "$f"
    fi
    touch -r "$f" "$f".timestamp
    $EDITOR "$f"
    [ -e "$f" -a "$f" -nt "$f".timestamp ]
    mv "$f" "$CFG_FILE"
    set +e
    rm "$f".timestamp
}

while [ $# -gt 0 ]
do
    case "$1" in
        --edit) edit_config; exit ;;
        --) shift; break;;
        *) exit 1 ;;
    esac
    shift
done

if [ -t 0 ] && ! [ -e "$CFG_FILE" ]
then
    edit_config
fi

c "$@"