>> How or can I hold the channel open ?

Channels are auto-closeable - i.e., try-with-resource can be used. In other
words, it won't close on your end unless you close it. However:

>> It seems that once I create and open and actually use the channel, it is
being closed without my knowledge.

You can only be responsible for your end - there is nothing you  can do if
the peer decides to close it.

That being said, there is something many users forget - to consume the
input/output streams. Here is the right way to do this:

// Done once during 'main' start
SshClient client =  SshClient.setupDefaultClient();
...extra configurations...
client.start();

...while program is running...

try (ClientSession session =
client.connect(...).verify(...timeout...).getSession()) {

try (ChannelShell shell = session.createShellChannel(...)) {

shell.setIn(...);
shell.setOut(...);
shell.setErr(...);
// spawn the  threads that will read/write STDIN/STDOUT/STDERR
try {

shell.open().verify(...timeout...);
// This is the part that many miss and so channel.close() is called because
try-with-resource block exits
shell.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), 0L /* forever */);

} finally {

....kill the spawned threads...

}

}

}

... on exit...
client.stop();

Reply via email to