fix extracted function bug

fix extra arguments of ssh-copy
sshpubkey remove additional ssh options with -- use -o
master
David Kebler 2024-03-13 09:20:42 -07:00
parent c407e99b25
commit d45198b025
4 changed files with 38 additions and 82 deletions

View File

@ -132,14 +132,14 @@ EOF
# script can come from # script can come from
if [[ $rfn ]]; then if [[ $rfn ]]; then
if ! declare -f $rfn >/dev/null; then # if ! declare -f $rfn >/dev/null; then
if ! module_load $1; then # if ! module_load $1; then
[[ -f $1 ]] && source $1 # [[ -f $1 ]] && source $1
fi # fi
if ! declare -f $rfn >/dev/null; then echo fatal: remote-script, unable to source funtion $rfn, aborting; return 1; fi if ! declare -f $rfn >/dev/null; then echo fatal: remote-script, unable to extract funtion $rfn, aborting; return 1; fi
fi # # a passed script was used
shift # shift
local file # fi
script=$(mkrfilename temp_function) script=$(mkrfilename temp_function)
declare -f $rfn > $script declare -f $rfn > $script
cfn="-f $rfn" cfn="-f $rfn"
@ -147,6 +147,7 @@ EOF
script=$1 script=$1
shift 1 shift 1
fi fi
[[ ! $script ]] && echo fatal: must pass a script to remote run && help && return 1 [[ ! $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 debug echo host: $host user: $user hostname:$hostname script:$script function to run: $cfn

View File

@ -69,22 +69,21 @@ EOF
DEST=$1;shift DEST=$1;shift
if [[ $DEST =~ ":" ]]; then if [[ $DEST =~ ":" ]]; then
destination is remote # echo destination is remote
DHOST=${DHOST:-$(sed 's/\(.*\):.*/\1/' <<< "$DEST")} DHOST=${DHOST:-$(sed 's/\(.*\):.*/\1/' <<< "$DEST")}
DPATH=$(sed 's/.*:\(.*\)/\1/' <<< "$DEST") DPATH=$(sed 's/.*:\(.*\)/\1/' <<< "$DEST")
else else
DPATH=$DEST DPATH=$DEST
fi 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 >&2 echo fatal: need at least a valid remote source host $SHOST or remote destination host $DHOST, aborting remote copy
return 1 return 1
fi fi
[[ $DHOST && $SHOST && (! $DHOST = "$SHOST") ]] && { echo full remote copy must be same hosts; return 2; } [[ $DHOST && $SHOST && (! $DHOST = "$SHOST") ]] && { echo full remote copy must be same hosts; return 2; }
# parse ssh arguments from the rest # parse ssh arguments from the rest
debug $( ( IFS=$','; echo all arguments: "$*" ) ) debug $( ( IFS=$','; echo all arguments: "$*" ) )
for ((d=1; d<$#; ++d)); do for ((d=1; d<$#; ++d)); do
@ -100,6 +99,8 @@ EOF
# reset script arguments to just those before -- # reset script arguments to just those before --
# set -- "${args[@]}" # set -- "${args[@]}"
debug $( ( IFS=$','; echo remaining arguments to parse: "$*" ) ) debug $( ( IFS=$','; echo remaining arguments to parse: "$*" ) )
else
args=("${@:1}")
fi fi
# # echo parse: $* # # echo parse: $*
@ -126,11 +127,10 @@ EOF
# todo test remote to local copy # todo test remote to local copy
local cmd="$sshpass scp ${args[*]} $sshopts $([[ $SHOST ]] && echo "${host}:")$SPATH $([[ $DHOST ]] && echo "${host}:")$DPATH" local cmd=""$sshpass" scp "${args[*]}" "$sshopts" $([[ $SHOST ]] && echo "${host}:")"$SPATH" $([[ $DHOST ]] && echo "${host}:")"$DPATH""
# echo $cmd # echo $cmd
if $([[ $dr ]] && echo "echo ") $cmd; then if $([[ $dr ]] && echo "echo ") $cmd; then
debug copy success debug copy success
debug $(ssh ${sshargs[*]} ${DHOST:-$SHOST} ls -la $DPATH)
else else
>&2 echo remote copy failed >&2 echo remote copy failed
>&2 echo $cmd >&2 echo $cmd

View File

@ -69,8 +69,8 @@ sshpubkey () {
local kname=id_rsa local kname=id_rsa
local user local user
local opts;local dr="true";local rm; local ropts local opts;local dr="true";local rm;
local vkey; local kuser; local host; local upass; local replace local vkey; local kuser; local host; local upass; local remove
local _sudo; local list; local _sudo; local list;
@ -78,7 +78,7 @@ sshpubkey () {
help() { help() {
cat <<EOF cat <<EOF
usage: sshpubkey <pubkey opts> host <ssh run options> -- <more ssh options> usage: sshpubkey <pubkey opts> host <ssh script options>
-a, <alternate user> put the key at another user on remote. if you want to put it to root use 'root' will require remote sudo -a, <alternate user> put the key at another user on remote. if you want to put it to root use 'root' will require remote sudo
-u, <user> remote user if not available in host -u, <user> remote user if not available in host
-s, <paswd> sudo password for remote if needed -s, <paswd> sudo password for remote if needed
@ -147,54 +147,11 @@ EOF
return 2 return 2
fi fi
# parse ssh arguments from the rest sshsopts=("${@}")
# 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"
user=${user:-$(get_user_host $host)} user=${user:-$(get_user_host $host)}
[[ ! $user ]] && >&2 echo "unable to determine remote user, aborting" && return 2 [[ ! $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 () ( run () (
local func; local _sudo local func; local _sudo
func=$1; shift func=$1; shift
@ -202,15 +159,12 @@ EOF
kuser="" kuser=""
_sudo=-s _sudo=-s
fi 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 if [[ $list ]]; then
run list_keys run list_keys
return $? return $?
@ -220,8 +174,8 @@ EOF
############# REMOVE PUBLIC KEY ################# ############# REMOVE PUBLIC KEY #################
# todo allow removeall without access to public key # todo allow removeall without access to public key
echo ">>>>> removing public key: \"$rm\" from ${kuser:-$user}" echo ">>>>> removing public key: \"$rm\" from ${kuser:-$user}"
[[ ! $dr ]] && replace=" -i" [[ ! $dr ]] && remove="_remove_"
run rm_key $replace $rm run rm_key "$remove" "$rm"
return $? return $?
fi fi
@ -275,10 +229,10 @@ function cpy_key () {
} }
function rm_key () { function rm_key () {
local kname; local replace; local found local kname; local remove; local found
[[ $1 = "-i" ]] && { replace=$1; shift 1; } echo "$@"
kname=$* [[ $1 = "_remove_" ]] && remove="-i" && shift 1
echo "" kname="$*"
if [[ ! -f $HOME/.ssh/authorized_keys ]]; then if [[ ! -f $HOME/.ssh/authorized_keys ]]; then
echo no $HOME/authorized_keys file nothing to remove echo no $HOME/authorized_keys file nothing to remove
else else
@ -287,9 +241,9 @@ function rm_key () {
if [[ $found ]]; then if [[ $found ]]; then
echo key found in authorized_keys, removing... echo key found in authorized_keys, removing...
echo "$found" echo "$found"
if [[ $replace ]]; then if [[ $remove ]]; then
echo "********updated authorized_keys file for $USER *******************" 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 cat $HOME/.ssh/authorized_keys
echo "******************************************************" echo "******************************************************"
else else

View File

@ -30,7 +30,7 @@ ssh() {
cat <<EOF cat <<EOF
usage: usage:
ssh <setup options> host <SSH options> <commands to run on remote> ssh <script options> host <SSH command options> <commands to run on remote>
put any additional SSH (man ssh) options after the host, aborting put any additional SSH (man ssh) options after the host, aborting
hH:tu:dF:p:ro:k: hH:tu:dF:p:ro:k:
-h -h
@ -113,10 +113,11 @@ EOF
# # echo setting multipass # # echo setting multipass
# mp=true # mp=true
# ;; # ;;
*) echo unknown run option -$OPTARG *)
echo unknown script option $OPTARG
help help
return 3 return 3
# opts="$opts ${@:$OPTIND:1}" # opts+=" ${@:$OPTIND:1}"
# ((OPTIND+=1)) # ((OPTIND+=1))
#echo remaining ${@:$OPTIND} #echo remaining ${@:$OPTIND}
;; ;;