Many of our users want to connect the terminal to an SSH session using our Maverick Legacy Client API. Please read the Creating a Connection article for detailed information about setting up an SSH client. We will assume here you have a SshClient instance that is connected and authenticated and ready to start a session.
There are of course other SSH APIs and there is no restriction here as you are simply plugging in a set of I/O streams to the terminal. However, we will focus on our Maverick Legacy API for the remainder of this document.
Before connecting the streams you need to start the shell or execute the command you want to interact with. First, we open a session channel on the SshClient instance.
SshSession session = ssh.openSessionChannel();
We then need to set up the pseudo-terminal on the remote host. We will use xterm and we get the number of columns and rows from the Terminal object. The final parameters are width and height which do not apply in this case so we pass a value of zero.
session.requestPseudoTerminal("xterm", buffer.getColumns(), buffer.getRows(), 0, 0);
If you make your UI component resizable then you will want to add a listener to the Terminal to capture changes in terminal dimensions. We need to send these changes back to the SSH server.
buffer.addListener(new TerminalListenerAdapter() { public void screenResize(Terminal terminal, int cols, int rows, boolean remote) { if(!remote) { try { session.changeTerminalDimensions(cols, rows, 0, 0); } catch (SshException e) { } } } });
Then we clear the screen for good measure and start the remote shell.
buffer.clearScreen(); session.startShell();
Now as we demonstrated above, we connect the streams.
Thread t2= new Thread() { public void run() { try { IOUtil.copy(new TerminalInputStream(buffer), session.getOutputStream()); } catch (Exception e) { } } }; t2.start(); IOUtil.copy(session.getInputStream(), new TerminalOutputStream(buffer));
The image shows a Swing version of the example code outlined in this article.