【问题标题】:Ganymed-ssh - how to set timeout to close session when execCommand takes more timeGanymed-ssh - 如何在 execCommand 需要更多时间时设置超时以关闭会话
【发布时间】:2014-03-14 18:38:10
【问题描述】:

我正在使用 Ganymed-ssh 远程提取日志详细信息。下面是我的代码。有时 stdout 返回的数据很大,因此返回需要更多时间。如果标准输出在 1 分钟内没有返回,我想关闭会话。如何设置会话超时自动关闭标准输出超过 1 分钟?

Connection conn = new Connection(hostname);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated == false)
   throw new IOException("Authentication failed.");
Session session = conn.openSession();
session.execCommand("grep traceid trace.log");
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
     {
         String line = br.readLine();
         if (line == null)
              break;
         System.out.println(line);
      }
System.out.println("ExitCode: " + session.getExitStatus());
session.close();
conn.close();

【问题讨论】:

    标签: java session ssh


    【解决方案1】:

    您可以使用waitForCondition 方法 或waitUntilDataAvailable Session 类的方法。 两者都需要超时参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-10
      • 2021-12-28
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 2014-03-25
      相关资源
      最近更新 更多