JSCH会话连接问题

时间:2017-07-13 18:33:49

标签: java ssh jsch

我运行下面的程序并重新启动主机,这需要大约6到8分钟来完成重启,但我从来没有得到超时...即它永远不会去其他部分。

    public static void main(String a[]) throws JSchException, InterruptedException {
        java.util.Properties config = new java.util.Properties();
        JSch jsch = new JSch();
        String user = "cli";
        String host = "135.104.217.114";
        String password = "cli";
        int port = 22;
        Session session = jsch.getSession(user,host,port);
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.setPassword(password);
        session.connect();
        while(true)
        {
            if(session.isConnected())
            {
                System.out.println("session connected");
            }
            else
            {
                System.out.println("sesion not connected--------------");
            }
            Thread.sleep(1000);
        }
    }

1 个答案:

答案 0 :(得分:1)

您很可能需要配置"Sever Alive" properties

session.setServerAliveInterval(5000); // Check if server is alive every 5 seconds
session.setServerAliveCountMax(5);  // Disconnect after 5 Server Alive checks did not receive a response.