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>

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();
}

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

.withPrivateKeyFile(Paths.get("file:///Users/lee/.ssh/id_rsa"));

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());
})

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

.