Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Start SSH Keyboard-Interactive Authentication

See more SSH Examples

Demonstrates the Chilkat Ssh.StartKeyboardAuth method, which begins keyboard-interactive authentication. The only argument is the login name. It returns XML describing the server's prompts and whether each response should be echoed. Answer the prompts with ContinueKeyboardAuth.

Background: Keyboard-interactive is the flexible, prompt-driven SSH method — the server sends one or more questions (a password, a one-time code, a security question) and the client answers each. It is how SSH supports two-factor and other challenge-response schemes. The returned XML tells you exactly what to ask the user and whether to mask their input.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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;
  sshPort: Integer;
  infoRequestXml: string;

begin
  success := False;

  //  Demonstrates the Ssh.StartKeyboardAuth method, which begins keyboard-interactive
  //  authentication.  The only argument is the login name.  It returns XML describing the server's
  //  prompts and whether each response should be echoed.

  ssh := TSsh.Create;

  //  Connect to the SSH server.  Port 22 is the usual SSH port.
  sshPort := 22;
  success := ssh.Connect('ssh.example.com',sshPort);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  Begin keyboard-interactive authentication.  The returned XML describes the prompts.
  infoRequestXml := ssh.StartKeyboardAuth('mySshLogin');
  if (ssh.LastMethodSuccess = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;
  WriteLn(infoRequestXml);

  //  Answer the prompt(s) with ContinueKeyboardAuth.

  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.