Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jsch How to reuse a session

Tags:

ssh

jsch

I am creating many different (Session) objects passing the login credentials to a java class from an Oracle pl/sql package. I then store this (Session) objects in a Vector. The idea is to connect, open the required channels, close channels and finally disconnect a particular session from this Vector. I managed to do it but it works just once for each connection. I mean, in the Vector I have (session1,session2,session3), when I call session1.connect() and then session1.disconnect(), I am no more able to call session1.connect() again, since apparently it tries to connect to the server but then I get:

Retrieving the session stored in the Vector I open a session and get:

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,[email protected],aes128-ctr,aes192-ctr,aes256-ctr
INFO: kex: server: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour128,arcfour256,arcfour,aes192-cbc,aes256-cbc,[email protected],aes128-ctr,aes192-ctr,aes256-ctr
INFO: kex: server: hmac-md5,hmac-sha1,[email protected],hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
INFO: kex: server: hmac-md5,hmac-sha1,[email protected],hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
INFO: kex: server: none,[email protected]
INFO: kex: server: none,[email protected]
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

Everything worked fine and at the end I disconnected session1.

Then when I try to use session1 again I get the following exception...

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)

Am I doing something wrong in the way I try to reuse the java object (Session)?

Thanks very much for your help

Luca

like image 697
Luca Avatar asked Oct 11 '12 10:10

Luca


1 Answers

com.jcraft.jsch.JSchException: Packet corrupt

A lot of things happen internally to establish a connection. Everytime a session is created, a random number (called packet) is associated with that session. And this session is stored in a JSCH session pool.

When the session is disconnected, the session is removed from the pool and the packet is made void. there are lot of other things that happen but these 2 matter most for the error message sated above.

Now when you try to connect using the session that has already been disconnected, it does not find the packet and throws this error.

like image 176
Tushar Mishra Avatar answered Sep 20 '22 13:09

Tushar Mishra