Sample code for 30+ languages & platforms
Android™

SSH Keyboard-Interactive Authentication

See more SSH Examples

Demonstrates keyboard-interactive authentication with an SSH server. StartKeyboardAuth returns XML describing the server's prompts, and ContinueKeyboardAuth submits each response. Authentication is complete when the returned XML contains either a success or an error node.

Background: Keyboard-interactive is SSH's flexible, prompt-driven method: rather than assuming a single password, the server asks one or more questions — a password, a one-time code, a security question — and the client answers each. This is how SSH supports two-factor and other challenge-response schemes. The prompt XML also indicates whether each response should be echoed, so a client knows when to mask input. A server may issue several rounds, so a robust implementation loops until it sees success or error rather than assuming one exchange is enough.

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;

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  Demonstrates keyboard-interactive authentication with an SSH server.  The server sends one or
    //  more prompts as XML, and the application answers each with ContinueKeyboardAuth.

    CkSsh ssh = new CkSsh();

    ssh.put_ConnectTimeoutMs(5000);
    ssh.put_ReadTimeoutMs(15000);

    String hostname = "ssh.example.com";
    int port = 22;
    success = ssh.Connect(hostname,port);
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

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

    //  If the server sent a user authentication banner, an application may display it before
    //  prompting.
    Log.i(TAG, "UserAuthBanner: " + ssh.userAuthBanner());

    CkXml xml = new CkXml();
    success = xml.LoadXml(xmlResponse);
    if (success == false) {
        Log.i(TAG, xml.lastErrorText());
        return;
        }

    //  Authentication is complete when the XML contains either a "success" or an "error" node.
    if (xml.HasChildWithTag("success")) {
        Log.i(TAG, "No password required, already authenticated.");
        return;
        }

    if (xml.HasChildWithTag("error")) {
        Log.i(TAG, "Authentication failed.");
        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";

    //  Answer the prompt.  Typically one call is enough, but a server may issue several rounds of
    //  prompts, so a robust client loops until it sees "success" or "error".
    xmlResponse = ssh.continueKeyboardAuth(password);
    if (ssh.get_LastMethodSuccess() == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    success = xml.LoadXml(xmlResponse);
    if (success == false) {
        Log.i(TAG, xml.lastErrorText());
        return;
        }

    if (xml.HasChildWithTag("success")) {
        Log.i(TAG, "SSH keyboard-interactive authentication successful.");
        return;
        }

    if (xml.HasChildWithTag("error")) {
        Log.i(TAG, "Authentication failed.");
        }


  }

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