0

Currently I am executing command over ssh using:

val sshCmd = session.exec(command)
println(IOUtils.readFully(sshCmd.inputStream).toString())
sshCmd.join()

However, to see the output I need to wait until the command is finished.

How can I get "live" response?

I guess I can read the input stream until end of the line occurs and then print the line; however, is there already some method in the library that can help me with this?

1 Answer 1

3

It blocks and waits for the whole thing because that's what IOUtils.readFully is meant to do, it reads fully. Instead, to read line-by-line, you can do something as simple as:

 try (BufferedReader reader = new BufferedReader(new InputStreamReader(sshCmd.inputStream))) {
     String line;
     while ((line = reader.readLine()) != null) {
         System.out.println(line);
     }
 } catch (IOException e) {
     System.out.println(e);
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.