【问题标题】:JSch: How to keep the session alive and upJSch:如何保持会话活跃和正常
【发布时间】:2013-04-14 04:18:48
【问题描述】:

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

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, 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,程序返回

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

我一直在尝试获取会话状态

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

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

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

我的意图是创建一个会话来托管(登录),同时让会话保持打开状态,执行一个或多个命令,并可能稍后执行其他命令,然后在不需要时关闭会话(注销)。 我一直在这个论坛上搜索,我找到了这个question,但代码是创建一个方法来定义一个命令首先执行,然后创建会话,调用命令的方法并关闭会话。

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

【问题讨论】:

    标签: java session ssh jsch keep-alive


    【解决方案1】:

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

    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;
    }
    

    更新:几天后它失败了。

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

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

    更新 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;
    }
    

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

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

    【讨论】:

    • 不错的解决方案,只有一个提示:我会在成功检查后添加testChannel.exit();,否则每次检查都会在服务器端多一个 sftp-server 守护进程
    • 谢谢。添加了testChannel.exit()
    • 我在打开多个频道时遇到了死锁问题。使 getSession() synchronized 解决了这个问题,但这有点影响性能。你知道哪个部分可能需要同步吗?也许测试通道连接 = 断开连接?
    • ChannelExec 没有 .exit 方法(版本 0.1.54 和 0.1.55)
    • 我正在使用来自番石榴缓存的 localCache,但我不知道为什么到目前为止打开 1 个频道还不够。建议还是 cmets ?频道根据需要处于活动状态或在最后一次访问后 15 分钟后关闭。我喜欢你实现的异常重试,我的项目中有弹性4j。我也试试看……
    【解决方案2】:

    你可以使用一个池,我使用 org.apache.commons.pool2 所以池负责提供连接的会话,基本上:

    • 在 makeObject 上创建 JSCH 会话
    • 如果断开连接则激活连接
    • 检查是否 isConnected 验证
    • 在钝化时发送KeepAliveMsg

    以上对我有用,还请记住,如果您在验证中添加逻辑,则 poolConfig 应在 testOnBorrow 或 testOnReturn 上设置为 true。 所以我使用 KeyedObjectPool 你应该通过 borrowObject 方法从池中获取连接,并确保将它们返回到通过 returnObject 完成操作后的池。

    https://commons.apache.org/proper/commons-pool/api-2.2/org/apache/commons/pool2/PooledObjectFactory.html

    【讨论】:

      【解决方案3】:

      您可以尝试在调用Session#connect()之前调用以下方法。

      Session#setServerAliveInterval(int milliseconds)

      虽然我发现了另外一个功能Session#sendKeepAliveMsg() 可以尝试。

      【讨论】:

        猜你喜欢
        • 2022-10-13
        • 1970-01-01
        • 2016-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多