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

SSH Tunnel Authenticate with a Password

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.AuthenticatePw method, which authenticates the SSH connection with a login name and password. The first argument is the login and the second is the password. Connect first, and call BeginAccepting afterward.

Background: Password authentication is the simplest option, but the password is a secret and should never be a hard-coded literal — obtain it from an interactive prompt, an environment variable, or a secrets vault. Public-key authentication is generally preferred for the unattended, long-running services that tunnels often are.

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.SshTunnel;

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

procedure RunDemo;
var
  success: Boolean;
  tunnel: TSshTunnel;
  sshPort: Integer;
  password: string;
  waitForThreadExit: Boolean;

begin
  success := False;

  //  Demonstrates the SshTunnel.AuthenticatePw method, which authenticates the SSH connection with
  //  a login name and password.  The 1st argument is the login and the 2nd is the password.

  tunnel := TSshTunnel.Create;

  //  The tunnel forwards accepted local connections to this destination through the SSH server.
  tunnel.DestHostname := 'db.internal.example.com';
  tunnel.DestPort := 5432;

  //  Connect to the SSH server.  This performs the TCP/proxy connection and SSH handshake, but
  //  does not authenticate yet.
  sshPort := 22;
  success := tunnel.Connect('ssh.example.com',sshPort);
  if (success = False) then
    begin
      WriteLn(tunnel.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 := tunnel.AuthenticatePw('mySshLogin',password);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;
  WriteLn('Authenticated.');

  //  After authenticating, call BeginAccepting to start listening for local connections to forward.

  waitForThreadExit := True;
  success := tunnel.CloseTunnel(waitForThreadExit);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;


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