JSch:如何保持会话活动并持续运行

20

我正在使用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,但是它也不起作用。

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

有什么想法可以做到我上面说的?

3个回答

24
尝试了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 守护进程。 - GWu
谢谢。添加了testChannel.exit() - blafasel
当我打开多个通道时,出现了死锁问题。将 getSession() 方法设置为 synchronized 可以解决这个问题,但会影响一些性能。你知道哪部分需要进行同步吗?也许是测试通道的连接与断开? - Sridhar Sarnobat
ChannelExec在0.1.54和0.1.55版本中没有.exit方法。 - svarog
我正在使用Guava Cache中的localCache,但我不知道为什么目前一个打开的通道不够。有什么建议或评论吗?该通道在需要时保持活动状态,或在最后一次访问后15分钟内关闭。我喜欢你实现的异常重试,我的项目中也有Resilient4j。我也会尝试一下... - Breton F.

7

0

您可以使用连接池,我使用了org.apache.commons.pool2,因此池负责提供已连接的会话,基本上:

  • 在makeObject上创建JSCH会话
  • 如果断开连接,则在activate上连接
  • 在validate上检查isConnected
  • 在passivate上发送KeepAliveMsg

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接