'Jsch java ssh client is not getting disconnected after command gets executed

I am using java ssh client (http://www.jcraft.com/jsch/) to connect to remote machine and execute the command. The code is working fine till i connect to remote machine and execute the command. however, the issue is , the channel and session are not getting disconnected even after command executed successfully.

I have called session.disconnect and channel.disconnect as well but still the issue.

Here is my code:

    JSch jsch = new JSch();
    String host = null;
    host = "192.168.102.211";
    String privateKey = "C:\\test\\key";
    String cmd = "a";
    String command = "b";

    jsch.addIdentity(privateKey);
    Session session = jsch.getSession("user1", host, 22);
    Channel channel = session.openChannel("shell");

    UserInfo ui = new MyUserInfo() {
        public boolean promptYesNo(String message) {
            return true;
        }
    };
    session.setUserInfo(ui);
    session.connect(30000);

    OutputStream ops = channel.getOutputStream();
    PrintStream ps = new PrintStream(ops, true);

    channel.connect();
    ps.println(cmd);
    ps.println(command);
    ps.close();

    InputStream in = channel.getInputStream();
    byte[] bt = new byte[1024];
        while (in.available() > 0) {
        //  int i = in.read(bt, 0, 1024);
            for (int i = in.read(); i >=0; i--)
                    {
            String str = new String(bt, 0, i);
            System.out.print(str);
                    }
            break;

    }

 if (channel != null) {
        channel.disconnect();
        session.disconnect();
        System.out.println(channel.isConnected());
    }
}

Please suggest



Solution 1:[1]

Update your question with a relevant code. just a hint, you should do something like.

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

Solution 2:[2]

I have similar error I change my code to that
first I have declare session and channel

static Session session = null;
static Channel channel = null;

And this the connation code

public static OutputStream puttyConnection(String user, String hostName, String password)
        throws JSchException, IOException {
    session = new JSch().getSession(user, hostName, 22);
    session.setPassword(password);
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect();
    channel = session.openChannel("shell");
    OutputStream ops = channel.getOutputStream();
    channel.connect();
    return ops;
}

and in the finish of my running code I use to disconnect like that

  if (channel != null) {
                channel.disconnect();
                session.disconnect();
                System.out.println(channel.isConnected());
        }

Solution 3:[3]

I encountered a similar problem, realizing that the channel could only be disconnected if it was closed. So I used the while loop to check if the channel is closed and then I was able to disconnect the channel without any problem.

       while (true){
            if(channel.isClosed()){
                channel.disconnect();
                break;
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lucas Sa
Solution 2 Vladi
Solution 3 Stone