riot: add scp

This commit is contained in:
xiongdian.me 2023-05-23 20:32:43 +08:00
parent 00b7a550ab
commit b33604ac14

View File

@ -47,7 +47,7 @@ get_server_meta()
SERVER=$host SERVER=$host
PORT=${PORT:-12022} PORT=${PORT:-12022}
USERNAME=${USERNAME:-dictxiong} USERNAME=${USERNAME:-dictxiong}
SSH_OPTIONS=$SSH_OPTIONS' -o ProxyJump="ssh@nasp.ob.ac.cn:36022"' SSH_OPTIONS="$SSH_OPTIONS -o ProxyJump=ssh@nasp.ob.ac.cn:36022"
trust_server=1 trust_server=1
;; ;;
proxied ) proxied )
@ -63,22 +63,34 @@ get_server_meta()
SERVER="$arg" SERVER="$arg"
esac esac
if [[ "$trust_server" == "1" ]]; then if [[ "$trust_server" == "1" ]]; then
SSH_OPTIONS=$SSH_OPTIONS' -o ForwardX11=yes -o ForwardAgent=yes' SSH_OPTIONS="$SSH_OPTIONS -o ForwardX11=yes -o ForwardAgent=yes"
fi
}
eval_or_echo() {
if [[ "$DFS_DRY_RUN" == "1" ]]; then
echo $@
else
eval $@
fi fi
} }
# ssh # ssh
SSH_BIN=ssh prepare_ssh_cmd() {
SSH_PORT_PARAM='-p' local ssh_bin="${1:-ssh}"
if [[ "$ssh_bin" == "scp" || "$ssh_bin" == "sftp" ]]; then
local port_param='-P'
else
local port_param='-p'
fi
echo "$ssh_bin ${PORT:+$port_param} $PORT $SSH_OPTIONS $SCP_SRC $USERNAME${USERNAME:+@}$SERVER $SCP_DST"
}
run_ssh() run_ssh()
{ {
CMD="$SSH_BIN ${PORT:+$SSH_PORT_PARAM} $PORT $SSH_OPTIONS $USERNAME${USERNAME:+@}$SERVER" local cmd="$(prepare_ssh_cmd $1)"
fmt_note "-->" $CMD fmt_note "-->" $cmd
if [[ "$DFS_DRY_RUN" == "1" ]]; then eval_or_echo $cmd
echo $CMD
else
eval $CMD
fi
} }
# sshl # sshl
@ -98,21 +110,42 @@ run_sshl()
do do
continue continue
done done
CMD="ssh ${PORT:+-p} $PORT $SSH_OPTIONS -NC -L $port:$arg $USERNAME${USERNAME:+@}$SERVER"
fmt_note "-->" $CMD SSH_OPTIONS="$SSH_OPTIONS -NC -L $port:$arg"
local cmd="$(prepare_ssh_cmd ssh)"
fmt_note "-->" $cmd
fmt_note " > please access localhost:$port" fmt_note " > please access localhost:$port"
if [[ "$DFS_DRY_RUN" == "1" ]]; then eval_or_echo $cmd
echo $CMD }
else
eval $CMD # scp
run_scp() {
local src="$1"
local dst="$2"
local dst_is_remote=1
# whoever is ./*, it can't be the remote; whoever not exists on local, it's possible the remote.
# it is suggested to use ./* for local files.
if [[ "$src" != "./"* && ( "$dst" == "./"* || ( ! -e "$src" && -e "$dst" ) ) ]]; then
dst_is_remote=0
fi fi
if [[ "$dst_is_remote" == "1" ]]; then
SCP_SRC=\""$src"\"
SERVER="$SERVER":\""$dst"\"
else
SERVER="$SERVER":\""$src"\"
SCP_DST=\""$dst"\"
fi
SSH_OPTIONS="$SSH_OPTIONS -r"
local cmd="$(prepare_ssh_cmd scp)"
fmt_note "-->" $cmd
eval_or_echo $cmd
} }
# main # main
print_help() print_help()
{ {
fmt_info "usage: $0 <service> [command] [options]" fmt_info "usage: $0 <service> [command] [options]"
echo "available commands: ssh (default), sshl (ssh -L)" echo "available commands: ssh (default), sshl (ssh -L), zssh, sftp"
} }
router() router()
@ -131,17 +164,17 @@ router()
run_ssh run_ssh
;; ;;
zssh ) zssh )
SSH_BIN=zssh run_ssh zssh
run_ssh
;; ;;
sftp ) sftp )
SSH_BIN=sftp run_ssh sftp
SSH_PORT_PARAM='-P'
run_ssh
;; ;;
sshl ) sshl )
run_sshl "$3" run_sshl "$3"
;; ;;
scp )
run_scp "$3" "$4"
;;
* ) * )
print_help print_help
fmt_fatal "unknown command: $2" fmt_fatal "unknown command: $2"