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

Notify the SSH Server of a Terminal Resize

See more SSH Examples

Demonstrates the Chilkat Ssh.SendReqWindowChange method, which tells the server that the terminal dimensions changed. The arguments are the channel number, the new character width and height, and the pixel width and height. It may be sent any time after the PTY is allocated.

Background: When a user resizes a terminal window, full-screen remote programs such as vi, top, or less need to know the new size or they will keep drawing at the old dimensions. This request delivers that update, causing the server to signal the remote process (SIGWINCH on Unix) so it can redraw correctly.

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;
  newWidthInChars: Integer;
  newHeightInRows: Integer;
  pixWidth: Integer;
  pixHeight: Integer;

begin
  success := False;

  //  Demonstrates the Ssh.SendReqWindowChange method, which notifies the server that the terminal
  //  dimensions changed.  The 1st argument is the channel number, the 2nd and 3rd are the new
  //  character width and height, and the 4th and 5th are the pixel width and height.

  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;

  //  Allocate a PTY and start a shell.
  success := ssh.SendReqPty(channelNum,'xterm',80,24,0,0);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;
  success := ssh.SendReqShell(channelNum);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  The user resized the terminal window -- tell the server the new dimensions.
  newWidthInChars := 132;
  newHeightInRows := 43;
  pixWidth := 0;
  pixHeight := 0;
  success := ssh.SendReqWindowChange(channelNum,newWidthInChars,newHeightInRows,pixWidth,pixHeight);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;
  WriteLn('Window change sent.');

  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.