1) I used the code from the tutorial to read the server output (java inputstream). I did this in the single command mode, not a pseudo-terminal.
2) I found that a high rate of the commands that I was executing appeared to be failing in an unknown manner when examining the client-side output.
3) I wrote a simple loop to essentially "echo i > /test/i && cat i;" where i was my counter from 1 to 1000. I found that a relatively high percentage (>20%) of the time the commands were executed, the output would be empty.
4) I was able to verify that on the server side, all commands were executing successfully, as I had 1000 files containing the proper contents.
5) The *really* funky thing happened at this point. I went from my Linux box (using the official sun jdk) and ran it on a Windows XP Pro box. 100% of the commands I ran successfully produced output on the client side.
6) I executed a lot of commands in the form "command1 && command 2; echo Exit $?" and found that my output fell into a few categories:
a) blank output
b) output for command 1
c) output for command 1 and command 2
d) output for all commands invoked
Most frequently results were a) and d)
Ugly Quick Fix & Theory:
Once the session channel's state is closed, one can no longer read the associated InputStream. This was verified by waiting for the channel to close and then attempting to process the output, which failed 100% of the time.
Adding "; sleep .250" to the end of each command before it is executed resulted in me getting 100% accurate output, "; sleep .100" led to success > 95%, and as I decreased the length of the sleep command, my failure rate increased.
It is my hypothesis that the session channel was getting closed before the full output had been read from the streams. Adding an extra command which produced no output, but increased the time between my last real command and the closing of the session channel is what gave adequate time to finish processing the stream before being severed.
The following is a snippet of what I am now using:
SessionChannelClient session = null;
InputStream in = null;
String line = "";
String output = "";
// The connection is authenticated we can now do some real work!
session = ssh.openSessionChannel();
in = session.getInputStream();
// Pause for a fraction of a second to prevent connection from
// being closed prematurely, before we've finished reading out
command = command + "; sleep .250;";
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuffer buffer = new StringBuffer();
while ((line = br.readLine()) != null || session.getState().equals(ChannelState.CHANNEL_OPEN))
buffer.append(line + "\n");
output = buffer.toString();