Sample code for 30+ languages & platforms
Lazarus Pascal

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 Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Ssh;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  ssh: TSsh;
  port: Integer;
  authMethods: string;
  password: string;

begin
  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.

  ssh := TSsh.Create;

  port := 22;

  //  The server must be connected, but not yet authenticated.
  success := ssh.Connect('ssh.example.com',port);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  Returns a lowercase, comma-separated list, such as:
  //      publickey,password,keyboard-interactive
  authMethods := ssh.GetAuthMethods();
  if (ssh.LastMethodSuccess = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;
  WriteLn('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) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  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.
  password := 'mySshPassword';

  success := ssh.AuthenticatePw('mySshLogin',password);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  WriteLn('SSH authentication successful.');

  ssh.Disconnect();


  ssh.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.