summaryrefslogtreecommitdiff
path: root/ssh-check
blob: 1d52fc50c95e2066e9141d36ea7475942c3b797f (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash

CFG_FILE=$HOME/.config/ssh-check.list
TIMEOUT=5 # seconds

read -d '' SSH_OPTIONS <<END
-o ConnectTimeout=$TIMEOUT
-o ControlPath=none
-o PasswordAuthentication=no
-o StrictHostKeyChecking=yes
END

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

match()
{
    local search_text="$1" search_pattern invert=
    shift
    for search_pattern in "$@"
    do
        if [[ "$search_pattern" = '!'* ]]
        then
            invert=y
        fi
        if  (set +e
            [[ $search_text = ${search_pattern#!} ]]
            [ $? ${invert:+ !}= 0 ])
        then
            return
        fi
    done
    false
}

main()
{
    local h
    declare -a hosts=()
    declare -i max_found_host_length=0
    while read h cmd
    do
        if [ ${#h} -gt $max_found_host_length ]
        then
            max_found_host_length=${#h}
        fi
        hosts+=("$h $cmd")
    done < "$CFG_FILE"
    while true
    do
        {
            default_hostcmd=uptime
            for h in "${hosts[@]}"
            do
                host=${h%% *}
                hostcmd=${h#* }
                [ "$host" ] || continue
                [ $# = 0 ] || match "$host" "$@" || continue
                (
                    #.........."1234567890123456"
                    successpat=" Success:       %-${max_found_host_length}s  %s\n"
                    failurepat="*FAILURE:       %-${max_found_host_length}s  %s\n"
                    command_stderr=$(mktemp)
                    if command_stdout=$(ssh -n $SSH_OPTIONS -- \
                            "$host" \
                            "${hostcmd:-$default_hostcmd}" \
                            2>"$command_stderr")
                    then
                        if [ "$hostcmd" ]
                        then
                            logtail=${command_stdout: -40 }
                        else
                            logtail=${command_stdout: 1 : 40 }
                        fi
                        printf "$successpat" "$host" "${logtail@Q}"
                    else
                        REPLY=
                        while read
                        do
                                REPLY=${REPLY%$'\r'}
                                printf "$failurepat" "$host" "${REPLY@Q}"
                        done <"$command_stderr" >&2
                        logtail=${REPLY: -40 }
                        printf "$failurepat" "$host" "${logtail@Q}"
                    fi
                    rm "$command_stderr"
                ) &
            done
            wait
        } | column -t -s'$'
        tty -s && break
        sleep 5
    done
}

edit_config()
{
    set -e
    f=$(mktemp)
    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
}

usage()
{
    cat <<EOF
Usage: $0 [--help | -h] [--verbose | -v] [--edit] [--run] [--] [host pattern] [...]
EOF
}

unset run_anyway do_edit
while [ $# -gt 0 ]
do
    case "$1" in
        --edit) do_edit=y;;
        --run) run_anyway=y;;
        --help | -h) usage; exit;;
        --command) REMOTE_COMMAND=$2; shift 2;;
        --verbose | -v) VERBOSE=y; shift;;
        --) shift; break;;
        -*) usage >&2; exit 1 ;;
        *) break;;
    esac
    shift
done

if [ "$do_edit" ]
then
    edit_config
    r=$?
    if ! [ "${run_anyway}" ]
    then
        exit $r
    fi
elif [ ! -e "$CFG_FILE" -a -t 0 ]
then
    edit_config
fi

main "$@"