Java com.jcraft.jsch.JSch session not getting disconnect

146 Views Asked by At

I am using jcraft JSCH package to do ssh to a HP router and execute some CLI commands. After execution of commands I am calling session disconnect method. But this session disconnect method is getting hung. Not getting any idea why this disconnect is getting hung.

The session is getting disconnect once the idle timeout of router is reached. Here is my piece of code -

package HpRouter;
import java.lang.Thread;

import java.io.DataInputStream;
import java.io.DataOutputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;


public class HpRouter {

    String CIPHER_S2C_KEY_NAME = "cipher.s2c";
    String CIPHER_S2C_KEY_VALUE = "aes128-cbc,3des-cbc,blowfish-cbc";
    String CIPHER_C2S_KEY_NAME = "cipher.c2s";
    String CIPHER_C2S_KEY_VALUE = "aes128-cbc,3des-cbc,blowfish-cbc";
    String CHECK_CIPHERS_KEY_NAME = "CheckCiphers";
    String CHECK_CIPHERS_KEY_VALUE = "aes128-cbc";
    String BCFNNI_STRICCTHOSTKEYCHECKING = "StrictHostKeyChecking";
    String NO = "no";
    String OPEN_SHELL_CHANNEL = "shell";
    String USER_NAME = "username";
    String PCODE = "123456";
    String HOST_NAME = "1.1.1.1";
    String PORT = "22";
    
    
    public Session getChannelConnection() throws JSchException {
        String userName = USER_NAME;
        String password = PCODE;
        String hostName = HOST_NAME;
        int port = Integer.parseInt(PORT);

        JSch jsch = new JSch();
        Session session = jsch.getSession(userName, hostName, port);
        session.setPassword(password);

        session.setConfig(CIPHER_S2C_KEY_NAME, CIPHER_S2C_KEY_VALUE);
        session.setConfig(CIPHER_C2S_KEY_NAME, CIPHER_C2S_KEY_VALUE);
        session.setConfig(CHECK_CIPHERS_KEY_NAME, CHECK_CIPHERS_KEY_VALUE);

        java.util.Properties config = new java.util.Properties();
        config.put(BCFNNI_STRICCTHOSTKEYCHECKING, NO);
        session.setConfig(config);

        session.connect();

        return session;
    }
    
    public StringBuilder getChannelOutput(DataInputStream ins, Session session, Channel channel) throws Exception {

        String line = null;
        StringBuilder sb = new StringBuilder();
        byte[] tmpBuffer = new byte[1024];
        try {
            while (true) {
                while (ins.available() > 0) {
                    int tmp = ins.read(tmpBuffer, 0, 1024);

                    if (tmp < 0)
                        break;
                    line = new String(tmpBuffer, 0, tmp);
                    sb.append(line);
                }


                if (channel.isClosed()) {
                    break;
                }
            }

            if (session != null) {
                session.disconnect();
            }
            channel.disconnect();

        }
        catch (Exception e) {
            System.out.println("Exception while getting Channel Output - ");
            Thread.currentThread().interrupt();
            throw new Exception("Exception while getting Channel Output - ", e);    
        }
        return sb;
    }
    
    public static void main(String[] args) {
        
        HpRouter hpObj = new HpRouter();
        try {
            Session session = hpObj.getChannelConnection();
            Channel channel = session.openChannel("shell");
            channel.connect();
            
            DataOutputStream ops = new DataOutputStream(channel.getOutputStream());
            DataInputStream ins = new DataInputStream(channel.getInputStream());

            
            ops.writeBytes("quit" + "\r\n");
            ops.flush();
            
            StringBuilder sb = hpObj.getChannelOutput(ins, session, channel);
            
        } catch (Exception e) {
            
        }


    }

}

Thread dump -

"main" #1 prio=5 os_prio=0 cpu=1272.29ms elapsed=37.55s tid=0x00007feb6c410000 nid=0x20f504 runnable  [0x00007feb7a452000]
   java.lang.Thread.State: RUNNABLE
        at java.net.PlainSocketImpl.socketClose0([email protected]/Native Method)
        at java.net.AbstractPlainSocketImpl.socketPreClose([email protected]/AbstractPlainSocketImpl.java:722)
        at java.net.AbstractPlainSocketImpl.close([email protected]/AbstractPlainSocketImpl.java:601)
        - locked <0x000000043af0b0a8> (a java.lang.Object)
        at java.net.SocksSocketImpl.close([email protected]/SocksSocketImpl.java:1067)
        at java.net.Socket.close([email protected]/Socket.java:1513)
        - locked <0x000000043aef9190> (a java.lang.Object)
        - locked <0x000000043aef4b60> (a java.net.Socket)
        at java.net.SocketInputStream.close([email protected]/SocketInputStream.java:249)
        at com.jcraft.jsch.Session.disconnect(Session.java:1753)
        at HpRouter.HpRouter.getChannelOutput(HpRouter.java:76)
        at HpRouter.HpRouter.main(HpRouter.java:104)

   Locked ownable synchronizers:
        - None
0

There are 0 best solutions below