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

Request SSH X11 Forwarding

See more SSH Examples

Demonstrates the Chilkat Ssh.SendReqX11Forwarding method, which sends an X11-forwarding request on a session channel. The arguments are the channel number, whether forwarding is limited to a single connection, and the X11 authentication protocol, cookie, and screen number.

Background: X11 forwarding lets a graphical program running on the remote host display its window on your local machine, tunneled over SSH. This is a low-level method that only sends the protocol request — your application is still responsible for the local X server side of the arrangement. The authentication cookie guards the forwarded display, so it should be a freshly generated value rather than a fixed constant.

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;
  password: string;
  channelNum: Integer;
  singleConnection: Boolean;
  authProt: string;
  authCookie: string;
  screenNum: Integer;

begin
  success := False;

  //  Demonstrates the Ssh.SendReqX11Forwarding method, which sends an SSH X11-forwarding request
  //  on a session channel.  The 1st argument is the channel number, the 2nd limits forwarding to a
  //  single connection, and the 3rd through 5th supply the X11 authentication protocol, cookie,
  //  and screen number.

  ssh := TSsh.Create;

  sshPort := 22;
  success := ssh.Connect('ssh.example.com',sshPort);
  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;

  //  Open a session channel.  A negative return value indicates failure.
  channelNum := ssh.OpenSessionChannel();
  if (channelNum < 0) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  Request X11 forwarding.  Named variables make each argument's meaning clear.
  singleConnection := False;
  authProt := 'MIT-MAGIC-COOKIE-1';
  authCookie := 'd92f5e1a7c8b4306af17c2e5b9d43081';
  screenNum := 0;
  success := ssh.SendReqX11Forwarding(channelNum,singleConnection,authProt,authCookie,screenNum);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;
  WriteLn('X11 forwarding requested.');

  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.