Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
SSH Parallel Remote Commands on a Single Server
See more SSH Examples
Demonstrates running several commands in parallel on one SSH server and collecting each command's output as it finishes. QuickCmdSend starts each command and returns immediately; QuickCmdCheck reports them as they complete.
Background: Because SSH multiplexes channels, several commands can run at once over one authenticated connection — far faster than running them one after another when each involves waiting. Results arrive in completion order rather than the order the commands were started, so the loop keys off the returned channel number. Distinguish the two negative returns carefully:
-1 means "still working, ask again," while -2 means nothing remains to collect or the connection failed.Chilkat Pascal (Lazarus/Delphi) Downloads
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;
port: Integer;
password: string;
channel1: Integer;
channel2: Integer;
channel3: Integer;
pollTimeoutMs: Integer;
numFinished: Integer;
channel: Integer;
cmdOutput: string;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates running several commands in parallel on a single SSH server and collecting each
// command's output as it finishes.
ssh := TSsh.Create;
port := 22;
success := ssh.Connect('ssh.example.com',port);
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;
// QuickCmdSend starts a command and returns immediately, so all three run concurrently, each on
// its own session channel.
channel1 := ssh.QuickCmdSend('df');
if (channel1 < 0) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
channel2 := ssh.QuickCmdSend('date');
if (channel2 < 0) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
channel3 := ssh.QuickCmdSend('echo hello world');
if (channel3 < 0) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
// Collect the results. QuickCmdCheck returns the channel number of a completed command,
// -1 when commands are still pending but none finished within the timeout, or -2 when nothing
// remains to be checked (or an error occurred).
pollTimeoutMs := 50;
numFinished := 0;
while numFinished < 3 do
begin
channel := ssh.QuickCmdCheck(pollTimeoutMs);
if (channel = -2) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
if (channel >= 0) then
begin
WriteLn('---- channel ' + channel + ' finished ----');
cmdOutput := ssh.GetReceivedText(channel,'utf-8');
if (ssh.LastMethodSuccess = False) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
WriteLn(cmdOutput);
numFinished := numFinished + 1;
end;
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.