Sample code for 30+ languages & platforms
Android™

Connect an SSH Tunnel Through a Jump Host

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.ConnectThroughSsh method, which connects to an SSH server through an already connected and authenticated Ssh object. The first argument is the jump-host Ssh object, and the second and third are the destination SSH hostname and port.

Background: This chains the tunnel through a "jump host" (bastion): the application reaches a gateway server, then tunnels onward to an SSH server that is only accessible from inside the network. Each hop authenticates separately with its own credentials. The result is a normal SshTunnel — still requiring its own authentication and BeginAccepting — whose transport is routed through the first connection.

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;

    //  Demonstrates the SshTunnel.ConnectThroughSsh method, which connects to an SSH server through
    //  an already connected and authenticated Ssh object (a jump host).  The 1st argument is the Ssh
    //  object, and the 2nd and 3rd are the destination SSH hostname and port.

    CkSsh sshJump = new CkSsh();

    //  Connect and authenticate to the jump host first.
    int sshPort = 22;
    success = sshJump.Connect("jump.example.com",sshPort);
    if (success == false) {
        Log.i(TAG, sshJump.lastErrorText());
        return;
        }

    //  Normally you would not hard-code the password in source.  You should instead obtain it
    //  from an interactive prompt, environment variable, or a secrets vault.
    String password = "mySshPassword";

    success = sshJump.AuthenticatePw("myJumpLogin",password);
    if (success == false) {
        Log.i(TAG, sshJump.lastErrorText());
        return;
        }

    //  Set up the tunnel and connect to the target SSH server through the jump host.
    CkSshTunnel tunnel = new CkSshTunnel();
    tunnel.put_DestHostname("db.internal.example.com");
    tunnel.put_DestPort(5432);

    success = tunnel.ConnectThroughSsh(sshJump,"ssh.internal.example.com",sshPort);
    if (success == false) {
        Log.i(TAG, tunnel.lastErrorText());
        return;
        }

    //  Authenticate to the target SSH server (its own credentials).
    success = tunnel.AuthenticatePw("mySshLogin",password);
    if (success == false) {
        Log.i(TAG, tunnel.lastErrorText());
        return;
        }

    //  After authenticating, call BeginAccepting to start listening for local connections to forward.

    boolean waitForThreadExit = true;
    success = tunnel.CloseTunnel(waitForThreadExit);
    if (success == false) {
        Log.i(TAG, tunnel.lastErrorText());
        return;
        }

    sshJump.Disconnect();

  }

  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."
  }
}