Sample code for 30+ languages & platforms
Android™

Start SSH Tunnel Keyboard-Interactive Authentication

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.StartKeyboardAuth method, which begins keyboard-interactive authentication. The only argument is the login name. It returns XML describing the server's prompts, which are answered with ContinueKeyboardAuth.

Background: Keyboard-interactive is the prompt-driven authentication method: the server sends one or more questions — a password, a one-time code, a security question — and the client answers each. This is how a tunnel supports two-factor and other challenge-response schemes. The returned XML says exactly what to ask and whether to mask the input.

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.StartKeyboardAuth method, which begins keyboard-interactive
    //  authentication.  The only argument is the login name.  It returns XML describing the server's
    //  prompts, which are answered with ContinueKeyboardAuth.

    CkSshTunnel tunnel = new CkSshTunnel();

    //  The tunnel forwards accepted local connections to this destination through the SSH server.
    tunnel.put_DestHostname("db.internal.example.com");
    tunnel.put_DestPort(5432);

    //  Connect to the SSH server.  This performs the TCP/proxy connection and SSH handshake, but
    //  does not authenticate yet.
    int sshPort = 22;
    success = tunnel.Connect("ssh.example.com",sshPort);
    if (success == false) {
        Log.i(TAG, tunnel.lastErrorText());
        return;
        }

    //  Begin keyboard-interactive authentication.  The returned XML describes the server's prompts.
    String infoRequestXml = tunnel.startKeyboardAuth("mySshLogin");
    if (tunnel.get_LastMethodSuccess() == false) {
        Log.i(TAG, tunnel.lastErrorText());
        return;
        }

    Log.i(TAG, infoRequestXml);

    //  Answer the prompt(s) with ContinueKeyboardAuth, then call BeginAccepting.

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