
ssh -vvv -F /home/me/.ssh/config serva -t "source ~/.bashrc"
-vvv
这是我使用标志时得到的输出:
debug1: Authentication succeeded (publickey).
debug1: channel 0: new [client-session]
debug3: ssh_session2_open: channel_new: 0
debug2: channel 0: send open
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug3: Wrote 128 bytes for a total of 2413
debug2: callback start
debug2: client_session2_setup: id 0
debug2: channel 0: request pty-req confirm 1
debug1: Sending command: source ~/.bashrc
debug2: channel 0: request exec confirm 1
debug2: fd 3 setting TCP_NODELAY
debug2: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug3: Wrote 400 bytes for a total of 2813
debug2: channel_input_status_confirm: type 99 id 0
debug2: PTY allocation request accepted on channel 0
debug2: channel 0: rcvd adjust 2097152
debug2: channel_input_status_confirm: type 99 id 0
debug2: exec request accepted on channel 0
debug1: client_input_channel_req: channel 0 rtype exit-status rep
ly 0
debug1: client_input_channel_req: channel 0 rtype [email protected]
reply 0
debug2: channel 0: rcvd eow
debug2: channel 0: close_read
debug2: channel 0: input open -> 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
debug2: channel 0: rcvd close
debug3: channel 0: will not send data after close
debug2: channel 0: almost dead
debug2: channel 0: gc: notify user
debug2: channel 0: output drain -> closed
debug2: channel 0: rcvd close
debug3: channel 0: will not send data after 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
debug3: channel 0: status: The following connections are open:
#0 client-session (t4 r0 i3/0 o3/0 fd -1/-1 cfd -1)
debug3: channel 0: close_fds r -1 w -1 e 6 c -1
debug3: Wrote 32 bytes for a total of 2845
debug3: Wrote 64 bytes for a total of 2909
服务器端日志有以下消息:sshd[18763]: Received disconnect from
...
我使用的是CentOS 6.4
编辑
我原来的问题是有缺陷的。对于那个很抱歉。我想用我想要的 rc 文件(~/.bashrc_temp)执行 bash shell,然后执行其他东西。我认为 PROMPT_COMMAND 看起来是推荐的选项,或者在 ~/.bashrc_temp 本身内执行命令,这不太理想,但我可能可以添加一些条件语句。
答案1
当你这样做时:
ssh serva -t "source ~/.bashrc"
ssh
告诉sshd
调用远程用户的登录 shell,如下所示:
the-shell -c 'source ~/.bashrc'
这告诉 shell 运行该命令并退出。
也许,您想要的是运行交互式 shell 并让该交互式 shell 运行该source ~/.bashrc
命令进而发出提示并阅读更多要执行的命令。
首先请注意,在 的情况下source ~/.bashrc
,这是不需要的,因为在交互时bash
已经提供了来源~/.bashrc
(实际上,在 的情况下ssh
,即使在非交互时也会这样做)。所以:
ssh serva
足够。
现在,如果您想运行命令,然后运行交互式 shell,那么您可以执行以下操作:
ssh -t serva 'cmd; bash'
(在那里,需要 ,-t
因为ssh
在传递要执行的命令时默认情况下不会启动伪终端)
cmd
不过,它不会被执行bash
(它将由 sshd 启动的 shell(您的登录 shell)执行以解释该cmd; bash
命令行)。
如果您希望交互式bash
运行该命令。一个技巧是使用bash
sPROMPT_COMMAND
变量。bash
将该变量的内容解释为要在每个提示之前执行的 shell 代码。所以你可以这样做:
ssh -t serva 'PROMPT_COMMAND="cmd; unset PROMPT_COMMAND" bash'