Lianja
Lianja
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 Lianja Downloads
llSuccess = .F.
// 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.
loSsh1 = createobject("CkSsh")
loSsh2 = createobject("CkSsh")
loSsh3 = createobject("CkSsh")
lnPort = 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.
lcPassword = "mySshPassword"
llSuccess = loSsh1.Connect("ssh-server1.example.com",lnPort)
if (llSuccess = .F.) then
? loSsh1.LastErrorText
release loSsh1
release loSsh2
release loSsh3
return
endif
llSuccess = loSsh1.AuthenticatePw("mySshLogin",lcPassword)
if (llSuccess = .F.) then
? loSsh1.LastErrorText
release loSsh1
release loSsh2
release loSsh3
return
endif
llSuccess = loSsh2.Connect("ssh-server2.example.com",lnPort)
if (llSuccess = .F.) then
? loSsh2.LastErrorText
release loSsh1
release loSsh2
release loSsh3
return
endif
llSuccess = loSsh2.AuthenticatePw("mySshLogin",lcPassword)
if (llSuccess = .F.) then
? loSsh2.LastErrorText
release loSsh1
release loSsh2
release loSsh3
return
endif
llSuccess = loSsh3.Connect("ssh-server3.example.com",lnPort)
if (llSuccess = .F.) then
? loSsh3.LastErrorText
release loSsh1
release loSsh2
release loSsh3
return
endif
llSuccess = loSsh3.AuthenticatePw("mySshLogin",lcPassword)
if (llSuccess = .F.) then
? loSsh3.LastErrorText
release loSsh1
release loSsh2
release loSsh3
return
endif
// This command sleeps for 5 seconds and then prints the system date/time, making the parallel
// execution easy to observe.
lcCmd = "sleep 5; date"
// Start the command on each server. QuickCmdSend returns immediately.
lnChannel1 = loSsh1.QuickCmdSend(lcCmd)
if (lnChannel1 < 0) then
? loSsh1.LastErrorText
release loSsh1
release loSsh2
release loSsh3
return
endif
lnChannel2 = loSsh2.QuickCmdSend(lcCmd)
if (lnChannel2 < 0) then
? loSsh2.LastErrorText
release loSsh1
release loSsh2
release loSsh3
return
endif
lnChannel3 = loSsh3.QuickCmdSend(lcCmd)
if (lnChannel3 < 0) then
? loSsh3.LastErrorText
release loSsh1
release loSsh2
release loSsh3
return
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.)
lnPollTimeoutMs = 50
lnNumFinished = 0
llFinished1 = .F.
llFinished2 = .F.
llFinished3 = .F.
do while lnNumFinished < 3
if (not llFinished1) then
lnCh1 = loSsh1.QuickCmdCheck(lnPollTimeoutMs)
if (lnCh1 = -2) then
? loSsh1.LastErrorText
release loSsh1
release loSsh2
release loSsh3
return
endif
if (lnCh1 >= 0) then
? "---- ssh1 channel " + str(lnCh1) + " finished ----"
lcOut1 = loSsh1.GetReceivedText(lnCh1,"utf-8")
if (loSsh1.LastMethodSuccess = .F.) then
? loSsh1.LastErrorText
release loSsh1
release loSsh2
release loSsh3
return
endif
? lcOut1
llFinished1 = .T.
lnNumFinished = lnNumFinished + 1
endif
endif
if (not llFinished2) then
lnCh2 = loSsh2.QuickCmdCheck(lnPollTimeoutMs)
if (lnCh2 = -2) then
? loSsh2.LastErrorText
release loSsh1
release loSsh2
release loSsh3
return
endif
if (lnCh2 >= 0) then
? "---- ssh2 channel " + str(lnCh2) + " finished ----"
lcOut2 = loSsh2.GetReceivedText(lnCh2,"utf-8")
if (loSsh2.LastMethodSuccess = .F.) then
? loSsh2.LastErrorText
release loSsh1
release loSsh2
release loSsh3
return
endif
? lcOut2
llFinished2 = .T.
lnNumFinished = lnNumFinished + 1
endif
endif
if (not llFinished3) then
lnCh3 = loSsh3.QuickCmdCheck(lnPollTimeoutMs)
if (lnCh3 = -2) then
? loSsh3.LastErrorText
release loSsh1
release loSsh2
release loSsh3
return
endif
if (lnCh3 >= 0) then
? "---- ssh3 channel " + str(lnCh3) + " finished ----"
lcOut3 = loSsh3.GetReceivedText(lnCh3,"utf-8")
if (loSsh3.LastMethodSuccess = .F.) then
? loSsh3.LastErrorText
release loSsh1
release loSsh2
release loSsh3
return
endif
? lcOut3
llFinished3 = .T.
lnNumFinished = lnNumFinished + 1
endif
endif
enddo
loSsh1.Disconnect()
loSsh2.Disconnect()
loSsh3.Disconnect()
release loSsh1
release loSsh2
release loSsh3