Java – jsch: how to keep a conversation active

I am using SSH to write a Java GUI program for static route management My code is as follows:

import com.jcraft.jsch.*;
import java.io.*;

public class Konsep {
    String status;
    static String username;
    static String hostname;
    String inputcommand;
    String output;
    static Session session;

    JSch jsch = new JSch();

    public String status(String stringstatus) {
        stringstatus = status;
        return stringstatus;
    }

    public String InputCommand(String inputcommandstatus) {
        inputcommandstatus = inputcommand;
        return inputcommandstatus;
    }

    public void connect(String usernamelokal,String hostnamelokal,String password,int port) {
        //        JSch jsch=new JSch();
        try {
            Session sessionlokal = jsch.getSession(usernamelokal,hostnamelokal,port);
            sessionlokal.setPassword(password);
            UserInfo ui = new UserInfoku.Infoku();
            sessionlokal.setUserInfo(ui);
            sessionlokal.setTimeout(0);
            sessionlokal.connect();
            status = "tersambung \n";
            username = usernamelokal;
            hostname = hostnamelokal;
            session = sessionlokal;
            System.out.println(username + " " + hostname);
        } catch (Exception e) {
            System.out.println(e);
            status = "Exception = \n " + e + "\n";

        }
    }

    public void disconnect() {
        //        JSch jsch=new JSch();
        try {
            Session sessionlokal = jsch.getSession(username,hostname);
            //            System.out.println(username +" "+ hostname);
            sessionlokal.disconnect();
            status = "wes pedhoott \n";
        } catch (Exception e) {
            System.out.println(e);
            status = "Exception = \n " + e + "\n";
        }

    }

    public void addRoute() {
        //        JSch jsch=new JSch();
        System.out.println(username + " " + hostname);
        try {
            Session sessionlokal = session; // =jsch.getSession(username,hostname);
            Channel channel = sessionlokal.openChannel("exec");
            ((ChannelExec) channel).setCommand(inputcommand);
            channel.setInputStream(null);
            channel.connect();
            ((ChannelExec) channel).setErrStream(System.err);
            InputStream in = channel.getInputStream();
            channel.connect();

            byte[] tmp = new byte[1024];
            while (true) {
                while (in.available() > 0) {
                    int i = in.read(tmp,1024);
                    if (i < 0)
                        break;
                    System.out.print(new String(tmp,i));
                }
                if (channel.isClosed()) {
                    System.out.println("exit-status: "
                            + channel.getExitStatus());
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (Exception ee) {
                }
            }

            channel.disconnect();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

The problem is when I call the connect method and then call addroute, the program returns.

root 192.168.50.2
root 192.168.50.2
com.jcraft.jsch.JSchException: session is down

I've been trying to get session status

Session sessionlokal=session; //returns com.jcraft.jsch.JSchException: ChannelExec

or

Session sessionlokal=jsch.getSession(username,hostname); //returns session is down

I also try to use keepalive, but it doesn't work

My goal is to create a session to host (log in), leave the session at the same time, execute a command or command, and possibly execute other commands later, and then close the session (log out) when it is not needed I have been searching on this forum. I found this question, but the code is to create a method to define a command to be executed first, then create a session, call the command method and close the session

Any ideas about the above?

Solution

Try session After sendkeepalivemsg() failed, I came to the following solution, which seems quite stable:

private Session getSession() throws Exception {
    try {
        if (!session.isConnected()) {
            logger.info("Session nicht konnektiert. Versuche (erneut) zu konnektieren.");
            session.connect();
        }
    } catch (Throwable t) {
        logger.info("Session kaputt. Baue neue.");
        session = jsch.getSession(user,host,port);
        session.setConfig(config);
        session.connect();
    }
    return session;
}

Update: a few days later, it failed

I tried to test it by killing the open session on the server All versions I have tested before show exactly the same behavior. No matter after waiting for a few days or killing the server process, the problem occurs, so I think this test - and the results of the above solutions are meaningful Unfortunately, isn't it

I'll try some other ways to solve it and keep you up to date

Update 2: final solution, camouflage and work:

private Session getSession() throws Exception {
    try {
        ChannelExec testChannel = (ChannelExec) session.openChannel("exec");
        testChannel.setCommand("true");
        testChannel.connect();
        if(logger.isDebugEnabled()) {
            logger.debug("Session erfolgreich getestet,verwende sie erneut");
        }
        testChannel.exit();
    } catch (Throwable t) {
        logger.info("Session kaputt. Baue neue.");
        session = jsch.getSession(user,port);
        session.setConfig(config);
        session.connect();
    }
    return session;
}

This version runs in a production environment for several weeks Once a day, I recorded messages

The cost of opening channels and executing some unattended commands is a bit annoying, but I find no other way to determine the status of the session with certainty

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>