From 77d124139c6e8374eec6f6377d9b5aa64adf0874 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Sun, 11 Feb 2024 15:16:25 -0500 Subject: much improvements --- ssh-check | 68 ++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file 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() match() { - local search_text="$1" search_pattern + local search_text="$1" search_pattern invert= shift for search_pattern in "$@" do - case "$search_text" in - $search_pattern) return ;; - esac + if [[ "$search_pattern" = '!'* ]] + then + invert=y + fi + if (set +e + [[ $search_text = ${search_pattern#!} ]] + [ $? ${invert:+ !}= 0 ]) + then + return + fi done false } @@ -32,36 +39,53 @@ main() { local h declare -a hosts=() - mapfile hosts < "$CFG_FILE" - MIN_HOST_FIELD_WIDTH=10 - host_field_width=$MIN_HOST_FIELD_WIDTH - for h in ${hosts[@]} + declare -i max_found_host_length=0 + while read h cmd do - if [ ${#h} -gt $host_field_width ] + if [ ${#h} -gt $max_found_host_length ] then - host_field_width=${#h} + max_found_host_length=${#h} fi - done + hosts+=("$h $cmd") + done < "$CFG_FILE" while true do { - for h in ${hosts[@]} + default_hostcmd=uptime + for h in "${hosts[@]}" do - [ $# = 0 ] || match "$h" "$@" || continue - + host=${h%% *} + hostcmd=${h#* } + [ "$host" ] || continue + [ $# = 0 ] || match "$host" "$@" || continue ( #.........."1234567890123456" - successpat=" Success: %-${host_field_width}s %s\n" - failurepat="*FAILURE: %-${host_field_width}s\n" - if command_stdout=$(ssh -n $SSH_OPTIONS \ - "$h" -- \ - ${REMOTE_COMMAND:-uptime} \ - 2>/dev/null) + 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 - printf "$successpat" "$h" "${command_stdout# }" + if [ "$hostcmd" ] + then + logtail=${command_stdout: -40 } + else + logtail=${command_stdout: 1 : 40 } + fi + printf "$successpat" "$host" "${logtail@Q}" else - printf "$failurepat" "$h" + 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 -- cgit v1.2.3