Files
sysconfig/ssh.sh
luk 0d286bb5c8 fix: 默认远程连接加 -t 分配伪终端,改用 tmux new -A
ssh 携带命令时不分配 PTY,导致 tmux 报 open terminal failed:
not a terminal、回退 shell 无提示符。未传 $2 时改用 ssh_opts="-Y -t",
默认命令改为 tmux new -A(有会话则附加,无则新建)。

Co-Authored-By: AtomCode (deepseek-v4-flash) <noreply@atomgit.com>
2026-08-24 00:54:01 +08:00

106 lines
3.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
echo "Usage: $(basename $0) [host_to_ssh] [cmd_to_run]"
# Ensure jq is installed
if ! command -v jq &> /dev/null; then
echo "jq is required but not installed. Please install jq and run the script again."
exit 1
fi
# Path to the settings.json file
SETTINGS_FILE="$HOME/Library/Application Support/Code/User/settings.json"
if [[ ! -f "$SETTINGS_FILE" ]]; then
SETTINGS_FILE="$HOME/product_产品/.vscode/settings.json"
if [[ ! -f "$SETTINGS_FILE" ]]; then
echo "settings.json file not found!"
exit 1
fi
fi
# Parse the JSON to get labels and corresponding details
labels=($(jq -r '.["sshfs.configs"][] | .label' "$SETTINGS_FILE"))
hosts=($(jq -r '.["sshfs.configs"][] | .host' "$SETTINGS_FILE"))
ports=($(jq -r '.["sshfs.configs"][] | .port' "$SETTINGS_FILE"))
names=($(jq -r '.["sshfs.configs"][] | .name' "$SETTINGS_FILE"))
usernames=($(jq -r '.["sshfs.configs"][] | .username' "$SETTINGS_FILE"))
passwords=($(jq -r '.["sshfs.configs"][] | .password' "$SETTINGS_FILE"))
privateKeyPaths=($(jq -r '.["sshfs.configs"][] | .privateKeyPath' "$SETTINGS_FILE"))
options=()
prefixes=()
for i in "${!labels[@]}"; do
option=$(echo "${labels[$i]}" | sed 's/[^[:alnum:]].*//') # 提取前几个字母和数字
options+=("$option: ${labels[$i]}") # 去掉序号保留前缀和完整label
prefixes+=("$option") # 保存前缀
done
# Check if a command-line argument is provided
if [ "$1" != "" ]; then
# 如果提供了参数,则直接使用这个前缀
host_to_ssh=$1
else
echo "Select a target to connect via SSH, or press 0 to exit:"
echo
# Display options without numbers
for opt in "${options[@]}"; do
echo "$opt"
done
echo
read -p "请输入要连接的主机前缀: " host_to_ssh
fi
echo
# 查找用户输入对应的索引
selected_index=-1
for i in "${!prefixes[@]}"; do
if [[ "${prefixes[$i]}" == "$host_to_ssh" ]]; then
selected_index=$i
break
fi
done
# 如果没有找到匹配的索引
if [ "$selected_index" -eq -1 ]; then
echo "无效的选择,请重试。"
exit 1
fi
label="${labels[$selected_index]}"
name="${names[$selected_index]}"
host="${hosts[$selected_index]}"
port="${ports[$selected_index]}"
username="${usernames[$selected_index]}"
password="${passwords[$selected_index]}"
privateKeyPath="${privateKeyPaths[$selected_index]}"
if [ "$port" = "null" ]; then
port=22
fi
# 未提供 $2 时,默认在远程执行:若装有 tmux 则 tmux a附加到已有会话否则进入交互式 shell。
# 此时必须加 -t 强制分配伪终端,否则 tmux 报 "open terminal failed: not a terminal",回退 shell 也不显示提示符
if [ -n "$2" ]; then
cmd="$2"
ssh_opts="-Y"
else
cmd='if command -v tmux >/dev/null 2>&1; then tmux new -A; else exec "${SHELL:-sh}"; fi'
ssh_opts="-Y -t"
fi
echo "::*** Connecting to ${label}"
if [ "$password" != "null" ]; then
if command -v sshpass &> /dev/null; then
echo "::*** sshpass -p $password $ssh_opts -p $port $username@$host '$cmd'"
sshpass -p "$password" ssh $ssh_opts -p "$port" "$username@$host" "$cmd"
else
echo "::*** ssh $ssh_opts -p $port $username@$host '$cmd'"
ssh $ssh_opts -p "$port" "$username@$host" "$cmd"
fi
else
echo "::*** ssh $ssh_opts -p $port $username@$host '$cmd'"
ssh $ssh_opts -p "$port" "$username@$host" "$cmd"
fi