improve linux-ssh-operator-skill: bug fixes, rsync support, tunneling docs

- Fix dry-run bug in ssh_alias_setup.sh (pub key check skipped when key was never generated)
- Remove dead code in ssh_run.sh (-- check after host arg was never reachable)
- Add --rsync/--delete flags to ssh_copy.sh for incremental rsync transfers
- Add -C/--compress flag to ssh_run.sh for slow-link connections
- Expand SKILL.md/SKILL.zh-CN.md: tunneling, jump host, SSH multiplexing, Docker ops
- Expand ssh-playbook.md: port forwarding, bastion, ControlMaster, structured troubleshooting table

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-13 17:26:47 +08:00
parent dea5db60d9
commit 766eb935a9
6 changed files with 363 additions and 91 deletions

View File

@@ -118,7 +118,7 @@ if [[ ! -f "$key" ]]; then
fi
fi
if [[ ! -f "$key_pub" ]]; then
if ! $dry_run && [[ ! -f "$key_pub" ]]; then
echo "Public key not found: $key_pub" >&2
exit 1
fi

View File

@@ -3,7 +3,7 @@ set -euo pipefail
usage() {
cat <<'USAGE'
Copy files via scp with consistent options.
Copy files via scp or rsync with consistent options.
Usage:
ssh_copy.sh [options] push HOST LOCAL_PATH REMOTE_PATH
@@ -13,9 +13,11 @@ Options:
-u, --user USER Override SSH user (or set REMOTE_USER)
-p, --port PORT SSH port (default: REMOTE_PORT or 22)
-i, --key PATH Identity file (default: REMOTE_KEY)
-r, --recursive Copy directories recursively
-r, --recursive Copy directories recursively (scp mode only; rsync always recurses)
--rsync Use rsync instead of scp (incremental, shows progress)
--delete With --rsync push: delete remote files not present in source
--accept-new Set StrictHostKeyChecking=accept-new
--dry-run Print the scp command that would run
--dry-run Print the command that would run
-h, --help Show help
Environment defaults:
@@ -23,6 +25,8 @@ Environment defaults:
Examples:
ssh_copy.sh push my-server ./app.tar.gz /tmp/app.tar.gz
ssh_copy.sh --rsync push my-server ./dist/ /var/www/html/
ssh_copy.sh --rsync --delete push my-server ./dist/ /var/www/html/
ssh_copy.sh --user ubuntu pull 10.0.0.1 /var/log/syslog ./syslog
USAGE
}
@@ -32,47 +36,28 @@ user="${REMOTE_USER:-}"
key="${REMOTE_KEY:-}"
recursive=false
use_rsync=false
delete_mode=false
accept_new=false
dry_run=false
while [[ $# -gt 0 ]]; do
case "$1" in
-u|--user)
user="${2:-}"
shift 2
;;
-p|--port)
port="${2:-}"
shift 2
;;
-i|--key)
key="${2:-}"
shift 2
;;
-r|--recursive)
recursive=true
shift
;;
--accept-new)
accept_new=true
shift
;;
--dry-run)
dry_run=true
shift
;;
-h|--help)
usage
exit 0
;;
-u|--user) user="${2:-}"; shift 2 ;;
-p|--port) port="${2:-}"; shift 2 ;;
-i|--key) key="${2:-}"; shift 2 ;;
-r|--recursive) recursive=true; shift ;;
--rsync) use_rsync=true; shift ;;
--delete) delete_mode=true; shift ;;
--accept-new) accept_new=true; shift ;;
--dry-run) dry_run=true; shift ;;
-h|--help) usage; exit 0 ;;
-*)
echo "Unknown option: $1" >&2
usage >&2
exit 2
;;
*)
break
;;
*) break ;;
esac
done
@@ -81,11 +66,8 @@ if [[ $# -lt 4 ]]; then
exit 2
fi
direction="$1"
shift
host="$1"
shift
direction="$1"; shift
host="$1"; shift
dest_host="$host"
if [[ -n "$user" ]]; then
@@ -93,34 +75,70 @@ if [[ -n "$user" ]]; then
dest_host="${user}@${host_no_user}"
fi
scp_opts=(-P "$port" -p)
if [[ -n "$key" ]]; then
scp_opts+=(-i "$key" -o "IdentitiesOnly=yes")
fi
if $recursive; then
scp_opts+=(-r)
fi
if $accept_new; then
scp_opts+=(-o "StrictHostKeyChecking=accept-new")
fi
if $use_rsync; then
if ! command -v rsync >/dev/null 2>&1; then
echo "rsync is required but not installed." >&2
exit 1
fi
ssh_parts=("ssh" "-p" "$port")
if [[ -n "$key" ]]; then
ssh_parts+=("-i" "$key" "-o" "IdentitiesOnly=yes")
fi
if $accept_new; then
ssh_parts+=("-o" "StrictHostKeyChecking=accept-new")
fi
rsync_opts=(-avz --progress -e "${ssh_parts[*]}")
case "$direction" in
push)
local_path="$1"
remote_path="$2"
full_cmd=(scp "${scp_opts[@]}" "$local_path" "${dest_host}:${remote_path}")
;;
pull)
remote_path="$1"
local_path="$2"
full_cmd=(scp "${scp_opts[@]}" "${dest_host}:${remote_path}" "$local_path")
;;
*)
echo "Unknown direction: $direction (expected: push|pull)" >&2
usage >&2
exit 2
;;
esac
case "$direction" in
push)
local_path="$1"
remote_path="$2"
if $delete_mode; then
rsync_opts+=(--delete)
fi
full_cmd=(rsync "${rsync_opts[@]}" "$local_path" "${dest_host}:${remote_path}")
;;
pull)
remote_path="$1"
local_path="$2"
full_cmd=(rsync "${rsync_opts[@]}" "${dest_host}:${remote_path}" "$local_path")
;;
*)
echo "Unknown direction: $direction (expected: push|pull)" >&2
usage >&2
exit 2
;;
esac
else
scp_opts=(-P "$port" -p)
if [[ -n "$key" ]]; then
scp_opts+=(-i "$key" -o "IdentitiesOnly=yes")
fi
if $recursive; then
scp_opts+=(-r)
fi
if $accept_new; then
scp_opts+=(-o "StrictHostKeyChecking=accept-new")
fi
case "$direction" in
push)
local_path="$1"
remote_path="$2"
full_cmd=(scp "${scp_opts[@]}" "$local_path" "${dest_host}:${remote_path}")
;;
pull)
remote_path="$1"
local_path="$2"
full_cmd=(scp "${scp_opts[@]}" "${dest_host}:${remote_path}" "$local_path")
;;
*)
echo "Unknown direction: $direction (expected: push|pull)" >&2
usage >&2
exit 2
;;
esac
fi
if $dry_run; then
printf '%q ' "${full_cmd[@]}"

View File

@@ -14,6 +14,7 @@ Options:
-p, --port PORT SSH port (default: REMOTE_PORT or 22)
-i, --key PATH Identity file (default: REMOTE_KEY)
-t, --tty Force pseudo-tty allocation (useful for sudo prompts)
-C, --compress Enable SSH compression (useful on slow links)
--accept-new Set StrictHostKeyChecking=accept-new
--sudo Prefix command with sudo --
--sudo-non-interactive Prefix command with sudo -n -- (fails if password needed)
@@ -36,6 +37,7 @@ key="${REMOTE_KEY:-}"
connect_timeout="${REMOTE_CONNECT_TIMEOUT:-10}"
tty=false
compress=false
accept_new=false
sudo_mode=""
dry_run=false
@@ -58,6 +60,10 @@ while [[ $# -gt 0 ]]; do
tty=true
shift
;;
-C|--compress)
compress=true
shift
;;
--accept-new)
accept_new=true
shift
@@ -126,14 +132,15 @@ if $accept_new; then
ssh_opts+=(-o "StrictHostKeyChecking=accept-new")
fi
if $compress; then
ssh_opts+=(-C)
fi
if $tty; then
ssh_opts+=(-tt)
fi
cmd=("$@")
if [[ ${#cmd[@]} -gt 0 && "${cmd[0]}" == "--" ]]; then
cmd=("${cmd[@]:1}")
fi
if [[ -n "$sudo_mode" && ${#cmd[@]} -gt 0 ]]; then
if [[ "$sudo_mode" == "sudo-n" ]]; then
cmd=("sudo" "-n" "--" "${cmd[@]}")