Why does the SFTP connection still exist after closing the NK channel?

When the code below is complete, netstat -a|grep sftp shows an open SFTP connection. It also appears as an open connection in JProfiler.

channel.isConnected() in the finally block prints false. Any ideas why the connections are not closing as I am at a loss?

 public static void clean() { com.jcraft.jsch.ChannelSftp channel = null; try { channel = Helper.openNewTLSftpChannel(); channel.connect(); channel.cd(remoteFileDirectory); List<ChannelSftp.LsEntry> list = channel.ls("*." + fileType); for (ChannelSftp.LsEntry file : list) { String fileName = file.getFilename(); DateTime fileDate = new DateTime(parseDateFromFileName(fileName)); //if this file is older than the cutoff date, delete from the SFTP share if (fileDate.compareTo(cleanupCutoffdate) < 0) { channel.rm(fileName); } } } catch (Exception exception) { exception.printStackTrace(); } finally { if (channel != null) { channel.disconnect(); System.out.println(channel.isConnected()); } } } 

Adding openNewTLSftpChannel() below:

 public static ChannelSftp openNewSftpChannel(String privateKeyFileName, String password, String username, String host, int port) throws ConfigurationErrorException { JSch jsch = new JSch(); File sftpPrivateFile = new File(privateKeyFileName); Channel channel; try { if (!sftpPrivateFile.canRead()) { throw new ConfigurationErrorException("File access error: " + sftpPrivateFile.getAbsolutePath()); } jsch.addIdentity(sftpPrivateFile.getAbsolutePath(), password); Session session = jsch.getSession(username, host, port); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); channel = session.openChannel("sftp"); } catch (JSchException jschException) { throw new ConfigurationErrorException("File access error: " + sftpPrivateFile.getAbsolutePath()); } return (ChannelSftp) channel; } 
+6
source share
2 answers

If you look at the caller ID examples for SFTP, you will see how the session ends:

 //setup Session here ... session.connect(); ... Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; ...run sftp logic... //close sessions here sftpChannel.exit(); session.disconnect(); 

You will notice that there are two parts to connecting and disconnecting; Session object and Channel object.

In my code, I use the Session object to set my authentication information and the Channel object to execute the sftp commands that I need.

In your example, you create a Session object in your openNewSftpChannel method, but it never closes, so your session stays alive.

For further familiarization with examples.

+14
source

Robert H is correct, you need to exit your channel and disconnect the session. I wanted to add that the session exists even when the channel is closed. Since you are creating your session inside the try block inside the method, it seems that you lost the session, but you can return it using "getSession" on your sftpChannel channel.

You can modify your finally block as follows:

 } finally { if (channel != null) { Session session = channel.getSession(); channel.disconnect(); session.disconnect(); System.out.println(channel.isConnected()); } } 
+4
source

All Articles