Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.JSchException: Packet corrupt

Tags:

java

linux

ssh

jsch

I am using Jsch 0.1.51 on RHEl 6 with Jdk 1.7_51. While making session to a remote machine I am getting exception that is :

com.jcraft.jsch.JSchException: Packet corrupt
        at com.jcraft.jsch.Session.start_discard(Session.java:1049)
        at com.jcraft.jsch.Session.read(Session.java:919)
        at com.jcraft.jsch.UserAuthNone.start(UserAuthNone.java:56)
        at com.jcraft.jsch.Session.connect(Session.java:389)
        at com.jcraft.jsch.Session.connect(Session.java:183)
        at TestSFTP.checkException(TestSFTP.java:130)
        at TestSFTP.moveFileToDir(TestSFTP.java:78)
        at TestSFTP.main(TestSFTP.java:73)

Same code was working fine with RHEL 5. Can any body provide some suggestions .. Thanks

code used is :

Session   session     = null;
ChannelSftp channelSftp = null;
JSch jsch = new JSch();

session = jsch.getSession(this.sftpUser,this.sftpHost,this.sftpPort);

Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(this.sftpPassword);
session.connect();
channelSftp = (ChannelSftp)session.openChannel("sftp");
channelSftp.connect();

I am getting error at session.connect();

like image 714
Chandan Gupta Avatar asked Mar 17 '23 10:03

Chandan Gupta


1 Answers

This exception can occur if you are trying to connect on an already existent session. A work around is closing the session and then starting a new session. This helped me. Found some help from this site:

http://flyingjxswithjava.blogspot.com/2015/03/comjcraftjschjschexception-packet.html

Quoting essential points from the site to understand the problem:

  • This exception occurs when the Session is reused repeatedly in a loop where the session is disconnected intentionally or due to time out and needs to reconnect again.

  • The reason that such an exception is thrown is that the first time the Session is connected to the remote site, a random number called Packet is generated for the session.

  • When the thread is having its 1 hour sleep, the session gets automatically disconnected due to no activity for a certain period of time.

  • When the Session is disconnected, the Packet is lost.

  • When the Session is trying to reconnect, it could not find the Packet, thus the exception is thrown.

like image 76
Anu M Avatar answered Mar 25 '23 07:03

Anu M