Sample code for 30+ languages & platforms
AutoIt

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

AutoIt
Local $bSuccess = 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.

$oSsh1 = ObjCreate("Chilkat.Ssh")
$oSsh2 = ObjCreate("Chilkat.Ssh")
$oSsh3 = ObjCreate("Chilkat.Ssh")

Local $iPort = 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.
Local $sPassword = "mySshPassword"

$bSuccess = $oSsh1.Connect("ssh-server1.example.com",$iPort)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh1.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oSsh1.AuthenticatePw("mySshLogin",$sPassword)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh1.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oSsh2.Connect("ssh-server2.example.com",$iPort)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh2.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oSsh2.AuthenticatePw("mySshLogin",$sPassword)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh2.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oSsh3.Connect("ssh-server3.example.com",$iPort)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh3.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oSsh3.AuthenticatePw("mySshLogin",$sPassword)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh3.LastErrorText & @CRLF)
    Exit
EndIf

;  This command sleeps for 5 seconds and then prints the system date/time, making the parallel
;  execution easy to observe.
Local $sCmd = "sleep 5; date"

;  Start the command on each server.  QuickCmdSend returns immediately.
Local $iChannel1 = $oSsh1.QuickCmdSend($sCmd)
If ($iChannel1 < 0) Then
    ConsoleWrite($oSsh1.LastErrorText & @CRLF)
    Exit
EndIf

Local $iChannel2 = $oSsh2.QuickCmdSend($sCmd)
If ($iChannel2 < 0) Then
    ConsoleWrite($oSsh2.LastErrorText & @CRLF)
    Exit
EndIf

Local $iChannel3 = $oSsh3.QuickCmdSend($sCmd)
If ($iChannel3 < 0) Then
    ConsoleWrite($oSsh3.LastErrorText & @CRLF)
    Exit
EndIf

;  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.)
Local $iPollTimeoutMs = 50
Local $iNumFinished = 0
Local $bFinished1 = False
Local $bFinished2 = False
Local $bFinished3 = False

While $iNumFinished < 3
    If (Not $bFinished1) Then
Local $iCh1 = $oSsh1.QuickCmdCheck($iPollTimeoutMs)
        If ($iCh1 = -2) Then
            ConsoleWrite($oSsh1.LastErrorText & @CRLF)
            Exit
        EndIf

        If ($iCh1 >= 0) Then
            ConsoleWrite("---- ssh1 channel " & $iCh1 & " finished ----" & @CRLF)
Local $sOut1 = $oSsh1.GetReceivedText($iCh1,"utf-8")
            If ($oSsh1.LastMethodSuccess = False) Then
                ConsoleWrite($oSsh1.LastErrorText & @CRLF)
                Exit
            EndIf

            ConsoleWrite($sOut1 & @CRLF)
            $bFinished1 = True
            $iNumFinished = $iNumFinished + 1
        EndIf

    EndIf

    If (Not $bFinished2) Then
Local $iCh2 = $oSsh2.QuickCmdCheck($iPollTimeoutMs)
        If ($iCh2 = -2) Then
            ConsoleWrite($oSsh2.LastErrorText & @CRLF)
            Exit
        EndIf

        If ($iCh2 >= 0) Then
            ConsoleWrite("---- ssh2 channel " & $iCh2 & " finished ----" & @CRLF)
Local $sOut2 = $oSsh2.GetReceivedText($iCh2,"utf-8")
            If ($oSsh2.LastMethodSuccess = False) Then
                ConsoleWrite($oSsh2.LastErrorText & @CRLF)
                Exit
            EndIf

            ConsoleWrite($sOut2 & @CRLF)
            $bFinished2 = True
            $iNumFinished = $iNumFinished + 1
        EndIf

    EndIf

    If (Not $bFinished3) Then
Local $iCh3 = $oSsh3.QuickCmdCheck($iPollTimeoutMs)
        If ($iCh3 = -2) Then
            ConsoleWrite($oSsh3.LastErrorText & @CRLF)
            Exit
        EndIf

        If ($iCh3 >= 0) Then
            ConsoleWrite("---- ssh3 channel " & $iCh3 & " finished ----" & @CRLF)
Local $sOut3 = $oSsh3.GetReceivedText($iCh3,"utf-8")
            If ($oSsh3.LastMethodSuccess = False) Then
                ConsoleWrite($oSsh3.LastErrorText & @CRLF)
                Exit
            EndIf

            ConsoleWrite($sOut3 & @CRLF)
            $bFinished3 = True
            $iNumFinished = $iNumFinished + 1
        EndIf

    EndIf

Wend

$oSsh1.Disconnect 
$oSsh2.Disconnect 
$oSsh3.Disconnect