SSH命令执行挂起,尽管交互式shell功能很好 [英] SSH Command Execution Hangs, although interactive shell functions fine

查看:312
本文介绍了SSH命令执行挂起,尽管交互式shell功能很好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用ssh在远程服务器上执行命令时,ssh命令在exec request accepted调试消息后挂起,并最终超时.

When I attempt to execute a command on a remote server with ssh, the ssh command hangs after the exec request accepted debug message, and eventually times out.

失败的命令:ssh -v -v <username>@<server> uptime(也尝试过echo hello等)

The failing command: ssh -v -v <username>@<server> uptime (also tried echo hello etc.)

debug1: Authentication succeeded (publickey).
Authenticated to <server> (<ip>:22).
debug1: channel 0: new [client-session]
debug2: channel 0: send open
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug2: callback start
debug2: client_session2_setup: id 0
debug2: fd 4 setting TCP_NODELAY
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug2: channel 0: request env confirm 0
debug1: Sending command: uptime
debug2: channel 0: request exec confirm 1
debug2: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 2097152
debug2: channel_input_status_confirm: type 99 id 0
debug2: exec request accepted on channel 0

它无限期地挂着.

但是,当我在没有命令的情况下将ssh放入远程服务器时,我得到了一个交互式shell,一切都很好.

When I ssh without a command into my remote server, however, I get an interactive shell and all is good.

成功的命令:ssh -v -v <username>@<server>

输出:

debug1: Authentication succeeded (publickey).
Authenticated to <server> (<ip>:22).
debug1: channel 0: new [client-session]
debug2: channel 0: send open
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug2: callback start
debug2: client_session2_setup: id 0
debug2: fd 4 setting TCP_NODELAY
debug2: channel 0: request pty-req confirm 1
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug2: channel 0: request env confirm 0
debug2: channel 0: request shell confirm 1
debug2: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
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: shell request accepted on channel 0
Welcome!
<prompt>%
...

有人知道为什么交互式会话会成功但命令执行不成功吗?

Has anyone an idea why an interactive session would be successful but a command execution not?

已经困扰我几个月了,因为我再也无法使用统一来同步我的文件了(它曾经可以工作).任何帮助表示赞赏.

Has been haunting me for months now because I cannot use unison to sync my files any more (it used to work). Any help much appreciated.

推荐答案

问题确实是我的登录脚本,尽管与要求终端无关(我怀疑是这样,并用-t-T进行了测试选项).问题是我的.bashrc正在运行exec(在本例中为zsh-因为我们的系统不允许chshzsh).

The problem was indeed my login script, although not to do with requiring a terminal (I'd suspected that and tested with the -t and -T options). The problem was that my .bashrc was running an exec (in this case to zsh - because our system doesn't allow chsh to zsh).

违规行:

test -f /usr/bin/zsh && exec /usr/bin/zsh

已通过首先检查交互式外壳程序并退出(如果是)来解决:

Solved by first checking for interactive shell and exiting if so:

[ -z "$PS1" ] && return
test -f /usr/bin/zsh && exec /usr/bin/zsh

因此,从本质上讲,由于shell正在执行zsh,因此ssh一直在等待此操作完成-从未发生.

So, essentially, because the shell was execing into zsh, ssh was waiting for this to finish - which never happened.

我有点困惑为什么我的.bashrc被调用-我以为这只是用于交互式shell,但是各种初始化脚本的确切目的和顺序是我永远不会想到的.学习.

I am a little confused why my .bashrc was being called at all - I thought this was only for interactive shells, but the exact purpose and order of the various init scripts is something I don't think I'll ever learn.

我希望这对其他在启动脚本中使用exec的人有用.

I hope this can be useful to others who have some kind of exec in their startup scripts.

顺便说一句-其他两个答案都在正确的轨道上,所以我完全不确定我应该回答"还是只是评论他们的答案.如果在stackoverflow上回答我自己的问题在道德上是错误的,请告诉我,我会做的.谢谢其他答复者.

BTW - the other two answers were on the right track so I was completely unsure if I should 'answer' or just comment their answers. If answering my own question is morally wrong on stackoverflow, let me know and I'll do penitence. Thank you to the other answerers.

这篇关于SSH命令执行挂起,尽管交互式shell功能很好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆