Sample code for 30+ languages & platforms
Android™

Open an SSH Tunnel

See more Socket/SSL/TLS Examples

Demonstrates Socket.SshOpenTunnel, which connects to an SSH server and initializes an SSH tunneling session, then authenticates it.

Background. An SSH tunnel lets a socket reach a destination host through an SSH server using port forwarding. The sequence is: open the tunnel, authenticate, then open forwarded channels to destinations.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    CkSocket socket = new CkSocket();

    //  Connect to the SSH server and initialize an SSH tunneling session.  Port 22 is conventional but
    //  not required.
    success = socket.SshOpenTunnel("ssh.example.com",22);
    if (success == false) {
        Log.i(TAG, socket.lastErrorText());
        return;
        }

    //  After the tunnel is open, authenticate before opening forwarded channels.
    //  The SSH password should come from a secure source rather than being hard-coded.
    String sshPassword = "mySshPassword";
    success = socket.SshAuthenticatePw("sshUser",sshPassword);
    if (success == false) {
        Log.i(TAG, socket.lastErrorText());
        return;
        }

    Log.i(TAG, "SSH tunnel established and authenticated.");

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}