JSch:如何保持会话活跃 [英] JSch: How to keep the session alive and up

查看:36
本文介绍了JSch:如何保持会话活跃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 Java GUI 程序,用于使用 SSH 进行静态路由管理.我的代码如下:

I'm writing Java GUI program for static route management using SSH. 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 
";
            username = usernamelokal;
            hostname = hostnamelokal;
            session = sessionlokal;
            System.out.println(username + " " + hostname);
        } catch (Exception e) {
            System.out.println(e);
            status = "Exception = 
 " + e + "
";

        }
    }

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

    }

    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, 0, 1024);
                    if (i < 0)
                        break;
                    System.out.print(new String(tmp, 0, 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);
        }
    }
}      

问题是当我调用connect方法然后调用addroute时,程序返回

The problem is when I call the connect method and then calling the 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 with either

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

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

我也尝试过使用 keepalive,但它也不起作用.

I've also tried to use keepalive, but its not working either.

我的目的是创建一个会话来主持(登录),同时让会话保持运行,执行一个或多个命令,然后可能会执行其他命令,然后在不需要时关闭会话(注销).我一直在这个论坛上搜索,我发现这个问题但代码是创建的先定义要执行的命令的方法,然后创建会话,调用命令的方法并关闭会话.

My intention is to create a session to host (log in), while leaving the session up, execute a command or commands and maybe executing other commands later, and then closing the session when its not needed (log out). I've been searching on this forum and I found this question but the code is create a method to define a command to execute first, and then creating the session, call the command's method and close the session.

关于如何做我上面提到的任何想法?

Any ideas about how to do as I mentioned above ?

推荐答案

在尝试 Session.sendKeepAliveMsg() 没有成功后,我得出了以下似乎相当稳定的解决方案:

After trying Session.sendKeepAliveMsg() without success, I came to the following solution which seems to be rather stable:

private Session getSession() throws Exception {
    try {
        if (!session.isConnected()) {
            logger.info("Session successfully tested, use it again.");
            session.connect();
        }
    } catch (Throwable t) {
        logger.info("Session terminated. Create a new one.");
        session = jsch.getSession(user, host, port);
        session.setConfig(config);
        session.connect();
    }
    return session;
}

更新:几天后它失败了.

我试图通过杀死服务器上的打开会话来测试它.我以这种方式测试的所有先前版本都显示出完全相同的行为,无论问题是在等待几天后还是终止服务器进程后出现,所以我认为这个测试 - 以及上述解决方案的结果 - 是有意义的.不幸的是,它不是.

I tried to test it by killing the open session on the server. All prior versions I tested this way showed the exact same behavior, regardless whether the problem popped up after waiting some days or killing the server process, so I thought this test - and its outcome for the above solution - to be meaningful. Unfortunately, it isn't.

我将尝试其他一些方法来修复它并让您了解最新情况.

I'm going to try some other ways to fix it and keep you up to date.

更新 2:最终解决方案,保证不优雅且有效:

private Session getSession() throws Exception {
    try {
        ChannelExec testChannel = (ChannelExec) session.openChannel("exec");
        testChannel.setCommand("true");
        testChannel.connect();
        if(logger.isDebugEnabled()) {
            logger.debug("Session successfully tested, use it again.");
        }
        testChannel.exit();
    } catch (Throwable t) {
        logger.info("Session terminated. Create a new one.");
        session = jsch.getSession(user, host, port);
        session.setConfig(config);
        session.connect();
    }
    return session;
}

此版本在生产环境中运行数周.我每天记录一次信息消息.

This version runs several weeks in a productive environment. Once a day I have the info message logged.

打开频道和执行一些无所事事的命令的成本有点烦人,但我发现没有其他方法可以确定会话的状态.

The costs of opening a channel and performing some do-nothing-command are somewhat annoying, but I found no other way to be definitely sure about the state of the session.

这篇关于JSch:如何保持会话活跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆