On 2023/03/27 12:39, Greg Wooledge wrote:
You aren't showing the actual commands that the script is running, so
we have no way to verify that whatever the script is doing is identical
to what you were doing interactively.
Also:
readarray output< <(ssh -n -T "$user@$host" "$@" 2>&1)
That "$@" is not going to work the way you want it to in the general
case.
----
Well, I'm first trying to get the specific case to work. Not doing
anything other than running scripts on the remote machine, like
"bin/sfc_check".
Script follows(tnx):
#!/bin/bash -u
# gvim=:SetNumberAndWidth
cd ~
# run a command 'on_host' as user
set -o pipefail
shopt -s expand_aliases
PS4='>${BASH_SOURCE:+${BASH_SOURCE/$HOME/\~}}#${LINENO}${FUNCNAME:+(${FUNCNAME})}>
'
alias my='declare ' int='my -i ' array='my -a '
host_resolvable() {
if dig $host|grep -P 'IN\sA\s\d' >& /dev/null; then
return 0
fi
return 1
}
host_up () {
if (($# < 1)); then
echo "Internal usage error: host_up needs hostname" >&2
exit 1
fi
my host="$1"
int stat=0
int dflt_chks=3
int num_chks=dflt_chks
if (($#>1)); then
num_chks=$2
# if num checks not reasonable, silently set to default value
if ((num_chks<1)); then num_chks=dflt_chks; fi
fi
while ((num_chks-- >=1)); do
if ping -c 1 -w $((1+dflt_chks-num_chks)) $host >& /dev/null; then
rm -f "$sf"
return 0
fi
done
if [[ ! -f $sf ]]; then
echo $(date +%s) > "$sf"
echo "ping $host failed. Is it up?"
return 1
fi
}
filter_ssh() {
ign0='ssh: connect to host \w+ port 22: Connection refused'
ign1='(agent returned different signature type ssh-rsa)'
ign2='(ssh_exchange_identification: read: Connection reset by peer)'
ign3='(packet_write_wait: Connection to 192.168.3.12 port 22: Broken
pipe)'
#ign="$ign1|$ign2|$ign3"
ign="$ign1"
# ssh -n -T "$user@$host" "$@" |& readarray output |&
# grep -Pv "$ign" </dev/null | cat
readarray output< <(ssh -n -T "$user@$host" "$@" 2>&1)
echo "Read ${#output[@]} lines"
for o in "${output[@]}"; do
if [[ $o =~ $ign ]]; then continue; fi
printf "%s" "$o"
done
}
check_bins() {
for i in "${needed_bins[@]}"; do
alias $i="$(type -P "$i")"
if ((${#BASH_ALIASES[$i]}==0)); then
printf >&2 "%d: error: $i not found\n" $LINENO
exit 1
fi
done
}
array needed_bins=(dig grep ping rm ssh date)
check_bins
export HOME=/Users/law.Bliss
my user='Bliss\law'
my host=""
if [[ $0 =~ on_host ]]; then
host=$1
echo >&2 "Using host $host"
shift
elif [[ $0 =~ on_(a-zA-Z0-9) ]]; then
host=${BASH_REMATCH[1]}
fi
my sf=~law/.on_$host
host_resolvable || exit 1
if ! host_up $host; then
printf "%d: host $host, not up\n" $LINENO
echo "host $host, not up"
exit 1
else
printf "%d: host $host, up\n" $LINENO
fi
my -a output
filter_ssh
my -p output