From 0d286bb5c81ee5364c08092ea08ed695c8f70606 Mon Sep 17 00:00:00 2001 From: luk Date: Mon, 24 Aug 2026 00:54:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=BB=98=E8=AE=A4=E8=BF=9C=E7=A8=8B?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E5=8A=A0=20-t=20=E5=88=86=E9=85=8D=E4=BC=AA?= =?UTF-8?q?=E7=BB=88=E7=AB=AF=EF=BC=8C=E6=94=B9=E7=94=A8=20tmux=20new=20-A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- ssh.sh | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/ssh.sh b/ssh.sh index 848d033..0351ee2 100755 --- a/ssh.sh +++ b/ssh.sh @@ -81,23 +81,26 @@ if [ "$port" = "null" ]; then port=22 fi -# 未提供 $2 时,默认在远程执行:若装有 tmux 则 tmux a(附加到已有会话),否则进入交互式 shell +# 未提供 $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 a || tmux new; else exec "${SHELL:-sh}"; fi' + 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 -Y -p $port $username@$host '$cmd'" - sshpass -p "$password" ssh -Y -p "$port" "$username@$host" "$cmd" + 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 -Y -p $port $username@$host '$cmd'" - ssh -Y -p "$port" "$username@$host" "$cmd" + echo "::*** ssh $ssh_opts -p $port $username@$host '$cmd'" + ssh $ssh_opts -p "$port" "$username@$host" "$cmd" fi else - echo "::*** ssh -Y -p $port $username@$host '$cmd'" - ssh -Y -p "$port" "$username@$host" "$cmd" + echo "::*** ssh $ssh_opts -p $port $username@$host '$cmd'" + ssh $ssh_opts -p "$port" "$username@$host" "$cmd" fi \ No newline at end of file