Jsch如何重复使用会话

6
我正在创建许多不同的(会话)对象,并从Oracle pl/sql程序包向java类传递登录凭据。然后我将这些(会话)对象存储在一个向量中。该想法是连接,打开所需通道,关闭通道,最后从该向量中断开特定会话。我设法做到了,但每个连接只能工作一次。我的意思是,在向量中,我有(session1,session2,session3),当我调用session1.connect()然后session1.disconnect()时,我不能再次调用session1.connect() ,因为显然它尝试连接到服务器,但是我收到以下错误消息:
检索存储在向量中的会话后,我打开了一个会话并获得:
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)的方式上是否有做错的地方?

非常感谢您的帮助。

Luca

1个回答

8
com.jcraft.jsch.JSchException: Packet corrupt

每次建立连接时,会发生很多内部操作。每当创建一个会话时,都会将一个随机数(称为数据包)与该会话相关联。并且这个会话存储在JSCH会话池中。
当会话断开连接时,该会话将从池中移除,并使数据包无效。还有很多其他事情发生,但对于上述错误消息来说,这两个最重要。
现在,当您尝试使用已经断开连接的会话进行连接时,它找不到数据包并抛出此错误。

感谢@tushmish的解释!很抱歉延迟了将其标记为解决方案。当时我的“解决方法”是创建一个自定义的Sessions对象池,存储在ArrayList中,然后负责处理它们的释放。虽然不太优雅,但对于我所要做的事情来说是有效的。 - Luca

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接