Discussion:
[JSch-users] Setting channel out/err stream to System.out/err
Tobia Conforto
2016-05-11 16:51:01 UTC
Permalink
Hi

I've seen the following code mentioned in several examples:

channel.setOutputStream(System.out);

First of all, I think the various examples should have the second parameter "dontclose" set to true, otherwise JSch will close System.out/err and the Java program will terminate!

But more importantly, I cannot get it to work. Basically I'm doing this:

ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand("./test.sh");
channel.setInputStream(null);
channel.setOutputStream(System.out, true);
channel.setErrStream(System.err, true);
channel.connect();
while (!channel.isClosed()) {
Thread.sleep(100);
}
channel.disconnect();

The "test.sh" remote script prints something to both streams and sleeps for a few seconds:

#!/bin/sh
echo Hello
echo Hello err >&2
sleep 10

I can confirm that the loop waits for the remote script to end (if I set the remote script to sleep for 10 sec, the loop waits for 10 sec, etc.) but I don't get anything printed on the local terminal, neither stdout nor stderr.

I'm using Java 1.7.0_101 on Linux 64bit and JSch 0.1.53, but I have also tried previous JSch versions, with the same results.

What am I doing wrong?

-Tobia

PS: I have posted the same question here with a longer example: http://stackoverflow.com/questions/37167331/jsch-output-to-terminal
Tobia Conforto
2016-05-11 17:55:24 UTC
Permalink
And of course the problem was on my side!
I had "hardened" the SSH account and that prevented me from running the actual script.
It all works as expected.
Sorry for the noise and thanks for a great library.

-Tobia

Loading...