summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2024-02-11 15:16:25 -0500
committerAndrew Cady <d@jerkface.net>2024-02-11 15:16:25 -0500
commit77d124139c6e8374eec6f6377d9b5aa64adf0874 (patch)
tree50eb40501b716f83d8726dbb2a163c511f94249a
parenteb59877b0b9db7bdf0d0326d09d096ff77b9e282 (diff)
much improvementsHEADmaster
-rwxr-xr-xssh-check68
1 files changed, 46 insertions, 22 deletions
diff --git a/ssh-check b/ssh-check
index 6d23b59..1d52fc5 100755
--- a/ssh-check
+++ b/ssh-check
@@ -17,13 +17,20 @@ quietly()
17 17
18match() 18match()
19{ 19{
20 local search_text="$1" search_pattern 20 local search_text="$1" search_pattern invert=
21 shift 21 shift
22 for search_pattern in "$@" 22 for search_pattern in "$@"
23 do 23 do
24 case "$search_text" in 24 if [[ "$search_pattern" = '!'* ]]
25 $search_pattern) return ;; 25 then
26 esac 26 invert=y
27 fi
28 if (set +e
29 [[ $search_text = ${search_pattern#!} ]]
30 [ $? ${invert:+ !}= 0 ])
31 then
32 return
33 fi
27 done 34 done
28 false 35 false
29} 36}
@@ -32,36 +39,53 @@ main()
32{ 39{
33 local h 40 local h
34 declare -a hosts=() 41 declare -a hosts=()
35 mapfile hosts < "$CFG_FILE" 42 declare -i max_found_host_length=0
36 MIN_HOST_FIELD_WIDTH=10 43 while read h cmd
37 host_field_width=$MIN_HOST_FIELD_WIDTH
38 for h in ${hosts[@]}
39 do 44 do
40 if [ ${#h} -gt $host_field_width ] 45 if [ ${#h} -gt $max_found_host_length ]
41 then 46 then
42 host_field_width=${#h} 47 max_found_host_length=${#h}
43 fi 48 fi
44 done 49 hosts+=("$h $cmd")
50 done < "$CFG_FILE"
45 while true 51 while true
46 do 52 do
47 { 53 {
48 for h in ${hosts[@]} 54 default_hostcmd=uptime
55 for h in "${hosts[@]}"
49 do 56 do
50 [ $# = 0 ] || match "$h" "$@" || continue 57 host=${h%% *}
51 58 hostcmd=${h#* }
59 [ "$host" ] || continue
60 [ $# = 0 ] || match "$host" "$@" || continue
52 ( 61 (
53 #.........."1234567890123456" 62 #.........."1234567890123456"
54 successpat=" Success: %-${host_field_width}s %s\n" 63 successpat=" Success: %-${max_found_host_length}s %s\n"
55 failurepat="*FAILURE: %-${host_field_width}s\n" 64 failurepat="*FAILURE: %-${max_found_host_length}s %s\n"
56 if command_stdout=$(ssh -n $SSH_OPTIONS \ 65 command_stderr=$(mktemp)
57 "$h" -- \ 66 if command_stdout=$(ssh -n $SSH_OPTIONS -- \
58 ${REMOTE_COMMAND:-uptime} \ 67 "$host" \
59 2>/dev/null) 68 "${hostcmd:-$default_hostcmd}" \
69 2>"$command_stderr")
60 then 70 then
61 printf "$successpat" "$h" "${command_stdout# }" 71 if [ "$hostcmd" ]
72 then
73 logtail=${command_stdout: -40 }
74 else
75 logtail=${command_stdout: 1 : 40 }
76 fi
77 printf "$successpat" "$host" "${logtail@Q}"
62 else 78 else
63 printf "$failurepat" "$h" 79 REPLY=
80 while read
81 do
82 REPLY=${REPLY%$'\r'}
83 printf "$failurepat" "$host" "${REPLY@Q}"
84 done <"$command_stderr" >&2
85 logtail=${REPLY: -40 }
86 printf "$failurepat" "$host" "${logtail@Q}"
64 fi 87 fi
88 rm "$command_stderr"
65 ) & 89 ) &
66 done 90 done
67 wait 91 wait