我在 Unix 终端中执行以下操作:
client$: ssh host "cat foo.txt"
并期望文件输出和 ssh 连接关闭,即:
bar
client$:
实际发生的情况是 cat 没有产生任何输出并且终端仍然在远程主机上:
server$ :
我现在可以执行“cat foo.txt”并获取文件内容,然后使用 exit 或 logout 关闭连接。ssh 的输出为:
debug1: Entering interactive session.
debug2: callback start
debug2: client_session2_setup: id 0
debug1: Sending environment.
debug1: Sending env LANG = en_GB
debug2: channel 0: request env confirm 0
debug1: Sending command: cat foo.txt
debug2: channel 0: request exec confirm 0
debug2: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 2097152
我确实有另一个用户帐户,它实际上可以正常工作,但到目前为止,我无法找到可能导致该行为的设置/配置文件差异。工作用户帐户的 ssh 输出为:
debug1: Entering interactive session.
debug2: callback start
debug2: client_session2_setup: id 0
debug1: Sending environment.
debug1: Sending env LANG = en_GB
debug2: channel 0: request env confirm 0
debug1: Sending command: cat foo.txt
debug2: channel 0: request exec confirm 0
debug2: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 2097152
bar
debug2: channel 0: rcvd eof
debug2: channel 0: output open -> drain
debug2: channel 0: obuf empty
debug2: channel 0: close_write
debug2: channel 0: output drain -> closed
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug2: channel 0: rcvd close
debug2: channel 0: close_read
debug2: channel 0: input open -> closed
debug2: channel 0: almost dead
debug2: channel 0: gc: notify user
debug2: channel 0: gc: user detached
debug2: channel 0: send close
debug2: channel 0: is dead
debug2: channel 0: garbage collecting
debug1: channel 0: free: client-session, nchannels 1
debug1: Transferred: stdin 0, stdout 0, stderr 0 bytes in 0.2 seconds
debug1: Bytes per second: stdin 0.0, stdout 0.0, stderr 0.0
debug1: Exit status 0
是否有任何明显的设置可能导致这种情况?或者任何可以提供帮助的 ssh 选项?我查看了 .bashrc 和 .bash_profile,但我看不到任何可能的原因。两个用户都没有 .ssh/config,所以我认为配置是系统范围的 /etc/ssh/ssh_config。运行 ssh -n host "cat foo.txt" 会关闭连接,但 cat 仍然没有输出:
debug1: Entering interactive session.
debug2: callback start
debug2: client_session2_setup: id 0
debug1: Sending environment.
debug1: Sending env LANG = en_GB
debug2: channel 0: request env confirm 0
debug1: Sending command: cat foo.txt
debug2: channel 0: request exec confirm 0
debug2: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 2097152
debug2: channel 0: read<=0 rfd 5 len 0
debug2: channel 0: read failed
debug2: channel 0: close_read
debug2: channel 0: input open -> drain
debug2: channel 0: ibuf empty
debug2: channel 0: send eof
debug2: channel 0: input drain -> closed
debug2: channel 0: rcvd eof
debug2: channel 0: output open -> drain
debug2: channel 0: obuf empty
debug2: channel 0: close_write
debug2: channel 0: output drain -> closed
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug2: channel 0: rcvd close
debug2: channel 0: almost dead
debug2: channel 0: gc: notify user
debug2: channel 0: gc: user detached
debug2: channel 0: send close
debug2: channel 0: is dead
debug2: channel 0: garbage collecting
debug1: channel 0: free: client-session, nchannels 1
debug1: Transferred: stdin 0, stdout 0, stderr 0 bytes in 0.3 seconds
debug1: Bytes per second: stdin 0.0, stdout 0.0, stderr 0.0
debug1: Exit status 0
答案1
尝试使用ssh -T "username@"host" "cat foo.txt"
-T 停止分配伪 tty 终端
或者尝试失败
echo "cat foo.txt" | ssh "username"@"host"
这应该就像ssh "username"@"host" "cat foo.txt"
只是想到了另一个而不是回声,尝试使用
ssh -t -t "username"@"host" "cat foo.txt"
答案2
我不确定这是否与您的情况相关,但我能够通过编辑解决 Ubuntu 16.10 上的类似问题.bashrc
:
从:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) exit;;
esac
到:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
因此,如果你能在配置文件的任何地方找到exit
,也许这就是令人讨厌的人。
只需将其替换为return
。