Java SSH Client by Example

Java SSH Client by Example

Maverick Synergy Java SSH Client Examples

Download Documentation Discuss

Introduction to the Java SSH Client API

The Maverick Synergy Java SSH Client is a third-generation API with over 18 years of development. In our examples below, we have tried as much as possible to create working examples that can be run and understood by an experienced Java programmer. Some source attributes, such as hostnames and IP addresses, may need to be changed. If you would like a specific example, don’t hesitate to contact us using our Contact Form.

Maverick Synergy is available under the LGPL open-source with regular updates and maintenance releases made in line with our open-source release policy. Priority support and instance hotfixes are available under a commercial subscription agreement. Our developers have over 18 years of experience working with and supporting Java SSH solutions.

Executing Remote Commands

A common requirement of using a Java SSH API is to execute commands on remote servers. As an end-user, SSH has two separate mechanisms that can acheive this; you can start an interactive shell and type commands by just connecting to the server i.e. ssh root@hostname, or you can execute the ssh command appending the command you want to execute to the command line i.e. ssh root@hostname ls -l

Executing Single Commands
Executing Interactive Commands within a Remote Shell

It is often required that a command be executed under a privileged account using the Sudo command. This could prompt the user for their password, which presents a problem for many developers as the shell is just a stream of bytes and examining the command output for this can be problematic. Maverick Synergy has solved this problem with its ExpectShell implementation.

Executing Interactive Commands with Sudo

File Transfer

Transferring Files by SFTP
Copy a Directory using SCP
Comparing the Contents of a Remote File against a Local File

Port Forwarding

Port Forwarding

Using Keys

How to use SSH Public Keys
Creating a Certificate Authority and Authenticating with OpenSSH Certificates

Jump Hosts

Connecting the Client through a Jump Host

Project Setup

The quickest way to set up your project to use Maverick Synergy is by using a dependency manager like Maven.

You can also download an archive from our Download page.

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sshtools</groupId>
<artifactId>examples</artifactId>
<version>0.0.1</version>

<dependencies>
   <dependency>
      <groupId>com.sshtools</groupId>
      <artifactId>maverick-synergy-client</artifactId>
      <version>3.0.7</version>
   </dependency>
</dependencies>

<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
	 <artifactId>maven-compiler-plugin</artifactId>
      <version>3.1</version>
	<configuration>
	   <source>1.8</source>
	   <target>1.8</target>
	</configuration>
      </plugin>
   </plugins>
</build>
</project>