【问题标题】:"session is down" error when opening an SSH channel with JSch使用 JSch 打开 SSH 通道时出现“会话已关闭”错误
【发布时间】:2018-05-04 14:41:57
【问题描述】:

我用 JSch 制作了一个 SSH 客户端。客户端与我的 Apache Mina SSH 服务器正常工作。但是当我用真机测试它时,它失败了。

这是客户端的代码:

public boolean openConnection() throws ItsSshException {

   boolean connectSuccess = false;
   Properties config = new Properties();
   config.put("StrictHostKeyChecking", "no");
   jschSSH.setConfig(config);
    try {
     sshSession = jschSSH.getSession(username, hostname, port);
     sshSession.setPassword(password);
     sshSession.connect(connectionTimeout);
     LOGGER.info("Connection timeout : " + connectionTimeout);
     Thread.sleep(1000);
     sshHChannel = sshSession.openChannel("shell");
     sshHChannel.connect();
     in = sshHChannel.getInputStream();
     out = new PrintStream(sshHChannel.getOutputStream());
     clearInitialSocketState();
     connectSuccess = true;
   } catch (Exception e) {
     LOGGER.error("Error during connecting to host: " + hostname +
                  ", port: " + port + "!", e);
     throw new ItsSshException("Error during connecting to host: " + e.getMessage());
   }
   LOGGER.info("connectSuccess : " + connectSuccess);
   return connectSuccess;
}

此时代码失败:

sshHChannel = sshSession.openChannel("shell");

连接主机时出错:127.0.0.1,端口:22!
com.jcraft.jsch.JSchException:会话已关闭
在 com.jcraft.jsch.Session.openChannel(Session.java:861)

我的问题是如何解决这个问题以及什么会导致它发生?

这是 JSch 日志:

INFO: Connecting to 127.0.0.1 port 22 
INFO: Connection established 
INFO: Remote version string: SSH-2.0-Mocana SSH
INFO: Local version string: SSH-2.0-JSCH-0.1.54
INFO: CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
INFO: aes256-ctr is not available.
INFO: aes192-ctr is not available.
INFO: aes256-cbc is not available.
INFO: aes192-cbc is not available.
INFO: CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
INFO: CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO: SSH_MSG_KEXINIT sent
INFO: SSH_MSG_KEXINIT received
INFO: kex: server: diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
INFO: kex: server: ssh-dss,ssh-rsa
INFO: kex: server: aes256-cbc,rijndael256-cbc,aes192-cbc,rijndael192-cbc,aes128-cbc,rijndael128-cbc,3des-cbc,arcfour
INFO: kex: server: aes256-cbc,rijndael256-cbc,aes192-cbc,rijndael192-cbc,aes128-cbc,rijndael128-cbc,3des-cbc,arcfour
INFO: kex: server: hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96
INFO: kex: server: hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96
INFO: kex: server: none
INFO: kex: server: none
INFO: kex: server: 
INFO: kex: server: 
INFO: kex: client: diffie-hellman-group1-sha1
INFO: kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
INFO: kex: client: none
INFO: kex: client: none
INFO: kex: client: 
INFO: kex: client: 
INFO: kex: server->client aes128-cbc hmac-md5 none
INFO: kex: client->server aes128-cbc hmac-md5 none
INFO: SSH_MSG_KEXDH_INIT sent
INFO: expecting SSH_MSG_KEXDH_REPLY
INFO: ssh_rsa_verify: signature true
WARN: Permanently added '127.0.0.1' (RSA) to the list of known hosts.
INFO: SSH_MSG_NEWKEYS sent
INFO: SSH_MSG_NEWKEYS received
INFO: SSH_MSG_SERVICE_REQUEST sent
INFO: SSH_MSG_SERVICE_ACCEPT received
INFO: Authentications that can continue: publickey,keyboard-interactive,password
INFO: Next authentication method: publickey
INFO: Authentications that can continue: keyboard-interactive,password
INFO: Next authentication method: keyboard-interactive
INFO: Authentications that can continue: password
INFO: Next authentication method: password
INFO: Authentication succeeded (password).
INFO: Caught an exception, leaving main loop due to End of IO Stream Read
INFO: Disconnecting from 127.0.0.1 port 22

【问题讨论】:

    标签: java shell ssh jsch


    【解决方案1】:

    尝试使用:

    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    config.put("PreferredAuthentications", "password");
    jschSSH.setConfig(config);
    

    还有设置

    sshSession.connect(connectionTimeout) to 5000 or more.
    

    正如马丁所说

    remove Thread.sleep(1000);
    

    【讨论】:

    • 认证成功,设置PreferredAuthentications无济于事。 + 不要在不解释后果的情况下推荐任何人使用StrictHostKeyChecking=no
    • 感谢您的详细解答!
    • 谢谢你,工作正常
    【解决方案2】:

    删除这个:

    Thread.sleep(1000);
    

    服务器可能在等待来自客户端 (JSch) 的请求时超时并中止连接。

    【讨论】:

    • 感谢您的详细解答!你帮了我很多。
    • 关注这种 SO 交互的历史(4 年后)和 SMH。这应该是答案。
    猜你喜欢
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    相关资源
    最近更新 更多