Remote Connectivity
Æsh Readline supports remote connections via SSH, Telnet, and WebSockets.
SSH Connectivity
Adding SSH Dependency
<dependency>
<groupId>org.aesh</groupId>
<artifactId>terminal-ssh</artifactId>
<version>3.5</version>
</dependency>SSH Server Example
import org.aesh.terminal.ssh.SshTerminal;
SshTerminal ssh = SshTerminal.builder()
.host("0.0.0.0")
.port(2222)
.keyPair(new File("hostkey.ser"))
.connectionHandler(connection -> {
Readline readline = ReadlineBuilder.builder().build();
read(connection, readline, "[ssh]$ ");
})
.build();
ssh.start();Telnet Connectivity
Adding Telnet Dependency
<dependency>
<groupId>org.aesh</groupId>
<artifactId>terminal-telnet</artifactId>
<version>3.5</version>
</dependency>Telnet Server Example
import org.aesh.terminal.telnet.TelnetTerminal;
TelnetTerminal telnet = TelnetTerminal.builder()
.host("0.0.0.0")
.port(2323)
.connectionHandler(connection -> {
Readline readline = ReadlineBuilder.builder().build();
read(connection, readline, "[telnet]$ ");
})
.build();
telnet.start();HTTP/WebSocket Connectivity
Adding HTTP Dependency
<dependency>
<groupId>org.aesh</groupId>
<artifactId>terminal-http</artifactId>
<version>3.5</version>
</dependency>WebSocket Terminal Example
import org.aesh.terminal.http.WebSocketTerminal;
WebSocketTerminal ws = WebSocketTerminal.builder()
.host("0.0.0.0")
.port(8080)
.path("/terminal")
.connectionHandler(connection -> {
Readline readline = ReadlineBuilder.builder().build();
read(connection, readline, "[web]$ ");
})
.build();
ws.start();Common Connection Pattern
All remote terminals follow a similar pattern:
import org.aesh.readline.Readline;
import org.aesh.readline.ReadlineBuilder;
private static void read(Connection connection, Readline readline, String prompt) {
readline.readline(connection, prompt, input -> {
if (input != null && input.equals("exit")) {
connection.write("Goodbye!\n");
connection.close();
}
else {
connection.write("Echo: " + input + "\n");
read(connection, readline, prompt);
}
});
}Host Keys (SSH)
SSH requires host key for secure connections:
import org.apache.sshd.common.keyprovider.KeyPairProvider;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
KeyPairProvider keyPairProvider = new SimpleGeneratorHostKeyProvider(
new File("hostkey.ser").toPath()
);
SshTerminal ssh = SshTerminal.builder()
.keyPair(keyPairProvider)
// ...
.build();Authentication (SSH)
Configure SSH authentication:
import org.apache.sshd.server.auth.password.PasswordAuthenticator;
import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;
PasswordAuthenticator passwordAuth = (username, password, session) -> {
return authenticate(username, password);
};
PublickeyAuthenticator publicKeyAuth = (username, key, session) -> {
return authenticatePublicKey(username, key);
};
SshTerminal ssh = SshTerminal.builder()
.passwordAuthenticator(passwordAuth)
.publickeyAuthenticator(publicKeyAuth)
// ...
.build();Starting/Stopping Servers
// Start server
ssh.start();
telnet.start();
ws.start();
// Stop server
ssh.stop();
telnet.stop();
ws.stop();
// Check if running
boolean isRunning = ssh.isRunning();Multiple Connection Types
Run multiple connection types simultaneously:
SshTerminal ssh = SshTerminal.builder()...build();
TelnetTerminal telnet = TelnetTerminal.builder()...build();
ssh.start();
telnet.start();
// Both run concurrentlyWorking Examples
The aesh-examples repository contains complete working examples for all connectivity types:
- shell-ssh - Shell over SSH with authentication
- shell-telnet - Shell over Telnet
- shell-websocket - Web-based terminal over WebSocket
- cmd-mirror-ssh - Command mirroring over SSH
See the Examples and Tutorials page for detailed information about all available examples, including setup instructions and complete source code.