Documentation
Open Source Release Policy
Export Compliance
Using BouncyCastle
Installing the API
Enabling Logging
Per-Connection Logging
Third Party Dependencies
Generating SSH Keys
Using BouncyCastle
Using SSH Public Keys in Java
Supporting ED25519/Curve25519
Supporting Compression
Integrating Licensing into Maven Builds
Creating an SSH Client
Public Key Authentication
Keyboard Interactive Authentication
Public Key Authentication with sshagent
Executing Single Commands
Executing Commands within a Shell
Transferring Files
Connecting through Proxies
Display the Authentication Banner
Using the SFTP Client
Port Forwarding
Working Examples
Configuring Listening Interfaces
Configuring Host Keys
Password Authentication
Public Key Authentication
Challenge-Response Authentication
Configuring Port Forwarding
Configuring SFTP
Supporting SCP
Implementing your own File System
Creating an Interactive Terminal
Proxy Protocol Support
SSH Client Quick Start
Add the Maven dependency to your project.
<dependency>
<groupId>com.sshtools</groupId>
<artifactId>maverick-synergy</artifactId>
<version>3.1.0</version>
</dependency>
Code language: HTML, XML (xml)
Import the SshClient class and create a connection, passing the hostname, port, username and password to the constructor.
import java.io.IOException;
import com.sshtools.client.SshClient;
import com.sshtools.client.SshClient.SshClientBuilder;
import com.sshtools.common.ssh.SshException;
try(SshClient ssh = SshClientBuilder.create()
.withHostname("localhost")
.withPort(2222)
.withUsername("root")
.withPassword("xxxxx")
.build()) {
ssh.putFile(new File("package.deb"));
ssh.executeCommand("dpkg -i package.deb");
} catch (IOException | SshException e) {
e.printStackTrace();
}
Code language: JavaScript (javascript)
Using a private key instead of a password is easy; replace withPassword method with:
.withPrivateKeyFile(Paths.get("file:///Users/lee/.ssh/id_rsa"));
Code language: JavaScript (javascript)
If you need to prompt for a passphrase from the user. You can set a PassphrasePrompt with an additional lambda expression.
.withPrivateKeyFile(Paths.get("file:///Users/lee/.ssh/id_rsa"), (info)->{
System.out.println(info);
return new String(System.console().readPassword());
})
Code language: JavaScript (javascript)
Inside the try/catch, you have a connection and authenticated SshClient instance. In the example, we upload a file and then execute a command to install it.
The client will be automatically disconnected when the program leaves the try/catch code block.
You can do much more; here are just a few ideas.
Executing Commands within a Shell
Transferring Files
Connecting through Proxies
Using the SFTP Client
Port Forwarding
.