Sample code for 30+ languages & platforms
Ruby

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 Ruby Downloads

Ruby
require 'chilkat'

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 = Chilkat::CkSsh.new()
ssh2 = Chilkat::CkSsh.new()
ssh3 = Chilkat::CkSsh.new()

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 = ssh1.Connect("ssh-server1.example.com",port)
if (success == false)
    print ssh1.lastErrorText() + "\n";
    exit
end

success = ssh1.AuthenticatePw("mySshLogin",password)
if (success == false)
    print ssh1.lastErrorText() + "\n";
    exit
end

success = ssh2.Connect("ssh-server2.example.com",port)
if (success == false)
    print ssh2.lastErrorText() + "\n";
    exit
end

success = ssh2.AuthenticatePw("mySshLogin",password)
if (success == false)
    print ssh2.lastErrorText() + "\n";
    exit
end

success = ssh3.Connect("ssh-server3.example.com",port)
if (success == false)
    print ssh3.lastErrorText() + "\n";
    exit
end

success = ssh3.AuthenticatePw("mySshLogin",password)
if (success == false)
    print ssh3.lastErrorText() + "\n";
    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 = ssh1.QuickCmdSend(cmd)
if (channel1 < 0)
    print ssh1.lastErrorText() + "\n";
    exit
end

channel2 = ssh2.QuickCmdSend(cmd)
if (channel2 < 0)
    print ssh2.lastErrorText() + "\n";
    exit
end

channel3 = ssh3.QuickCmdSend(cmd)
if (channel3 < 0)
    print ssh3.lastErrorText() + "\n";
    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
    if (!finished1)
        ch1 = ssh1.QuickCmdCheck(pollTimeoutMs)
        if (ch1 == -2)
            print ssh1.lastErrorText() + "\n";
            exit
        end

        if (ch1 >= 0)
            print "---- ssh1 channel " + ch1.to_s() + " finished ----" + "\n";
            out1 = ssh1.getReceivedText(ch1,"utf-8")
            if (ssh1.get_LastMethodSuccess() == false)
                print ssh1.lastErrorText() + "\n";
                exit
            end

            print out1 + "\n";
            finished1 = true
            numFinished = numFinished + 1
        end

    end

    if (!finished2)
        ch2 = ssh2.QuickCmdCheck(pollTimeoutMs)
        if (ch2 == -2)
            print ssh2.lastErrorText() + "\n";
            exit
        end

        if (ch2 >= 0)
            print "---- ssh2 channel " + ch2.to_s() + " finished ----" + "\n";
            out2 = ssh2.getReceivedText(ch2,"utf-8")
            if (ssh2.get_LastMethodSuccess() == false)
                print ssh2.lastErrorText() + "\n";
                exit
            end

            print out2 + "\n";
            finished2 = true
            numFinished = numFinished + 1
        end

    end

    if (!finished3)
        ch3 = ssh3.QuickCmdCheck(pollTimeoutMs)
        if (ch3 == -2)
            print ssh3.lastErrorText() + "\n";
            exit
        end

        if (ch3 >= 0)
            print "---- ssh3 channel " + ch3.to_s() + " finished ----" + "\n";
            out3 = ssh3.getReceivedText(ch3,"utf-8")
            if (ssh3.get_LastMethodSuccess() == false)
                print ssh3.lastErrorText() + "\n";
                exit
            end

            print out3 + "\n";
            finished3 = true
            numFinished = numFinished + 1
        end

    end

end

ssh1.Disconnect()
ssh2.Disconnect()
ssh3.Disconnect()