dhinu
dhinu

Reputation: 135

Ganymed-ssh - how to set timeout to close session when execCommand takes more time

I'm using Ganymed-ssh to pull log details remotely. Below is my code. Sometimes the data returned by stdout is huge and so it takes more time to return. I want to close the session if the stdout did not return in 1 minute. How to set timeout for session to close automatically is stdout took more than 1 minute ?

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();

Upvotes: 1

Views: 890

Answers (1)

choo.s
choo.s

Reputation: 11

You can use waitForCondition method or waitUntilDataAvailable method of Session Class. Both needs timeout parameter.

Upvotes: 1

Related Questions