【问题标题】:Jsch How to reuse a sessionJsch 如何重用会话
【发布时间】:2012-10-01 23:14:51
【问题描述】:

我正在创建许多不同的(会话)对象,将登录凭据从 Oracle pl/sql 包传递给 java 类。然后我将这个 (Session) 对象存储在 Vector 中。这个想法是连接、打开所需的通道、关闭通道并最终断开特定会话与此向量的连接。我设法做到了,但每个连接只工作一次。我的意思是,在我拥有的向量 (session1,session2,session3) 中,当我调用 session1.connect() 和 session1.disconnect() 时,我不再能够再次调用 session1.connect(),因为它显然会尝试连接到服务器,然后我得到:

检索存储在 Vector 中的会话我打开一个会话并获取:

INFO: Connecting to sftp.myserver.com port 2122
INFO: Connection established
INFO: Remote version string: SSH-2.0-OpenSSH_4.7
INFO: Local version string: SSH-2.0-JSCH-0.1.48
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: arcfour256 is not available.
INFO: CheckKexes: diffie-hellman-group14-sha1
INFO: diffie-hellman-group14-sha1 is not available.
INFO: SSH_MSG_KEXINIT sent
INFO: SSH_MSG_KEXINIT received
INFO: kex: server: diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-  sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
INFO: kex: server: ssh-rsa,ssh-dss
INFO: kex: server: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour128,arcfour256,arcfour,aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se,aes128-ctr,aes192-ctr,aes256-ctr
INFO: kex: server: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour128,arcfour256,arcfour,aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se,aes128-ctr,aes192-ctr,aes256-ctr
INFO: kex: server: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
INFO: kex: server: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
INFO: kex: server: none,zlib@openssh.com
INFO: kex: server: none,zlib@openssh.com
INFO: kex: server: 
INFO: kex: server: 
INFO: kex: client: diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1
INFO: kex: client: ssh-rsa,ssh-dss
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-sha1-96,hmac-md5-96
INFO: kex: client: hmac-md5,hmac-sha1,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-ctr hmac-md5 none
INFO: kex: client->server aes128-ctr hmac-md5 none
INFO: SSH_MSG_KEXDH_INIT sent
INFO: expecting SSH_MSG_KEXDH_REPLY
INFO: ssh_rsa_verify: signature true
WARN: Permanently added 'sftp.myserver.com' (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: password
INFO: Next authentication method: password
INFO: Authentication succeeded (password).
INFO: Disconnecting from sftp.myserver.com port 2122

一切正常,最后我断开了 session1。

然后当我再次尝试使用 session1 时,我得到以下异常...

INFO: Connecting to sftp.myserver.com port 2122
INFO: Connection established
INFO: Remote version string: SSH-2.0-OpenSSH_4.7
INFO: Local version string: SSH-2.0-JSCH-0.1.48
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: arcfour256 is not available.
INFO: CheckKexes: diffie-hellman-group14-sha1
INFO: diffie-hellman-group14-sha1 is not available.
INFO: SSH_MSG_KEXINIT sent
INFO: Disconnecting from sftp.myserver.com port 2122
com.jcraft.jsch.JSchException: Packet corrupt
at com.jcraft.jsch.Session.start_discard(Session.java:994)
at com.jcraft.jsch.Session.read(Session.java)
at com.jcraft.jsch.Session.connect(Session.java:288)
at com.jcraft.jsch.Session.connect(Session.java:162)
at sftp.make_dir(SFTP:118)

我在尝试重用 java 对象 (Session) 的方式上做错了吗?

非常感谢您的帮助

卢卡

【问题讨论】:

    标签: ssh jsch


    【解决方案1】:
    com.jcraft.jsch.JSchException: Packet corrupt
    

    为了建立连接,内部发生了很多事情。每次创建会话时,都会与该会话关联一个随机数(称为数据包)。并且这个会话存储在一个 JSCH 会话池中。

    当会话断开连接时,会话从池中删除,数据包无效。还有很多其他的事情发生了,但是对于上面提到的错误消息来说,这两件事最重要。

    现在,当您尝试使用已经断开的会话进行连接时,它找不到数据包并抛出此错误。

    【讨论】:

    • 感谢@tushmish 的解释!很抱歉延迟将此标记为解决方案。当时我的“解决方法”是创建一个存储在 ArrayList 中的自定义 Sessions 对象池,然后处理它们。我知道不优雅,但为我必须做的事情工作。
    猜你喜欢
    • 2014-10-20
    • 2012-05-20
    • 2021-08-23
    • 2017-12-18
    • 2016-05-02
    • 2013-04-14
    • 2012-04-02
    • 2017-10-19
    • 2014-11-19
    相关资源
    最近更新 更多