Sample code for 30+ languages & platforms
Android™

SSH Tunnel Uncommon Options

See more SSH Tunnel Examples

Demonstrates the UncommonOptions property, which enables optional behavior for uncommon requirements. The default is an empty string; provide a comma-separated list of keywords to enable more than one option. Available keywords are listed in the reference documentation.

Background: This is a catch-all for narrow workarounds that do not warrant their own properties — forcing a specific signature algorithm, disabling the periodic keep-alive, removing weak MACs, bypassing a VPN on Android, and so on. Most applications never set it. When you do, it is usually to satisfy a particular server's quirk or a specific security requirement, and the exact keyword matters, so consult the documented list rather than guessing.

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.UncommonOptions property, which enables optional behavior for
    //  uncommon requirements.  The default is an empty string, appropriate for most applications.
    //  Provide a comma-separated list of keywords to enable more than one option.

    CkSshTunnel tunnel = new CkSshTunnel();

    //  For example, disable the periodic keep-alive ignore message and remove weak MAC algorithms
    //  from those offered.  Available keywords are listed in the reference documentation.
    tunnel.put_UncommonOptions("NoKeepAliveIgnoreMsg,no-weak-mac-algs");

    tunnel.put_DestHostname("db.internal.example.com");
    tunnel.put_DestPort(5432);

    int sshPort = 22;
    success = tunnel.Connect("ssh.example.com",sshPort);
    if (success == false) {
        Log.i(TAG, tunnel.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 = tunnel.AuthenticatePw("mySshLogin",password);
    if (success == false) {
        Log.i(TAG, tunnel.lastErrorText());
        return;
        }

    int listenPort = 1080;
    success = tunnel.BeginAccepting(listenPort);
    if (success == false) {
        Log.i(TAG, tunnel.lastErrorText());
        return;
        }

    Log.i(TAG, "Tunnel established.");

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


  }

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