java com.jcraft.jsch.JSchException: 在 jsch 中打开频道时未打开频道

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33895500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:15:44  来源:igfitidea点击:

com.jcraft.jsch.JSchException: channel is not opened when opening a channel in jsch

javajsch

提问by K Erlandsson

When connecting to a remote host using jsch version 0.1.51 we occasionally run into the following exception when calling Channel.connect()on a ChannelExec.

当使用jsch版本0.1.51连接到远程主机,我们打电话时,偶尔会碰到下面的异常Channel.connect()ChannelExec

com.jcraft.jsch.JSchException: channel is not opened.
    at com.jcraft.jsch.Channel.sendChannelOpen(Channel.java:765)
    at com.jcraft.jsch.Channel.connect(Channel.java:151)
    at com.jcraft.jsch.Channel.connect(Channel.java:145)

The code we use after the session has been created is:

创建会话后我们使用的代码是:

ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand("echo hello");
channel.connect(); // Error here

The Channel.connect()call usually returns in under 100 ms, but when this error ocurrs the call hangs for more than 20 seconds before throwing the exception.

Channel.connect()呼叫通常在100毫秒的回报,但是当这个错误ocurrs超过20秒,通话挂起引发异常之前。

回答by K Erlandsson

The exception message is slightly misleading. The error can occur when there is a timeout waiting for the SSH_MSG_CHANNEL_OPEN_CONFIRMATIONmessage from the server. The default timeout in jsch (version 0.1.51) is 20 seconds. I think there are other situations where the same error occurrs but I have not investigated further.

异常消息有点误导。当等待SSH_MSG_CHANNEL_OPEN_CONFIRMATION来自服务器的消息超时时,可能会发生该错误。jsch(版本 0.1.51)中的默认超时为 20 秒。我认为还有其他情况会发生同样的错误,但我没有进一步调查。

While there probably can be numerous reasons for the timeout, we have seen it caused by reverse DNS lookups in sshd from OpenSSH that took a long time occasionally.

虽然超时的原因可能有很多,但我们已经看到它是由 OpenSSH 的 sshd 中的反向 DNS 查找引起的,偶尔需要很长时间。

That cause can be resolved by disabling DNS lookups from sshd by setting

可以通过设置禁用 sshd 的 DNS 查找来解决该原因

UseDNS no

in your sshd_config (typically /etc/ssh/sshd_config). This is generally safe to do according to what Gilleswrites in thisthread.

在您的 sshd_config (通常/etc/ssh/sshd_config)中。根据Gilles线程中所写的内容,通常是安全的。

Another option is to increase the timeout when connecting the channel. Channel.connectaccepts a timeout argument (milliseconds), e.g. channel.connect(60000). This can be useful if you do not control the server you are connecting to.

另一种选择是增加连接通道时的超时时间。Channel.connect接受超时参数(毫秒),例如channel.connect(60000). 如果您不控制要连接的服务器,这会很有用。