shell-network/modules/ssh-copy.func

71 lines
1.6 KiB
Bash

#!/bin/bash
# usage:
# sshcp src dest <ssh run opts> -- <ssh opts>
sshcp () {
local SRC=$1
local DEST=$2
local SHOST; local DHOST
local OPTS; local SOPTS
# echo remaining options: $*
shift 2
echo $SRC to $DEST
if [[ $SRC =~ ":" ]]; then
# echo source is remote
SHOST=$(sed 's/\(.*\):.*/\1/' <<< "$SRC")
SDIR=$(sed 's/.*:\(.*\)/\1/' <<< "$SRC")
else
# echo source is local
SDIR=$SRC
fi
# echo SHOST $SHOST SDIR $SDIR
if [[ $DEST =~ ":" ]]; then
# echo destination is remote
DHOST=$(sed 's/\(.*\):.*/\1/' <<< "$DEST")
DDIR=$(sed 's/.*:\(.*\)/\1/' <<< "$DEST")
else
# echo destination is local
DDIR=$DEST
fi
# echo DHOST $DHOST DDIR $DDIR
[[ $DHOST && $SHOST && (! $DHOST = "$SHOST") ]] && { echo full remote copy must be same hosts; return 2; }
# 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
module_load ssh
cmd=$(ssh -r ${SOPTS} ${DHOST:-$SHOST})
# echo $cmd
String::split ret "$(ssh -r ${SOPTS} ${DHOST:-$SHOST})" ,
local host=${ret[0]}; local opts=${ret[1]}; local sshpass=${ret[2]}
[[ -d $SDIR && ! $SHOST ]] && OPTS+=" -r"
if [[ $SHOST ]]; then
[[ $(ssh $SOPTS $SHOST test -d $SDIR ) ]] && OPTS+=" -r"
fi
local cmd="$sshpass scp $opts $OPTS $([[ $SHOST ]] && echo "${host}:")$SDIR $([[ $DHOST ]] && echo "${host}:")$DDIR"
echo $cmd
$cmd
return 0
}