Skip to content
This repository has been archived by the owner. It is now read-only.
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

NOTE: this project is no longer maintained and has been archived.

Simple SSH client for Java

This library is minimal by design, with minimal dependencies. Features:

  • Command execution
  • Thread-safe (reusable)
  • Configurable (e.g. session timeout)
  • Only public key authentication is supported
  • Pseudo-terminal (pty) allocation (for sudo)
  • Stdin may be passed in to commands

For more advanced SSH usage, I recommend Ansible. You can use this Java library to invoke Ansible remotely, and have Ansible (playbooks) do the real work.

Utilizes the Jsch SSH 2 library.

Maven dependency


Simple usage

// Think uname=$(ssh uname -a)
UserAtHost userAtHost = new UserAtHost("user", "");
SshClient sshClient = new JschSshClient("~/.ssh/id_rsa", "passphrase");
Result result = sshClient.executeCommand("uname -a", userAtHost);
String uname = result.stdoutAsText();

Passing in stdin and using a non-standard port

// Think echo secret | ssh -i path/to/ssh/private_key -p 2020 "cat - > secret.txt"
ByteBuffer stdin = ByteBuffer.wrap("secret".getBytes());
UserAtHost userAtHost = new UserAtHost("user", "", 2020);
SshClient sshClient = new JschSshClient("path/to/ssh/private_key", "passphrase");
sshClient.executeCommand("cat - > secret.txt", stdin, userAtHost);

Spring configuration (using the c-namespace and property placeholders)

// META-INF/spring/config.xml:

<bean id="sshClient" class="fi.jpalomaki.ssh.jsch.JschSshClient"
    c:privateKey="${ssh.privateKey}" c:knownHosts="${ssh.knownHosts}"
    c:passphrase="${ssh.passphrase}" c:options-ref="sshOptions"

<bean id="sshOptions" class="fi.jpalomaki.ssh.jsch.JschSshClient$Options"
    c:connectTimeout="3s" c:sessionTimeout="5m" c:maxStdoutSize="1M"
    c:maxStderrSize="1M" c:sshConfig="CompressionLevel=1;TCPKeepAlive=no"
<context:property-placeholder location="file:/opt/app/" />

// /opt/app/

ssh.knownHosts = /path/to/.ssh/known_hosts
ssh.privateKey = /path/to/.ssh/id_rsa
ssh.passphrase = supersecret

Mock SSH client Spring configuration, configurable at runtime through JMX (using e.g. jconsole)

// E.g. META-INF/spring/config.xml:

<bean id="mockSshClient" class="fi.jpalomaki.ssh.mock.MockSshClient" p:configuration-ref="mockSshClientConfiguration" />

<bean id="mockSshClientConfiguration" class="fi.jpalomaki.ssh.mock.MockSshClient.Configuration" p:commandDurationSeconds="5" />

<bean class="org.springframework.jmx.export.MBeanExporter">
    <property name="beans">
            <entry key="ssh:name=mockSshClientConfiguration" value-ref="mockSshClientConfiguration" />


Simple but configurable SSH client for Java



No packages published


You can’t perform that action at this time.