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

Collect Completed SSH Commands with QuickCmdCheck

See more SSH Examples

Demonstrates the Chilkat Ssh.QuickCmdCheck method, which waits for any command started by QuickCmdSend to complete. The only argument is the poll timeout in milliseconds; 0 performs a nonblocking check. It returns the channel number of a completed command, -1 when commands are still pending but none finished before the timeout, or -2 when no quick commands remain or an error occurred.

Background: This is the collection half of the concurrent pattern. Each completed channel is reported only once, so looping until -2 drains every outstanding command exactly once. Distinguish the two negative values carefully: -1 simply means "still working, ask again," while -2 means there is nothing left to wait for — or that the connection failed, so check LastErrorText when the distinction matters. Manually opened exec channels are not reported here.

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;
  channel1: Integer;
  channel2: Integer;
  pollTimeoutMs: Integer;
  completedChannel: Integer;
  output: string;

begin
  success := False;

  //  Demonstrates the Ssh.QuickCmdCheck method, which waits for any command started by
  //  QuickCmdSend to complete.  The only argument is the poll timeout in milliseconds; 0 performs
  //  a nonblocking check.

  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;

  //  Start two commands that will finish at different times.
  channel1 := ssh.QuickCmdSend('sleep 2; echo first done');
  if (channel1 < 0) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  channel2 := ssh.QuickCmdSend('echo second done');
  if (channel2 < 0) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  Collect the results as they complete.  The return value is the channel number of a completed
  //  command, -1 if commands are still pending but none finished before the timeout, or -2 when
  //  no quick commands remain (or an error occurred).
  pollTimeoutMs := 5000;
  completedChannel := ssh.QuickCmdCheck(pollTimeoutMs);
  while completedChannel <> -2 do
    begin
      if (completedChannel >= 0) then
        begin
          output := ssh.GetReceivedText(completedChannel,'utf-8');
          if (ssh.LastMethodSuccess = False) then
            begin
              WriteLn(ssh.LastErrorText);
              Exit;
            end;
          WriteLn('Channel ' + completedChannel + ' finished: ' + output);
        end;

      if (completedChannel = -1) then
        begin
          WriteLn('Still waiting for a command to finish...');
        end;

      completedChannel := ssh.QuickCmdCheck(pollTimeoutMs);
    end;

  WriteLn('All quick commands have been collected.');

  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.