Im trying to SSH from Java to a server and run multiple commands. the commands seem to work ok but I am not getting the results back in Java that I wanted.
Here is my code snippet
final Command cmd = session.exec("cd /app/t/backo/bin && ls -all && pwd && . setDBLApp && sudo -E -u deploy ./runDBLApp && sudo -E -u deploy ./nightlyDBL -type host ");
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join(30, TimeUnit.SECONDS);
System.out.println("\n** exit status: " + cmd.getExitStatus());
Here is what I see for output. I think its something to do with LocalWindow sizing but I cannot figure out how to fix it. This only happens (or seems to) when I use ssh commands that would have lots of screen output
This is a small snippet I get 100 or so lines of this
14:56:48.957 [reader] INFO n.s.s.c.c.direct.SessionChannel - Received window adjustment for 2097152 bytes
14:56:48.957 [reader] DEBUG n.s.s.c.channel.Window$Remote - Increasing by 2097152 up to 2097152
14:56:48.957 [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<chan#0 / chanreq for exec>> to `SOME`
14:56:48.963 [reader] DEBUG n.s.s.c.channel.Window$Local - Consuming by 4096 down to 2093056
14:56:48.964 [reader] DEBUG n.s.s.c.channel.Window$Local - Consuming by 1002 down to 2092054
14:56:48.964 [reader] DEBUG n.s.s.c.channel.Window$Local - Consuming by 23 down to 2092031
14:56:49.090 [reader] DEBUG n.s.s.c.channel.Window$Local - Consuming by 141 down to 2091890
14:56:49.120 [reader] DEBUG n.s.s.c.channel.Window$Local - Consuming by 267 down to 2091623
14:56:49.230 [reader] DEBUG n.s.s.c.channel.Window$Local - Consuming by 309 down to 2091314
14:56:49.531 [reader] DEBUG n.s.s.c.channel.Window$Local - Consuming by 308 down to 2091006
14:56:49.532 [reader] DEBUG n.s.s.c.channel.Window$Local - Consuming by 306 down to 2090700
What I want to see is the screen output from the ssh commands which might be verbose
You are right in thinking this probably has to do with window sizing. The local window sshj keeps for output coming from the server could have filled up. The default rule is for it to be expanded when you create space in the buffer by reading from the stdout/stderr streams provided by the API i.e. channel.getInputStream()
/channel.getErrorStream()
You have two options:
Keep draining the stdout and stderr streams while the command executes. Based on your snippet you actually are reading from stdout but not stderr. You could multiplex the reads from the 2 streams using the available()
method on java.io.InputStream
, which is implemented.
Set sessionChannel.setAutoExpand(true)
but note that this means, no matter how much output is produced the local window will keep expanding even if you are not draining the stdout/stderr streams by reading from them. So this has the potential to cause high memory consumption.