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

Request a Pseudo-Terminal (PTY) over SSH

See more SSH Examples

Demonstrates the Chilkat Ssh.SendReqPty method, which requests a pseudo-terminal for a session channel. The arguments are the channel number, the terminal type, the character width and height, and the pixel width and height. It must be sent before the primary exec or shell request.

Background: A PTY makes the remote side behave as though a real terminal is attached, which matters for programs that draw screens, prompt for passwords, or colorize output — many refuse to run interactively without one. The terminal type (such as xterm) tells the remote program what escape sequences it may use. Pixel dimensions are commonly 0, meaning unspecified.

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;
  termType: string;
  widthInChars: Integer;
  heightInChars: Integer;
  widthInPixels: Integer;
  heightInPixels: Integer;

begin
  success := False;

  //  Demonstrates the Ssh.SendReqPty method, which requests a pseudo-terminal for a session
  //  channel.  The 1st argument is the channel number, the 2nd is the terminal type, the 3rd and
  //  4th are the character width and height, and the 5th and 6th are the pixel width and height.
  //  It must be sent before the primary exec or shell request.

  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 an 80x24 xterm.  Pixel dimensions of 0 mean unspecified.
  termType := 'xterm';
  widthInChars := 80;
  heightInChars := 24;
  widthInPixels := 0;
  heightInPixels := 0;
  success := ssh.SendReqPty(channelNum,termType,widthInChars,heightInChars,widthInPixels,heightInPixels);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  Start the shell now that the PTY is allocated.
  success := ssh.SendReqShell(channelNum);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  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.