Sample code for 30+ languages & platforms
React Native

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 React Native Downloads

React Native
import { Ssh } from '@chilkat/react-native'

async function chilkatExample() {
  // 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.

  const ssh1 = new Ssh();
  const ssh2 = new Ssh();
  const ssh3 = new Ssh();

  const 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.
  const password = 'mySshPassword';

  try {
    await ssh1.connectAsync('ssh-server1.example.com', port);
  } catch {
    console.log(ssh1.lastErrorText);
    return;
  }

  try {
    await ssh1.authenticatePwAsync('mySshLogin', password);
  } catch {
    console.log(ssh1.lastErrorText);
    return;
  }

  try {
    await ssh2.connectAsync('ssh-server2.example.com', port);
  } catch {
    console.log(ssh2.lastErrorText);
    return;
  }

  try {
    await ssh2.authenticatePwAsync('mySshLogin', password);
  } catch {
    console.log(ssh2.lastErrorText);
    return;
  }

  try {
    await ssh3.connectAsync('ssh-server3.example.com', port);
  } catch {
    console.log(ssh3.lastErrorText);
    return;
  }

  try {
    await ssh3.authenticatePwAsync('mySshLogin', password);
  } catch {
    console.log(ssh3.lastErrorText);
    return;
  }

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

  // Start the command on each server.  QuickCmdSend returns immediately.
  const channel1 = await ssh1.quickCmdSendAsync(cmd);
  if (channel1 < 0) {
    console.log(ssh1.lastErrorText);
    return;
  }

  const channel2 = await ssh2.quickCmdSendAsync(cmd);
  if (channel2 < 0) {
    console.log(ssh2.lastErrorText);
    return;
  }

  const channel3 = await ssh3.quickCmdSendAsync(cmd);
  if (channel3 < 0) {
    console.log(ssh3.lastErrorText);
    return;
  }

  // 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.)
  const pollTimeoutMs = 50;
  let numFinished = 0;
  let finished1 = false;
  let finished2 = false;
  let finished3 = false;

  while (numFinished < 3) {
    if (!finished1) {
      const ch1 = await ssh1.quickCmdCheckAsync(pollTimeoutMs);
      if (ch1 === -2) {
        console.log(ssh1.lastErrorText);
        return;
      }

      if (ch1 >= 0) {
        console.log(`---- ssh1 channel ${ch1} finished ----`);
        let out1: string;
        try {
          out1 = ssh1.getReceivedText(ch1, 'utf-8');
        } catch {
          console.log(ssh1.lastErrorText);
          return;
        }

        console.log(out1);
        finished1 = true;
        numFinished++;
      }
    }

    if (!finished2) {
      const ch2 = await ssh2.quickCmdCheckAsync(pollTimeoutMs);
      if (ch2 === -2) {
        console.log(ssh2.lastErrorText);
        return;
      }

      if (ch2 >= 0) {
        console.log(`---- ssh2 channel ${ch2} finished ----`);
        let out2: string;
        try {
          out2 = ssh2.getReceivedText(ch2, 'utf-8');
        } catch {
          console.log(ssh2.lastErrorText);
          return;
        }

        console.log(out2);
        finished2 = true;
        numFinished++;
      }
    }

    if (!finished3) {
      const ch3 = await ssh3.quickCmdCheckAsync(pollTimeoutMs);
      if (ch3 === -2) {
        console.log(ssh3.lastErrorText);
        return;
      }

      if (ch3 >= 0) {
        console.log(`---- ssh3 channel ${ch3} finished ----`);
        let out3: string;
        try {
          out3 = ssh3.getReceivedText(ch3, 'utf-8');
        } catch {
          console.log(ssh3.lastErrorText);
          return;
        }

        console.log(out3);
        finished3 = true;
        numFinished++;
      }
    }
  }

  ssh1.disconnect();
  ssh2.disconnect();
  ssh3.disconnect();
}