SSH Client Quick Start

Add the Maven dependency to your project.


Import the SshClient class and create a connection, passing the hostname, port, username and password to the constructor.


import com.sshtools.client.SshClient;
import com.sshtools.client.SshClient.SshClientBuilder;
import com.sshtools.common.ssh.SshException;

try(SshClient ssh = SshClientBuilder.create()
                    .build()) {
   ssh.putFile(new File("package.deb"));
   ssh.executeCommand("dpkg -i package.deb");
} catch (IOException | SshException e) { 

Using a private key instead of a password is easy; replace withPassword method with:


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)->{
     return new String(System.console().readPassword());

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