Using SSH from Android

SSH is a protocol used to run commands securely in a remote machine. In my dissertation I needed to invoke latex commands in a remote machine the user mentions from an Android app. I explain the steps below to connect to a remote machine through SSH from an Android app. We will be using SSHJ API to do this. SSHJ is an open source Java implementation of the SSH protocol.

The steps involved in using the SSH protocol are as follows

  1. Include the library files in the build path.
    • Download the latest version of the SSHJ library from
    • Extract the zip file and copy the “sshj-0.8.1.jar” into the libs directory of your android project.
    • Include the dependencies of the SSHJ library also in the build path.
      • SLF4J – logging library. (slf4j-api-1.6.99.jar, slf4j-simple-1.6.99.jar)
      • Bounty Castle – Security library. (bcpkix-jdk15on-147.jar, bcprov-jdk15on-147.jar)
      • JZlib – Compression library. ()
    • Include the above mentioned jar files also into the lib folder of the Android project.
  2. To use username and password authentication use a NullHostKeyVerifier like the below one.

import net.schmizz.sshj.transport.verification.HostKeyVerifier;

public class NullHostKeyVerifier implements HostKeyVerifier {

* This method is used to bypass HostKeyVerification.
* It returns true for whatever the input is.
public boolean verify(String arg0, int arg1, PublicKey arg2) {
return true;
  1. Then use the below code to connect to a remote machine and to execute a command in a remote machine and to get the output of the command in the remote machine.
// creates new SSH client
final SSHClient ssh = new SSHClient(new AndroidConfig());
Command cmd = null;

// Adds a nullHostKeyVerifier
ssh.addHostKeyVerifier(new NullHostKeyVerifier());

// default port number
int pn = 22;

// connect to the machine
try {
ssh.connect(ipaddress, pn);
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);

// Authenticate with the password entered
ssh.authPassword(username, password);

// start a new session
final Session session = ssh.startSession();

// runs the commands in the remote machine
cmd = session.exec("ls");

// reads the output of the command
result = IOUtils.readFully(cmd.getInputStream()).toString();

I have excluded the try catch from the code.
Note: Line number 2 is the difference in using the code from a Java application and from the Android application.

Enhanced by Zemanta

4 thoughts on “Using SSH from Android

  1. Thank you so much, this is very helpful!
    What kind of permissions would I need to add to let the application work?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s