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,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

View File

@ -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

View File

@ -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 <<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
-u, <user> remote user if not available in host
-s, <paswd> 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

View File

@ -30,7 +30,7 @@ ssh() {
cat <<EOF
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
hH:tu:dF:p:ro:k:
-h
@ -113,10 +113,11 @@ EOF
# # echo setting multipass
# mp=true
# ;;
*) echo unknown run option -$OPTARG
*)
echo unknown script option $OPTARG
help
return 3
# opts="$opts ${@:$OPTIND:1}"
# opts+=" ${@:$OPTIND:1}"
# ((OPTIND+=1))
#echo remaining ${@:$OPTIND}
;;