shell-network/modules/ssh-copy.func

140 lines
3.3 KiB
Plaintext
Raw Normal View History

#!/bin/bash
module_load ssh
module_load debug
sshcp () {
local SHOST; local DHOST; local dr; local sshargs; local args
local OPTS; local SOPTS; local SRC; local DEST;
2024-05-01 19:13:54 -07:00
local SPATH; local SPATH; local cmd; local sargs
help() {
cat <<EOF
usage:
sshcp <script options> src dest <scp options> -- <ssh options>
-d, destination host
-s, source host
-y, dry run. don't run the rscript just output the command that will be run
-h, this help text
EOF
}
# parse sshcp options
local OPTION
local OPTARG
local OPTIND
while getopts 'd:ys:h' OPTION; do
# echo OPTION $OPTION ARG $OPTARG INDX $OPTIND
case "$OPTION" in
s)
# script filename to run
SHOST=$OPTARG
;;
y)
# dry run
dr=true
;;
d)
# run remote command as another user
DHOST=$OPTARG
;;
h)
help
return 0
;;
*)
>&2 echo fatal: unknown remote script option $OPTION, aborting
help
return 1
;;
esac
done
shift $((OPTIND - 1))
[[ ! $1 ]] && >&2 echo fatal: scp requires a source path to send && return 1
SRC=$1; shift
if [[ $SRC =~ ":" ]]; then
# echo source is remote
SHOST=${SHOST:-$(sed 's/\(.*\):.*/\1/' <<< "$SRC")}
SPATH=$(sed 's/.*:\(.*\)/\1/' <<< "$SRC")
else
# echo source is local
SPATH=$SRC
fi
DEST=$1;shift
if [[ $DEST =~ ":" ]]; then
# 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 &>/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; }
debug $( ( IFS=$','; echo all arguments: "$*" ) )
2024-05-01 19:13:54 -07:00
for ((d=1; d<$#+1; ++d)); do
# echo in loop $d, ${!d}
[[ ${!d} == "--" ]] && sargs=true && break
done
2024-05-01 19:13:54 -07:00
if [[ $sargs ]]; then # if there are extra ssh arguments
sshargs=("${@:$d+1:$#}")
debug $( ( IFS=$','; echo "ssh arguments: ${sshargs[*]}" ) )
args=("${@:1:$d-1}")
debug $( ( IFS=$','; echo remaining arguments to parse: "$*" ) )
2024-05-01 19:13:54 -07:00
else
args=("${@:1}")
fi
# # echo parse: $*
# if [[ ! $* =~ "--" ]]; then
# SOPTS=$*
# else
# SOPTS=$(sed 's/\(.*\)--.*/\1/' <<< "$*")
# OPTS=$(sed 's/.*--\(.*\)/\1/' <<< "$*")
# fi
# echo SOPTS $SOPTS
# echo OPTS $OPTS
module_load array
declare -a ret
# echo ssh -r "$SOPTS" "${DHOST:-$SHOST}"
# return
cmd="ssh -r ${sshargs[*]} ${DHOST:-$SHOST}"
String::split ret "$($cmd)" ,
local host=${ret[0]}; local sshopts=${ret[1]}; local sshpass=${ret[2]}
[[ -d $SPATH && ! $SHOST ]] && args+=(" -r")
2024-05-01 19:13:54 -07:00
echo "${args[*]}"
# todo test remote to local copy
2024-05-01 19:13:54 -07:00
cmd=""$sshpass" scp "${args[*]}" "$sshopts" $([[ $SHOST ]] && echo "${host}:")"$SPATH" $([[ $DHOST ]] && echo "${host}:")"$DPATH""
# echo $cmd
if $([[ $dr ]] && echo "echo ") $cmd; then
debug copy success
else
>&2 echo remote copy failed
>&2 echo $cmd
return 1
fi
}