ChannelShell channel = session.createShellChannel();
ByteArrayOutputStream sent = new ByteArrayOutputStream();
PipedOutputStream pipedIn = new PipedOutputStream();
channel.setIn(new PipedInputStream(pipedIn));
OutputStream teeOut = new TeeOutputStream(sent, pipedIn);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
channel.setOut(out);
channel.setErr(err);
channel.open();
teeOut.write("this is my command\n".getBytes());
teeOut.flush();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100; i++) {
sb.append("0123456789");
}
sb.append("\n");
final AtomicInteger exchanges = new AtomicInteger();
session.addListener(new SessionListener() {
public void sessionCreated(Session session) {
}
public void sessionEvent(Session sesssion, Event event) {
if (event == Event.KeyEstablished) {
exchanges.incrementAndGet();
}
}
public void sessionClosed(Session session) {
}
});
for (int i = 0; i < 100; i++) {
teeOut.write(sb.toString().getBytes());
teeOut.flush();
}
teeOut.write("exit\n".getBytes());
teeOut.flush();
channel.waitFor(ClientChannel.CLOSED, 0);
channel.close(false);
client.stop();