Android™
Android™
Get SSH Server Authentication Methods
See more SSH Examples
Demonstrates getting the authentication methods advertised by an SSH server. Connect first but do not authenticate, then call GetAuthMethods, which returns a lowercase, comma-separated list such as publickey,password,keyboard-interactive.
Important: Querying the authentication methods intentionally disconnects from the server, so reconnect before authenticating.
Background: Knowing what a server accepts lets a client choose the right authentication path rather than guessing — for example, falling back to keyboard-interactive when password authentication is not offered, or failing fast with a clear message when only public-key is allowed. It is also useful diagnostically when authentication fails for reasons that are not obvious from the error text.
Chilkat Android™ Downloads
// 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 getting the authentication methods advertised by an SSH server.
CkSsh ssh = new CkSsh();
int port = 22;
// The server must be connected, but not yet authenticated.
success = ssh.Connect("ssh.example.com",port);
if (success == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
// Returns a lowercase, comma-separated list, such as:
// publickey,password,keyboard-interactive
String authMethods = ssh.getAuthMethods();
if (ssh.get_LastMethodSuccess() == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
Log.i(TAG, "Authentication methods supported by this server: " + authMethods);
// IMPORTANT: Querying the authentication methods intentionally disconnects from the server.
// Reconnect before authenticating.
success = ssh.Connect("ssh.example.com",port);
if (success == false) {
Log.i(TAG, ssh.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 = ssh.AuthenticatePw("mySshLogin",password);
if (success == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
Log.i(TAG, "SSH authentication successful.");
ssh.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."
}
}