Re: [sorry for dups] Re: why difference between interactive+script doing same thing?

2023-03-28 Thread L A Walsh

On 2023/03/27 16:52, Greg Wooledge wrote:


Each function has its own private set of positional parameters ("$@" array)
independent of the main script's "$@".  If you want the funtion to see
a copy of the script's arguments, you need to pass "$@" to it.
  

---
Yeah, forgot that.  Fact was in area of self-memory insufficiently redundant
to survive ischemic event 16 months ago.

Changed to:

fnname "$@"

fnname () {
 my -a cmd=("$@")
 ... ssh ... "${cmd[@]}"
}




Re: [sorry for dups] Re: why difference between interactive+script doing same thing?

2023-03-27 Thread Greg Wooledge
On Mon, Mar 27, 2023 at 04:43:09PM -0700, L A Walsh wrote:
> On 2023/03/27 13:28, Greg Wooledge wrote:
> > You're calling filter_ssh with no arguments, but trying to use "$@"
> > inside it to generate the ssh command.
> Isn't "$@" still valid?  Originally I didn't have func-filterssh, it was
> inline.

Each function has its own private set of positional parameters ("$@" array)
independent of the main script's "$@".  If you want the funtion to see
a copy of the script's arguments, you need to pass "$@" to it.



Re: [sorry for dups] Re: why difference between interactive+script doing same thing?

2023-03-27 Thread L A Walsh

On 2023/03/27 13:05, L A Walsh wrote:

That "$@" is not going to work the way you want it to in the general
case. 


---
While I got rid of $@ in some test versions, it was back in
in later version, so that may be the main flaw at this point.
Will need time to clean this mess up...




Re: [sorry for dups] Re: why difference between interactive+script doing same thing?

2023-03-27 Thread L A Walsh

On 2023/03/27 13:28, Greg Wooledge wrote:

On Mon, Mar 27, 2023 at 01:05:33PM -0700, L A Walsh wrote:
  

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" &1)
   echo "Read ${#output[@]} lines"
   for o in "${output[@]}"; do
   if [[ $o =~ $ign ]]; then continue; fi
   printf "%s" "$o"
   done
}



  

filter_ssh



You're calling filter_ssh with no arguments, but trying to use "$@"
inside it to generate the ssh command.
  
Isn't "$@" still valid?  Originally I didn't have func-filterssh, it was 
inline.

I put it in a func to allow further debugging -- but you are right,
it's messed up.

You're also using dots inside a regex without backslashing them.  And
I'm not sure about the parentheses -- can't tell whether you meant those
to be literal or not.  They don't appear to serve any purpose if they're
not literal (there's no | inside them for example), but you didn't
backslash them either... very confusing.
  
Finally, using \w here is a libc extension and will only work on your

system, not necessarily other systems.  Just FYI.
  


As for the regex, I was originally trying to use grep -P to filter
the lines, again, as that didn't work it got migrated inside the function.

Note, this hacked version only searches for the 1st ignore msg, so the one
with the dots isn't included -- the parens were for grouping only, not 
capturing.


The '|' was to make a union of all the msgs, but it is commented out
to "simplify" things. (""?).  Also note, the \w was from attempt to use
grep-P

Will have to look over and see what simplifications are causing what --\
that's why I went with a separate test script -- apart from the script I had
tried to simplify -- since my problem occurs in the separate test script
apart from all the problems you point out in the original src script.

FWIW -- the multiple copies of that note that got sent to list -- try
to only look at latest DT-stamp, since as my post didn't appear, I examined
it and tried to fix some of the problems I saw...including hard coding the
command sent to ssh (so $@ isn't the problem).





Re: [sorry for dups] Re: why difference between interactive+script doing same thing?

2023-03-27 Thread Greg Wooledge
On Mon, Mar 27, 2023 at 01:05:33PM -0700, L A Walsh wrote:
> 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" 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
> }

> filter_ssh

You're calling filter_ssh with no arguments, but trying to use "$@"
inside it to generate the ssh command.

You're also using dots inside a regex without backslashing them.  And
I'm not sure about the parentheses -- can't tell whether you meant those
to be literal or not.  They don't appear to serve any purpose if they're
not literal (there's no | inside them for example), but you didn't
backslash them either... very confusing.

Finally, using \w here is a libc extension and will only work on your
system, not necessarily other systems.  Just FYI.



[sorry for dups] Re: why difference between interactive+script doing same thing?

2023-03-27 Thread L A Walsh

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" &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