SshClient client1 = SshClient.setUpDefaultClient();
client1.setAgentFactory(localAgentFactory);
client1.start();
ClientSession session1 = client1.connect("localhost", port1).await().getSession();
assertTrue(session1.authAgent("smx").await().isSuccess());
ChannelShell channel1 = session1.createShellChannel();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
channel1.setOut(out);
channel1.setErr(err);
channel1.setAgentForwarding(true);
channel1.open().await();
OutputStream pipedIn = channel1.getInvertedIn();
synchronized (shellFactory.shell) {
System.out.println("Possibly waiting for remote shell to start");
if (!shellFactory.shell.started) {
shellFactory.shell.wait();
}
}
SshClient client2 = SshClient.setUpDefaultClient();
client2.setAgentFactory(agentFactory);
client2.getProperties().putAll(shellFactory.shell.getEnvironment().getEnv());
client2.start();
ClientSession session2 = client2.connect("localhost", port2).await().getSession();
assertTrue(session2.authAgent("smx").await().isSuccess());
ChannelShell channel2 = session2.createShellChannel();
channel2.setIn(shellFactory.shell.getIn());
channel2.setOut(shellFactory.shell.getOut());
channel2.setErr(shellFactory.shell.getErr());
channel2.setAgentForwarding(true);
channel2.open().await();
pipedIn.write("foo\n".getBytes());
pipedIn.flush();
Thread.sleep(1000);