mirror of
https://github.com/DictXiong/dotfiles.git
synced 2025-07-09 02:20:30 +08:00
Compare commits
No commits in common. "70614af310e5f9f9d0a54066922ccb46bd6d0f98" and "4be9eb87f3587e48acdb33d7621b455e00259c69" have entirely different histories.
70614af310
...
4be9eb87f3
215
scripts/riot
215
scripts/riot
@ -4,7 +4,7 @@ THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
|
|||||||
source "$THIS_DIR/../tools/common.sh"
|
source "$THIS_DIR/../tools/common.sh"
|
||||||
RIOT_TRUST_CLIENT=${RIOT_TRUST_CLIENT:-${DFS_TRUST:-0}}
|
RIOT_TRUST_CLIENT=${RIOT_TRUST_CLIENT:-${DFS_TRUST:-0}}
|
||||||
RIOT_TRUST_SERVER=${RIOT_TRUST_SERVER:-0}
|
RIOT_TRUST_SERVER=${RIOT_TRUST_SERVER:-0}
|
||||||
EXTRA_SSH_OPTIONS=()
|
RIOT_EXTRA_OPTIONS=""
|
||||||
|
|
||||||
# config
|
# config
|
||||||
RIOT_CONFIG_FILES=(
|
RIOT_CONFIG_FILES=(
|
||||||
@ -83,12 +83,12 @@ parse_remote() {
|
|||||||
TRUST_SERVER=1
|
TRUST_SERVER=1
|
||||||
PORT="" # optional
|
PORT="" # optional
|
||||||
USERNAME="" # optional
|
USERNAME="" # optional
|
||||||
SSH_OPTIONS=("-o" "RequestTTY=yes")
|
SSH_OPTIONS="-o RequestTTY=yes" # optional
|
||||||
if [[ "$RIOT_TRUST_CLIENT" == "1" ]]; then
|
if [[ "$RIOT_TRUST_CLIENT" == "1" ]]; then
|
||||||
SSH_OPTIONS+=("-o" "PermitLocalCommand=yes")
|
SSH_OPTIONS="$SSH_OPTIONS -o PermitLocalCommand=yes"
|
||||||
if [[ "$(get_os_type)" != "msys" ]]; then
|
if [[ "$(get_os_type)" != "msys" ]]; then
|
||||||
test "$DFS_DRY_RUN" = "1" || mkdir -p ~/.ssh/master-socket
|
test "$DFS_DRY_RUN" = "1" || mkdir -p ~/.ssh/master-socket
|
||||||
SSH_OPTIONS+=("-o" "ControlMaster=auto" "-o" "ControlPath=~/.ssh/master-socket/%C")
|
SSH_OPTIONS="$SSH_OPTIONS -o ControlMaster=auto -o ControlPath=~/.ssh/master-socket/%C"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
# handle input
|
# handle input
|
||||||
@ -115,30 +115,31 @@ parse_remote() {
|
|||||||
done
|
done
|
||||||
# construct cmd
|
# construct cmd
|
||||||
if [[ "$RIOT_TRUST_SERVER" == "1" || "$TRUST_SERVER" == "1" ]]; then
|
if [[ "$RIOT_TRUST_SERVER" == "1" || "$TRUST_SERVER" == "1" ]]; then
|
||||||
SSH_OPTIONS+=("-o" "ForwardX11=yes" "-o" "ForwardAgent=yes")
|
SSH_OPTIONS="$SSH_OPTIONS -o ForwardX11=yes -o ForwardAgent=yes"
|
||||||
fi
|
fi
|
||||||
if [[ -n "$jump_servers" ]]; then
|
if [[ -n "$jump_servers" ]]; then
|
||||||
SSH_OPTIONS+=("-o" "ProxyJump=$jump_servers")
|
SSH_OPTIONS="$SSH_OPTIONS -o ProxyJump=$jump_servers"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
eval_or_echo() {
|
eval_or_echo() {
|
||||||
|
local cmd="$@"
|
||||||
local DO=""
|
local DO=""
|
||||||
local tmux_win=0
|
|
||||||
if [[ "$DFS_DRY_RUN" == "1" ]]; then
|
if [[ "$DFS_DRY_RUN" == "1" ]]; then
|
||||||
DO=echo
|
DO=echo
|
||||||
fi
|
fi
|
||||||
if [[ "$USE_TMUX" == "1" ]]; then
|
if [[ "$RIOT_USE_TMUX" == "1" ]]; then
|
||||||
if [[ -z "$TMUX_SESS" ]]; then
|
if [[ -z "$RIOT_TMUX_SESS" ]]; then
|
||||||
TMUX_SESS=riot-$(date +%s)
|
RIOT_TMUX_SESS=riot-$(date +%s)
|
||||||
$DO tmux new-session -d -s $TMUX_SESS bash -l
|
RIOT_TMUX_WIN=0
|
||||||
|
$DO tmux new-session -d -s $RIOT_TMUX_SESS bash -l
|
||||||
else
|
else
|
||||||
tmux_win=$((tmux_win+1))
|
RIOT_TMUX_WIN=$((RIOT_TMUX_WIN+1))
|
||||||
$DO tmux new-window -t $TMUX_SESS:$tmux_win -d bash -l
|
$DO tmux new-window -t $RIOT_TMUX_SESS:$RIOT_TMUX_WIN -d bash -l
|
||||||
fi
|
fi
|
||||||
$DO tmux send-keys -t $TMUX_SESS:$tmux_win "${CMD[@]}" Enter
|
$DO tmux send-keys -t $RIOT_TMUX_SESS:$RIOT_TMUX_WIN "$cmd" Enter
|
||||||
else
|
else
|
||||||
$DO "${CMD[@]}"
|
$DO $cmd
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,25 +151,15 @@ prepare_ssh_cmd() {
|
|||||||
else
|
else
|
||||||
local port_param='-p'
|
local port_param='-p'
|
||||||
fi
|
fi
|
||||||
CMD=(
|
echo "$ssh_bin ${PORT:+$port_param} $PORT $SSH_OPTIONS $RIOT_EXTRA_OPTIONS $SCP_SRC $USERNAME${USERNAME:+@}$SERVER $SCP_DST ${@:2}"
|
||||||
"$ssh_bin"
|
|
||||||
"${PORT:+$port_param}" "$PORT"
|
|
||||||
"${SSH_OPTIONS[@]}"
|
|
||||||
"${EXTRA_SSH_OPTIONS[@]}"
|
|
||||||
"$SCP_SRC"
|
|
||||||
"$USERNAME${USERNAME:+@}$SERVER"
|
|
||||||
"$SCP_DST"
|
|
||||||
"${@:2}"
|
|
||||||
)
|
|
||||||
for i in ${!CMD[@]}; do if [[ -z "${CMD[i]}" ]]; then unset CMD[i]; fi; done
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# ssh
|
# ssh
|
||||||
run_ssh()
|
run_ssh()
|
||||||
{
|
{
|
||||||
prepare_ssh_cmd "$@"
|
local cmd="$(prepare_ssh_cmd $@)"
|
||||||
fmt_note "-->" "${CMD[@]}"
|
fmt_note "-->" $cmd
|
||||||
eval_or_echo
|
eval_or_echo $cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
# sshl
|
# sshl
|
||||||
@ -180,22 +171,24 @@ run_sshl()
|
|||||||
arg=localhost:$arg
|
arg=localhost:$arg
|
||||||
fi
|
fi
|
||||||
local port=$(get_free_port)
|
local port=$(get_free_port)
|
||||||
SSH_OPTIONS+=("-NC" "-L" "$port:$arg")
|
|
||||||
prepare_ssh_cmd ssh
|
SSH_OPTIONS="$SSH_OPTIONS -NC -L $port:$arg"
|
||||||
fmt_note "-->" "${CMD[@]}"
|
local cmd="$(prepare_ssh_cmd ssh)"
|
||||||
|
fmt_note "-->" $cmd
|
||||||
fmt_note " > please access localhost:$port"
|
fmt_note " > please access localhost:$port"
|
||||||
eval_or_echo
|
eval_or_echo $cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
# sshd
|
# sshd
|
||||||
run_sshd()
|
run_sshd()
|
||||||
{
|
{
|
||||||
local port=${1:-$(get_free_port)}
|
local port=$(get_free_port)
|
||||||
SSH_OPTIONS+=("-NC" "-D" "$port")
|
|
||||||
prepare_ssh_cmd ssh
|
SSH_OPTIONS="$SSH_OPTIONS -NC -D $port"
|
||||||
fmt_note "-->" "${CMD[@]}"
|
local cmd="$(prepare_ssh_cmd ssh)"
|
||||||
|
fmt_note "-->" $cmd
|
||||||
fmt_note " > please access localhost:$port"
|
fmt_note " > please access localhost:$port"
|
||||||
eval_or_echo
|
eval_or_echo $cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
# scp
|
# scp
|
||||||
@ -215,27 +208,15 @@ run_scp() {
|
|||||||
SERVER="$SERVER":"$src"
|
SERVER="$SERVER":"$src"
|
||||||
SCP_DST="$dst"
|
SCP_DST="$dst"
|
||||||
fi
|
fi
|
||||||
SSH_OPTIONS+=("-r")
|
SSH_OPTIONS="$SSH_OPTIONS -r"
|
||||||
prepare_ssh_cmd scp
|
local cmd="$(prepare_ssh_cmd scp)"
|
||||||
fmt_note "-->" "${CMD[@]}"
|
fmt_note "-->" $cmd
|
||||||
eval_or_echo
|
eval_or_echo $cmd
|
||||||
}
|
|
||||||
|
|
||||||
# ping
|
|
||||||
run_ping() {
|
|
||||||
CMD=(ping)
|
|
||||||
if [[ "$1" == "ping4" ]]; then
|
|
||||||
CMD+=(-4)
|
|
||||||
elif [[ "$1" == "ping6" ]]; then
|
|
||||||
CMD+=(-6)
|
|
||||||
fi
|
|
||||||
CMD+=(-c 4 "$SERVER")
|
|
||||||
fmt_note "-->" "${CMD[@]}"
|
|
||||||
eval_or_echo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# remove host keys
|
# remove host keys
|
||||||
remove_hostkey() {
|
remove_hostkey()
|
||||||
|
{
|
||||||
local key
|
local key
|
||||||
if [[ -z "$PORT" || "$PORT" == "22" ]]; then
|
if [[ -z "$PORT" || "$PORT" == "22" ]]; then
|
||||||
key=$SERVER
|
key=$SERVER
|
||||||
@ -248,118 +229,94 @@ remove_hostkey() {
|
|||||||
# main
|
# main
|
||||||
print_help()
|
print_help()
|
||||||
{
|
{
|
||||||
fmt_info "usage: $0 [-Ddhlqt] [--dry-run] [--dev] [--help] [--lite] [--quite] [--trust] [--tmux] [--password] [[-o ssh-option]...] remote [command] [--] [ssh-command-args]"
|
fmt_info "usage: $0 <service> [command] [options]"
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
available commands:
|
available commands:
|
||||||
- ssh [ssh-command-args] (default)
|
- ssh (default)
|
||||||
- tmux [ssh-command-args] (run ssh in multiple tmux windows)
|
- tmux (run ssh in multiple tmux windows)
|
||||||
- sshl [local-port:remote-host:]remote-port (ssh -L)
|
- sshl (ssh -L)
|
||||||
- sshd [local-port] (ssh -D)
|
- sshd (ssh -D)
|
||||||
- zssh [ssh-command-args]
|
- zssh
|
||||||
- sftp
|
- sftp
|
||||||
- scp source destination
|
|
||||||
- rm (remove host keys)
|
- rm (remove host keys)
|
||||||
- ping/ping4/ping6 (ping the remote servers)
|
- ping/ping6 (let the remote to ping the given target)
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
router() {
|
router() {
|
||||||
local positional=()
|
if [[ -z "$1" || "$1" == "-h" || "$1" == "--help" ]]; then
|
||||||
while [[ $# > 0 ]]; do
|
print_help
|
||||||
case "$1" in
|
exit
|
||||||
-h|--help )
|
fi
|
||||||
print_help
|
|
||||||
exit 0
|
while [[ "$1" == -* ]]; do
|
||||||
;;
|
if [[ "$1" == "--tmux" ]]; then
|
||||||
-t|--trust )
|
RIOT_USE_TMUX=1
|
||||||
RIOT_TRUST_SERVER=1
|
shift
|
||||||
;;
|
continue
|
||||||
--tmux )
|
elif [[ "$1" == "-t" || "$1" == "--trust" ]]; then
|
||||||
USE_TMUX=1
|
RIOT_TRUST_SERVER=1
|
||||||
;;
|
shift
|
||||||
--password )
|
continue
|
||||||
EXTRA_SSH_OPTIONS+=("-o" "PasswordAuthentication=yes" "-o" "PubkeyAuthentication=no")
|
fi
|
||||||
;;
|
RIOT_EXTRA_OPTIONS="$RIOT_EXTRA_OPTIONS $1"
|
||||||
-o )
|
if [[ "$1" == "-o" ]]; then
|
||||||
EXTRA_SSH_OPTIONS+=("-o" "$2")
|
RIOT_EXTRA_OPTIONS="$RIOT_EXTRA_OPTIONS $2"
|
||||||
shift
|
shift
|
||||||
;;
|
fi
|
||||||
-- )
|
|
||||||
shift
|
|
||||||
positional+=("$@")
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
-* )
|
|
||||||
fmt_fatal "unknown option: $1"
|
|
||||||
;;
|
|
||||||
* )
|
|
||||||
positional+=("$1")
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
IFS=',' read -ra remotes <<< "${positional[0]}"
|
|
||||||
for i in ${!remotes[@]}; do if [[ -z "${remotes[i]}" ]]; then unset remotes[i]; fi; done
|
IFS=',' read -ra remotes <<< "$1"
|
||||||
if [[ "${#positional[@]}" == "0" || "${#remotes[@]}" == "0" ]]; then
|
for ((i=0; i<${#remotes[@]}; i++)); do
|
||||||
print_help
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
for i in ${!remotes[@]}; do
|
|
||||||
remote="${remotes[i]}"
|
remote="${remotes[i]}"
|
||||||
|
if [[ -z "$remote" ]]; then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
local batch_func="${remote}.batch"
|
local batch_func="${remote}.batch"
|
||||||
if is_function "$batch_func"; then
|
if is_function "$batch_func"; then
|
||||||
"$batch_func"
|
"$batch_func"
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
parse_remote "$remote"
|
parse_remote "$remote"
|
||||||
case "${positional[1]}" in
|
case $2 in
|
||||||
ssh|tmux|"" )
|
ssh|tmux|"" )
|
||||||
[[ "${positional[1]}" == tmux ]] && USE_TMUX=1
|
[[ "$2" == tmux ]] && RIOT_USE_TMUX=1
|
||||||
run_ssh ssh "${positional[@]:2}"
|
run_ssh ssh "${@:3}"
|
||||||
;;
|
;;
|
||||||
ping|ping4|ping6 )
|
ping|ping6 )
|
||||||
test "${#positional[@]}" -eq 2 || fmt_fatal "ping requires no arguments"
|
run_ssh ssh "${@:2}"
|
||||||
run_ping "${positional[1]}"
|
|
||||||
;;
|
;;
|
||||||
zssh )
|
zssh )
|
||||||
run_ssh zssh "${positional[@]:2}"
|
run_ssh zssh
|
||||||
;;
|
;;
|
||||||
sftp )
|
sftp )
|
||||||
run_ssh sftp "${positional[@]:2}"
|
run_ssh sftp
|
||||||
;;
|
;;
|
||||||
sshl )
|
sshl )
|
||||||
test -n "${positional[2]}" || fmt_fatal "no target address provided"
|
test -n "$3" || fmt_fatal "no target address provided"
|
||||||
test "${#positional[@]}" -eq 3 || fmt_fatal "sshl requires exactly one argument"
|
run_sshl "$3"
|
||||||
run_sshl "${positional[2]}"
|
|
||||||
;;
|
;;
|
||||||
sshd )
|
sshd )
|
||||||
test "${#positional[@]}" -le 3 || fmt_fatal "sshd requires one or no arguments"
|
run_sshd
|
||||||
if [[ "${#positional[@]}" -eq 3 ]]; then
|
|
||||||
check_port "${positional[2]}" || fmt_fatal "invalid port number: ${positional[2]}"
|
|
||||||
run_sshd "${positional[2]}"
|
|
||||||
else
|
|
||||||
run_sshd
|
|
||||||
fi
|
|
||||||
;;
|
;;
|
||||||
scp )
|
scp )
|
||||||
test "${#positional[@]}" -eq 4 || fmt_fatal "scp requires exactly two arguments: source and destination"
|
test -n "$3" || fmt_fatal "no source path specified"
|
||||||
test -n "${positional[2]}" || fmt_fatal "no source path specified"
|
test -n "$4" || fmt_fatal "no destination path specified"
|
||||||
test -n "${positional[3]}" || fmt_fatal "no destination path specified"
|
run_scp "$3" "$4"
|
||||||
run_scp "${positional[2]}" "${positional[3]}"
|
|
||||||
;;
|
;;
|
||||||
rm )
|
rm )
|
||||||
test "${#positional[@]}" -eq 2 || fmt_fatal "rm requires no arguments"
|
|
||||||
remove_hostkey
|
remove_hostkey
|
||||||
;;
|
;;
|
||||||
* )
|
* )
|
||||||
print_help
|
print_help
|
||||||
fmt_fatal "unknown command: ${positional[1]}"
|
fmt_fatal "unknown command: $2"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
if [[ -n "$TMUX_SESS" && "$DFS_DRY_RUN" != "1" ]]; then
|
if [[ -n "$RIOT_TMUX_SESS" ]]; then
|
||||||
tmux attach-session -t $TMUX_SESS
|
tmux attach-session -t $RIOT_TMUX_SESS
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,10 +13,7 @@ if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
|
|||||||
ARG=""
|
ARG=""
|
||||||
GOT_OPTS=()
|
GOT_OPTS=()
|
||||||
while [[ $# > 0 || -n "$ARG" ]]; do
|
while [[ $# > 0 || -n "$ARG" ]]; do
|
||||||
if [[ -z "$ARG" ]]; then
|
if [[ -z "$ARG" ]]; then ARG=$1; shift; fi
|
||||||
if [[ "$1" == "--" ]]; then GOT_OPTS+=("$@"); break; fi
|
|
||||||
ARG="$1"; shift;
|
|
||||||
fi
|
|
||||||
case $ARG in
|
case $ARG in
|
||||||
-q*|--quite ) export DFS_QUIET=1 ;;
|
-q*|--quite ) export DFS_QUIET=1 ;;
|
||||||
-l*|--lite ) export DFS_LITE=1 ;;
|
-l*|--lite ) export DFS_LITE=1 ;;
|
||||||
|
@ -42,7 +42,6 @@ test $(echo | tools/common.sh ask_for_Yn "test") = "1"
|
|||||||
test $(DFS_QUIET=1 tools/common.sh ask_for_Yn "test") = "1"
|
test $(DFS_QUIET=1 tools/common.sh ask_for_Yn "test") = "1"
|
||||||
test "$(DFS_TRUST=1 riot time@is.impt:2222/yes@you-r.right/you@are.really.recht./ibd./try@it,another@host scp /tmp/ ./tmp -D 2>/dev/null)" = 'scp -P 12022 -o RequestTTY=yes -o PermitLocalCommand=yes -o ControlMaster=auto -o ControlPath=~/.ssh/master-socket/%C -o ProxyJump=time@is.impt:2222,yes@you-r.right:12022,you@are.really.recht:12022,root@ibd:12022 -r try@it.dxng.net:/tmp/ ./tmp
|
test "$(DFS_TRUST=1 riot time@is.impt:2222/yes@you-r.right/you@are.really.recht./ibd./try@it,another@host scp /tmp/ ./tmp -D 2>/dev/null)" = 'scp -P 12022 -o RequestTTY=yes -o PermitLocalCommand=yes -o ControlMaster=auto -o ControlPath=~/.ssh/master-socket/%C -o ProxyJump=time@is.impt:2222,yes@you-r.right:12022,you@are.really.recht:12022,root@ibd:12022 -r try@it.dxng.net:/tmp/ ./tmp
|
||||||
scp -P 12022 -o RequestTTY=yes -o PermitLocalCommand=yes -o ControlMaster=auto -o ControlPath=~/.ssh/master-socket/%C -o ForwardX11=yes -o ForwardAgent=yes -r another@host.dxng.net:/tmp/ ./tmp'
|
scp -P 12022 -o RequestTTY=yes -o PermitLocalCommand=yes -o ControlMaster=auto -o ControlPath=~/.ssh/master-socket/%C -o ForwardX11=yes -o ForwardAgent=yes -r another@host.dxng.net:/tmp/ ./tmp'
|
||||||
test "$(riot you@example.com:55 -tD ssh --password -- ping -c 1 2>/dev/null)" = 'ssh -p 55 -o RequestTTY=yes -o PermitLocalCommand=yes -o ControlMaster=auto -o ControlPath=~/.ssh/master-socket/%C -o ForwardX11=yes -o ForwardAgent=yes -o PasswordAuthentication=yes -o PubkeyAuthentication=no you@example.com ping -c 1'
|
|
||||||
|
|
||||||
# check alias
|
# check alias
|
||||||
alias p114
|
alias p114
|
||||||
|
Loading…
x
Reference in New Issue
Block a user