Sample code for 30+ languages & platforms
Delphi DLL

SSH Parallel Remote Commands on Multiple Servers

See more SSH Examples

Demonstrates running the same command on several servers simultaneously. It is simply a matter of using one Ssh object per server, starting the command on each with QuickCmdSend, and then polling each connection with QuickCmdCheck until all have finished.

Background: This is the fan-out pattern behind configuration-management and monitoring tools: query or update a whole fleet in roughly the time the slowest single host takes, rather than the sum of all of them. Each server needs its own object because each has its own connection and authentication state. Note that results come back in whatever order the servers finish, which is why the example tracks completion per connection. Production code would keep the objects in arrays instead of repeating the logic per server.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
ssh1: HCkSsh;
ssh2: HCkSsh;
ssh3: HCkSsh;
port: Integer;
password: PWideChar;
cmd: PWideChar;
channel1: Integer;
channel2: Integer;
channel3: Integer;
pollTimeoutMs: Integer;
numFinished: Integer;
finished1: Boolean;
finished2: Boolean;
finished3: Boolean;
ch1: Integer;
out1: PWideChar;
ch2: Integer;
out2: PWideChar;
ch3: Integer;
out3: PWideChar;

begin
success := False;

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  Demonstrates running the same command on several servers simultaneously.  It is simply a
//  matter of using one Ssh object per server.

ssh1 := CkSsh_Create();
ssh2 := CkSsh_Create();
ssh3 := CkSsh_Create();

port := 22;

//  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 := CkSsh_Connect(ssh1,'ssh-server1.example.com',port);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh1));
    Exit;
  end;
success := CkSsh_AuthenticatePw(ssh1,'mySshLogin',password);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh1));
    Exit;
  end;

success := CkSsh_Connect(ssh2,'ssh-server2.example.com',port);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh2));
    Exit;
  end;
success := CkSsh_AuthenticatePw(ssh2,'mySshLogin',password);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh2));
    Exit;
  end;

success := CkSsh_Connect(ssh3,'ssh-server3.example.com',port);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh3));
    Exit;
  end;
success := CkSsh_AuthenticatePw(ssh3,'mySshLogin',password);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh3));
    Exit;
  end;

//  This command sleeps for 5 seconds and then prints the system date/time, making the parallel
//  execution easy to observe.
cmd := 'sleep 5; date';

//  Start the command on each server.  QuickCmdSend returns immediately.
channel1 := CkSsh_QuickCmdSend(ssh1,cmd);
if (channel1 < 0) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh1));
    Exit;
  end;

channel2 := CkSsh_QuickCmdSend(ssh2,cmd);
if (channel2 < 0) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh2));
    Exit;
  end;

channel3 := CkSsh_QuickCmdSend(ssh3,cmd);
if (channel3 < 0) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh3));
    Exit;
  end;

//  The command is now running on all three servers at once.  Poll each connection until every
//  command has finished.  (Production code would keep the objects in arrays rather than
//  repeating the logic.)
pollTimeoutMs := 50;
numFinished := 0;
finished1 := False;
finished2 := False;
finished3 := False;

while numFinished < 3 do
  begin
    if (not finished1) then
      begin
        ch1 := CkSsh_QuickCmdCheck(ssh1,pollTimeoutMs);
        if (ch1 = -2) then
          begin
            Memo1.Lines.Add(CkSsh__lastErrorText(ssh1));
            Exit;
          end;
        if (ch1 >= 0) then
          begin
            Memo1.Lines.Add('---- ssh1 channel ' + IntToStr(ch1) + ' finished ----');
            out1 := CkSsh__getReceivedText(ssh1,ch1,'utf-8');
            if (CkSsh_getLastMethodSuccess(ssh1) = False) then
              begin
                Memo1.Lines.Add(CkSsh__lastErrorText(ssh1));
                Exit;
              end;
            Memo1.Lines.Add(out1);
            finished1 := True;
            numFinished := numFinished + 1;
          end;
      end;

    if (not finished2) then
      begin
        ch2 := CkSsh_QuickCmdCheck(ssh2,pollTimeoutMs);
        if (ch2 = -2) then
          begin
            Memo1.Lines.Add(CkSsh__lastErrorText(ssh2));
            Exit;
          end;
        if (ch2 >= 0) then
          begin
            Memo1.Lines.Add('---- ssh2 channel ' + IntToStr(ch2) + ' finished ----');
            out2 := CkSsh__getReceivedText(ssh2,ch2,'utf-8');
            if (CkSsh_getLastMethodSuccess(ssh2) = False) then
              begin
                Memo1.Lines.Add(CkSsh__lastErrorText(ssh2));
                Exit;
              end;
            Memo1.Lines.Add(out2);
            finished2 := True;
            numFinished := numFinished + 1;
          end;
      end;

    if (not finished3) then
      begin
        ch3 := CkSsh_QuickCmdCheck(ssh3,pollTimeoutMs);
        if (ch3 = -2) then
          begin
            Memo1.Lines.Add(CkSsh__lastErrorText(ssh3));
            Exit;
          end;
        if (ch3 >= 0) then
          begin
            Memo1.Lines.Add('---- ssh3 channel ' + IntToStr(ch3) + ' finished ----');
            out3 := CkSsh__getReceivedText(ssh3,ch3,'utf-8');
            if (CkSsh_getLastMethodSuccess(ssh3) = False) then
              begin
                Memo1.Lines.Add(CkSsh__lastErrorText(ssh3));
                Exit;
              end;
            Memo1.Lines.Add(out3);
            finished3 := True;
            numFinished := numFinished + 1;
          end;
      end;
  end;

CkSsh_Disconnect(ssh1);
CkSsh_Disconnect(ssh2);
CkSsh_Disconnect(ssh3);

CkSsh_Dispose(ssh1);
CkSsh_Dispose(ssh2);
CkSsh_Dispose(ssh3);