2022-03-28 10:05:26 -07:00
|
|
|
#!/bin/bash
|
|
|
|
|
2024-02-21 11:33:37 -08:00
|
|
|
module_load ssh
|
|
|
|
module_load debug
|
2022-03-28 10:05:26 -07:00
|
|
|
|
2024-02-21 11:33:37 -08:00
|
|
|
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
|
2024-02-21 11:33:37 -08:00
|
|
|
|
|
|
|
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
|
2022-03-28 10:05:26 -07:00
|
|
|
# echo source is remote
|
2024-02-21 11:33:37 -08:00
|
|
|
SHOST=${SHOST:-$(sed 's/\(.*\):.*/\1/' <<< "$SRC")}
|
|
|
|
SPATH=$(sed 's/.*:\(.*\)/\1/' <<< "$SRC")
|
2022-03-28 10:05:26 -07:00
|
|
|
else
|
|
|
|
# echo source is local
|
2024-02-21 11:33:37 -08:00
|
|
|
SPATH=$SRC
|
2022-03-28 10:05:26 -07:00
|
|
|
fi
|
2024-03-13 09:20:42 -07:00
|
|
|
|
2024-02-21 11:33:37 -08:00
|
|
|
DEST=$1;shift
|
2022-03-28 10:05:26 -07:00
|
|
|
if [[ $DEST =~ ":" ]]; then
|
2024-03-13 09:20:42 -07:00
|
|
|
# echo destination is remote
|
2024-02-21 11:33:37 -08:00
|
|
|
DHOST=${DHOST:-$(sed 's/\(.*\):.*/\1/' <<< "$DEST")}
|
|
|
|
DPATH=$(sed 's/.*:\(.*\)/\1/' <<< "$DEST")
|
2022-03-28 10:05:26 -07:00
|
|
|
else
|
2024-02-21 11:33:37 -08:00
|
|
|
DPATH=$DEST
|
2022-03-28 10:05:26 -07:00
|
|
|
fi
|
|
|
|
|
2024-03-13 09:20:42 -07:00
|
|
|
[[ ! $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
|
2024-02-21 11:33:37 -08:00
|
|
|
>&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; }
|
2022-03-28 10:05:26 -07:00
|
|
|
|
2024-02-21 11:33:37 -08:00
|
|
|
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
|
2024-02-21 11:33:37 -08:00
|
|
|
done
|
2024-05-01 19:13:54 -07:00
|
|
|
if [[ $sargs ]]; then # if there are extra ssh arguments
|
2024-02-21 11:33:37 -08:00
|
|
|
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
|
2024-03-13 09:20:42 -07:00
|
|
|
args=("${@:1}")
|
2024-02-21 11:33:37 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# # echo parse: $*
|
|
|
|
# if [[ ! $* =~ "--" ]]; then
|
|
|
|
# SOPTS=$*
|
|
|
|
# else
|
|
|
|
# SOPTS=$(sed 's/\(.*\)--.*/\1/' <<< "$*")
|
|
|
|
# OPTS=$(sed 's/.*--\(.*\)/\1/' <<< "$*")
|
|
|
|
# fi
|
2022-03-28 10:05:26 -07:00
|
|
|
|
|
|
|
# echo SOPTS $SOPTS
|
|
|
|
# echo OPTS $OPTS
|
|
|
|
|
|
|
|
module_load array
|
|
|
|
declare -a ret
|
|
|
|
# echo ssh -r "$SOPTS" "${DHOST:-$SHOST}"
|
|
|
|
# return
|
|
|
|
|
2024-02-21 11:33:37 -08:00
|
|
|
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[*]}"
|
|
|
|
|
2024-02-21 11:33:37 -08:00
|
|
|
# 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""
|
2024-03-13 09:20:42 -07:00
|
|
|
# echo $cmd
|
2024-02-21 11:33:37 -08:00
|
|
|
if $([[ $dr ]] && echo "echo ") $cmd; then
|
|
|
|
debug copy success
|
|
|
|
else
|
|
|
|
>&2 echo remote copy failed
|
|
|
|
>&2 echo $cmd
|
|
|
|
return 1
|
|
|
|
fi
|
2022-03-28 10:05:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|