diff options
author | Andrew Cady <d@jerkface.net> | 2021-08-30 13:02:33 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2021-08-30 13:02:33 -0400 |
commit | ea405c08a6f062eb58219a6a1a177feb470dd8ee (patch) | |
tree | c94dbcec48ede66b567544cef5db3a3aabe31bb9 | |
parent | ab218353e3d1533a81cc987860c47956ece5c86f (diff) |
improvements
-rwxr-xr-x | ssh-check | 39 |
1 files changed, 30 insertions, 9 deletions
@@ -1,10 +1,13 @@ | |||
1 | #!/bin/bash | 1 | #!/bin/bash |
2 | 2 | ||
3 | CFG_FILE=$HOME/.config/ssh-check.list | 3 | CFG_FILE=$HOME/.config/ssh-check.list |
4 | CFG_FILE_MAX=$((2**31-1)) | 4 | TIMEOUT=5 # seconds |
5 | 5 | ||
6 | read SSH_OPTIONS <<END | 6 | |
7 | -o ConnectTimeout=3 | 7 | read_all='read -N2147483647' |
8 | |||
9 | $read_all SSH_OPTIONS <<END | ||
10 | -o ConnectTimeout=$TIMEOUT | ||
8 | -o ControlPath=none | 11 | -o ControlPath=none |
9 | -o PasswordAuthentication=no | 12 | -o PasswordAuthentication=no |
10 | -o StrictHostKeyChecking=yes | 13 | -o StrictHostKeyChecking=yes |
@@ -18,19 +21,27 @@ quietly() | |||
18 | c () | 21 | c () |
19 | { | 22 | { |
20 | local h hosts | 23 | local h hosts |
21 | read -r -N "$CFG_FILE_MAX" hosts < "$CFG_FILE" | 24 | $read_all -r hosts < "$CFG_FILE" |
22 | if [ $# = 0 ]; then | 25 | if [ $# = 0 ]; then |
23 | set -- -4 | 26 | set -- -4 |
24 | fi | 27 | fi |
28 | host_field_width=1 | ||
29 | for h in $hosts | ||
30 | do | ||
31 | if [ ${#h} -gt $host_field_width ] | ||
32 | then | ||
33 | host_field_width=${#h} | ||
34 | fi | ||
35 | done | ||
25 | while true; do | 36 | while true; do |
26 | { | 37 | { |
27 | for h in $hosts | 38 | for h in $hosts |
28 | do | 39 | do |
29 | ( if p=$(quietly ssh $SSH_OPTIONS -n "$h" -- true) | 40 | ( if uptime=$(ssh $SSH_OPTIONS -n "$h" -- uptime 2>/dev/null) |
30 | then | 41 | then |
31 | echo "Succeeded: $h" | 42 | printf "%-15s %-${host_field_width}s %s\n" "Succeeded:" "$h" "${uptime# }" |
32 | else | 43 | else |
33 | echo "Failed: $h" | 44 | printf "%-15s %-${host_field_width}s\n" "Failed:" "$h" |
34 | fi ) & | 45 | fi ) & |
35 | done | 46 | done |
36 | wait | 47 | wait |
@@ -56,17 +67,27 @@ edit_config() | |||
56 | rm "$f".timestamp | 67 | rm "$f".timestamp |
57 | } | 68 | } |
58 | 69 | ||
70 | unset run_anyway do_edit | ||
59 | while [ $# -gt 0 ] | 71 | while [ $# -gt 0 ] |
60 | do | 72 | do |
61 | case "$1" in | 73 | case "$1" in |
62 | --edit) edit_config; exit ;; | 74 | --edit) do_edit=y;; |
75 | --run) run_anyway=y;; | ||
63 | --) shift; break;; | 76 | --) shift; break;; |
64 | *) exit 1 ;; | 77 | *) exit 1 ;; |
65 | esac | 78 | esac |
66 | shift | 79 | shift |
67 | done | 80 | done |
68 | 81 | ||
69 | if [ -t 0 ] && ! [ -e "$CFG_FILE" ] | 82 | if [ "$do_edit" ] |
83 | then | ||
84 | edit_config | ||
85 | r=$? | ||
86 | if ! [ "$run_anyway}" ] | ||
87 | then | ||
88 | exit $r | ||
89 | fi | ||
90 | elif [ ! -e "$CFG_FILE" -a -t 0 ] | ||
70 | then | 91 | then |
71 | edit_config | 92 | edit_config |
72 | fi | 93 | fi |