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

Wait for a Message on Any SSH Channel

See more SSH Examples

Demonstrates the Chilkat Ssh.WaitForChannelMessage method, which waits for the next SSH channel message on any channel. The only argument is the poll timeout: 0 for a nonblocking check, a positive value to wait that long, or a negative value to wait with no method-level timeout. It returns the channel number the message belongs to, -1 if nothing arrived before the timeout, or -2 on another error such as a lost connection.

Background: When several channels are active at once, waiting on one specific channel would block while others sit ready. This method is the multiplexed alternative — it returns as soon as any channel has activity and tells you which one, so a single loop can service them all. The explicit timeout argument governs the wait; shorter ReadTimeoutMs values do not cut it short.

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;
  pollTimeoutMs: Integer;
  result: Integer;

begin
  success := False;

  //  Demonstrates the Ssh.WaitForChannelMessage method, which waits for the next SSH channel
  //  message on ANY channel.  The only argument is the poll timeout in milliseconds: 0 for a
  //  nonblocking check, a positive value to wait that long, or a negative value to wait with no
  //  method-level timeout.

  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;

  channelNum := ssh.OpenSessionChannel();
  if (channelNum < 0) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  success := ssh.SendReqExec(channelNum,'ls -l /tmp');
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  Wait up to 5 seconds for the next channel message.  The return value is the channel number
  //  the message belongs to, -1 if nothing arrived before the timeout, or -2 on another error.
  pollTimeoutMs := 5000;
  result := ssh.WaitForChannelMessage(pollTimeoutMs);

  if (result = -2) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  if (result = -1) then
    begin
      WriteLn('No channel message arrived before the timeout.');
    end;

  if (result >= 0) then
    begin
      WriteLn('Processed a message for channel ' + result);
    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.