From d45198b0255082c89ca0c0cd7ec22c5b63847314 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Wed, 13 Mar 2024 09:20:42 -0700 Subject: [PATCH] fix extracted function bug fix extra arguments of ssh-copy sshpubkey remove additional ssh options with -- use -o --- modules/remote.mod | 19 +++++----- modules/ssh-copy.func | 16 ++++----- modules/ssh-pubkey.mod | 78 +++++++++--------------------------------- modules/ssh.func | 7 ++-- 4 files changed, 38 insertions(+), 82 deletions(-) diff --git a/modules/remote.mod b/modules/remote.mod index 8e4dc7e..b30f383 100644 --- a/modules/remote.mod +++ b/modules/remote.mod @@ -132,21 +132,22 @@ EOF # script can come from if [[ $rfn ]]; then - if ! declare -f $rfn >/dev/null; then - if ! module_load $1; then - [[ -f $1 ]] && source $1 - fi - if ! declare -f $rfn >/dev/null; then echo fatal: remote-script, unable to source funtion $rfn, aborting; return 1; fi - fi - shift - local file + # if ! declare -f $rfn >/dev/null; then + # if ! module_load $1; then + # [[ -f $1 ]] && source $1 + # fi + if ! declare -f $rfn >/dev/null; then echo fatal: remote-script, unable to extract funtion $rfn, aborting; return 1; fi + # # a passed script was used + # shift + # fi script=$(mkrfilename temp_function) declare -f $rfn > $script cfn="-f $rfn" - else + else script=$1 shift 1 fi + [[ ! $script ]] && echo fatal: must pass a script to remote run && help && return 1 debug echo host: $host user: $user hostname:$hostname script:$script function to run: $cfn diff --git a/modules/ssh-copy.func b/modules/ssh-copy.func index 3a37cb5..b491308 100644 --- a/modules/ssh-copy.func +++ b/modules/ssh-copy.func @@ -66,25 +66,24 @@ EOF # echo source is local SPATH=$SRC fi - + DEST=$1;shift if [[ $DEST =~ ":" ]]; then - destination is remote + # echo destination is remote DHOST=${DHOST:-$(sed 's/\(.*\):.*/\1/' <<< "$DEST")} DPATH=$(sed 's/.*:\(.*\)/\1/' <<< "$DEST") else DPATH=$DEST fi - [[ ! $DPATH ]] && >&2 echo fatal: scp requires a destination file path && return 1 - if [[ ! $(get_user_host $DHOST) ]] && [[ ! $(get_user_host $SHOST ) ]]; then + [[ ! $DPATH ]] && >&2 echo fatal: scp requires a destination file path && return 1 + if ! get_user_host $DHOST &>/dev/null && ! get_user_host $SHOST &>/dev/null; then >&2 echo fatal: need at least a valid remote source host $SHOST or remote destination host $DHOST, aborting remote copy return 1 fi [[ $DHOST && $SHOST && (! $DHOST = "$SHOST") ]] && { echo full remote copy must be same hosts; return 2; } - # parse ssh arguments from the rest debug $( ( IFS=$','; echo all arguments: "$*" ) ) for ((d=1; d<$#; ++d)); do @@ -100,6 +99,8 @@ EOF # reset script arguments to just those before -- # set -- "${args[@]}" debug $( ( IFS=$','; echo remaining arguments to parse: "$*" ) ) + else + args=("${@:1}") fi # # echo parse: $* @@ -126,11 +127,10 @@ EOF # todo test remote to local copy - local cmd="$sshpass scp ${args[*]} $sshopts $([[ $SHOST ]] && echo "${host}:")$SPATH $([[ $DHOST ]] && echo "${host}:")$DPATH" - # echo $cmd + local cmd=""$sshpass" scp "${args[*]}" "$sshopts" $([[ $SHOST ]] && echo "${host}:")"$SPATH" $([[ $DHOST ]] && echo "${host}:")"$DPATH"" + # echo $cmd if $([[ $dr ]] && echo "echo ") $cmd; then debug copy success - debug $(ssh ${sshargs[*]} ${DHOST:-$SHOST} ls -la $DPATH) else >&2 echo remote copy failed >&2 echo $cmd diff --git a/modules/ssh-pubkey.mod b/modules/ssh-pubkey.mod index 00d364c..c01ffae 100644 --- a/modules/ssh-pubkey.mod +++ b/modules/ssh-pubkey.mod @@ -69,8 +69,8 @@ sshpubkey () { local kname=id_rsa local user - local opts;local dr="true";local rm; local ropts - local vkey; local kuser; local host; local upass; local replace + local opts;local dr="true";local rm; + local vkey; local kuser; local host; local upass; local remove local _sudo; local list; @@ -78,7 +78,7 @@ sshpubkey () { help() { cat < host -- +usage: sshpubkey host -a, put the key at another user on remote. if you want to put it to root use 'root' will require remote sudo -u, remote user if not available in host -s, sudo password for remote if needed @@ -147,54 +147,11 @@ EOF return 2 fi - # parse ssh arguments from the rest - # TODO change to a function to do this - if [[ ! $* =~ "--" ]]; then - debug $( ( IFS=$','; echo all arguments: "$*" ) ) - for ((d=1; d<$#; ++d)); do - [[ ${!d} == "--" ]] && break - done - if [[ $d -lt $# ]]; then # if there are extra ssh arguments - debug found -- at $d - opts=("${@:$d+1:$#}") - debug $( ( IFS=$','; echo "ssh arguments: ${opts[*]}" ) ) - # [[ ! ${sshargs[0]} ]] && { echo missing remote machine, must provide at least a hostname, -- hostname; return 3; } - ropts=("${@:1:$d-1}") - debug $( ( IFS=$','; echo "remaining arguments to parse: ${ropts[*]}" ) ) - fi - fi - - # if [[ ! $* =~ "--" ]]; then - # ropts=$* - # else - # ropts=$(sed 's/\(.*\)--.*/\1/' <<< "$*") - # opts=$(sed 's/.*--\(.*\)/\1/' <<< "$*") - # fi - - debug KEY $key, HOST $host - debug ssh run opts ${ropts[*]} - debug additional ssh opts ${opts[*]} - - # TODO add run remote function to ssh and this won't be required - # module_load array - # declare -a ret - # scmd="ssh -r ${ropts} ${host}" - # # echo "$cmd" - # String::split ret "$($scmd)" , - # host=${ret[0]}; opts+=${ret[1]}; local sshpass=${ret[2]} - # # echo "$host;$opts;$sshpass" - # scmd="$sshpass $(which ssh) $opts $host" + sshsopts=("${@}") user=${user:-$(get_user_host $host)} [[ ! $user ]] && >&2 echo "unable to determine remote user, aborting" && return 2 - # rfcmd () ( - # local fn - # fn=$1 - # shift 1 - # echo "bash -c '$(declare -f $fn); $fn $*'" - # ) - run () ( local func; local _sudo func=$1; shift @@ -202,15 +159,12 @@ EOF kuser="" _sudo=-s fi - remote_script $_sudo $kuser -x $func $host "$@" -- $upass + # echo run opts: $@ + [[ $upass || "${sshsopts[*]}" ]] && opts="-- " + echo remote_script $_sudo $kuser -x $func $host "$@" $opts $upass "${sshsopts[@]}" + remote_script $_sudo $kuser -x $func $host "$@" $opts $upass "${sshsopts[@]}" ) - # echo remote user: $user - - # if [[ $kuser ]]; then - # _sudo="echo '${supass}' | sudo -u ${kuser} --stdin" - # fi - if [[ $list ]]; then run list_keys return $? @@ -220,8 +174,8 @@ EOF ############# REMOVE PUBLIC KEY ################# # todo allow removeall without access to public key echo ">>>>> removing public key: \"$rm\" from ${kuser:-$user}" - [[ ! $dr ]] && replace=" -i" - run rm_key $replace $rm + [[ ! $dr ]] && remove="_remove_" + run rm_key "$remove" "$rm" return $? fi @@ -275,10 +229,10 @@ function cpy_key () { } function rm_key () { - local kname; local replace; local found - [[ $1 = "-i" ]] && { replace=$1; shift 1; } - kname=$* - echo "" + local kname; local remove; local found + echo "$@" + [[ $1 = "_remove_" ]] && remove="-i" && shift 1 + kname="$*" if [[ ! -f $HOME/.ssh/authorized_keys ]]; then echo no $HOME/authorized_keys file nothing to remove else @@ -287,9 +241,9 @@ function rm_key () { if [[ $found ]]; then echo key found in authorized_keys, removing... echo "$found" - if [[ $replace ]]; then + if [[ $remove ]]; then echo "********updated authorized_keys file for $USER *******************" - sed $replace "\,$kname$,d" $HOME/.ssh/authorized_keys + sed $remove "\,$kname$,d" $HOME/.ssh/authorized_keys cat $HOME/.ssh/authorized_keys echo "******************************************************" else diff --git a/modules/ssh.func b/modules/ssh.func index 98b74fb..cb03d9f 100644 --- a/modules/ssh.func +++ b/modules/ssh.func @@ -30,7 +30,7 @@ ssh() { cat < host +ssh