PowerBuilder
PowerBuilder
SSH Parallel Remote Commands on Multiple Servers
See more SSH Examples
Shows how to execute a command in parallel on multiple servers.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Ssh1
oleobject loo_Ssh2
oleobject loo_Ssh3
integer li_Port
string ls_Cmd
integer li_Ssh1Channel
integer li_Ssh2Channel
integer li_Ssh3Channel
integer li_PollTimeoutMs
integer li_NumFinished
integer li_Channel
integer li_Ssh1Finished
integer li_Ssh2Finished
integer li_Ssh3Finished
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Executing a command on multiple servers simultaneously is straightforward.
// It's just a matter of using one SSH object per server..
loo_Ssh1 = create oleobject
li_rc = loo_Ssh1.ConnectToNewObject("Chilkat.Ssh")
if li_rc < 0 then
destroy loo_Ssh1
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Ssh2 = create oleobject
li_rc = loo_Ssh2.ConnectToNewObject("Chilkat.Ssh")
loo_Ssh3 = create oleobject
li_rc = loo_Ssh3.ConnectToNewObject("Chilkat.Ssh")
li_Port = 22
li_Success = loo_Ssh1.Connect("ssh-server1.com",li_Port)
if li_Success <> 1 then
Write-Debug loo_Ssh1.LastErrorText
destroy loo_Ssh1
destroy loo_Ssh2
destroy loo_Ssh3
return
end if
// Authenticate using login/password:
li_Success = loo_Ssh1.AuthenticatePw("sshLogin1","sshPassword1")
if li_Success <> 1 then
Write-Debug loo_Ssh1.LastErrorText
destroy loo_Ssh1
destroy loo_Ssh2
destroy loo_Ssh3
return
end if
// Connect and authenticate with 2 more servers.
// For brevity, the success/failure won't be checked...
li_Success = loo_Ssh2.Connect("ssh-server2.com",li_Port)
li_Success = loo_Ssh2.AuthenticatePw("sshLogin2","sshPassword2")
li_Success = loo_Ssh3.Connect("ssh-server3.com",li_Port)
li_Success = loo_Ssh3.AuthenticatePw("sshLogin3","sshPassword3")
// Note: If we wanted, we could've used ConnectAsync and AuthenticatePwAsync
// to do the connecting and authenticating in parallel...
// The command to be run on each SSH server will sleep for 5 seconds,
// and then show the current system date/time.
ls_Cmd = "sleep 5; date"
// Start each command
li_Ssh1Channel = loo_Ssh1.QuickCmdSend(ls_Cmd)
if li_Ssh1Channel < 0 then
Write-Debug loo_Ssh1.LastErrorText
destroy loo_Ssh1
destroy loo_Ssh2
destroy loo_Ssh3
return
end if
// For brevity, we're not checking the return values here:
li_Ssh2Channel = loo_Ssh2.QuickCmdSend(ls_Cmd)
li_Ssh3Channel = loo_Ssh3.QuickCmdSend(ls_Cmd)
// OK, at this point the command is running simultaneously on each server.
// Now collect the results of each command.
li_PollTimeoutMs = 50
li_NumFinished = 0
// Note: You would rewrite this code to use arrays.
li_Ssh1Finished = 0
li_Ssh2Finished = 0
li_Ssh3Finished = 0
do while li_NumFinished < 3
// Check to see if anything has finished.
// QuickCmdCheck returns -1 if there are no errors and nothing else finished
// QuickCmdCheck returns -2 if there was an error (such as a lost connection)
// QuickCmdCheck returns a channel number if a channel finished.
if li_Ssh1Finished <> 1 then
li_Channel = loo_Ssh1.QuickCmdCheck(li_PollTimeoutMs)
if li_Channel = -2 then
Write-Debug loo_Ssh1.LastErrorText
destroy loo_Ssh1
destroy loo_Ssh2
destroy loo_Ssh3
return
end if
if li_Channel = li_Ssh1Channel then
Write-Debug "---- ssh1 channel " + string(li_Channel) + " finished ----"
Write-Debug loo_Ssh1.GetReceivedText(li_Channel,"ansi")
li_NumFinished = li_NumFinished + 1
li_Ssh1Finished = 1
end if
end if
if li_Ssh2Finished <> 1 then
li_Channel = loo_Ssh2.QuickCmdCheck(li_PollTimeoutMs)
if li_Channel = -2 then
Write-Debug loo_Ssh2.LastErrorText
destroy loo_Ssh1
destroy loo_Ssh2
destroy loo_Ssh3
return
end if
if li_Channel = li_Ssh2Channel then
Write-Debug "---- ssh2 channel " + string(li_Channel) + " finished ----"
Write-Debug loo_Ssh2.GetReceivedText(li_Channel,"ansi")
li_NumFinished = li_NumFinished + 1
li_Ssh2Finished = 1
end if
end if
if li_Ssh3Finished <> 1 then
li_Channel = loo_Ssh3.QuickCmdCheck(li_PollTimeoutMs)
if li_Channel = -2 then
Write-Debug loo_Ssh3.LastErrorText
destroy loo_Ssh1
destroy loo_Ssh2
destroy loo_Ssh3
return
end if
if li_Channel = li_Ssh3Channel then
Write-Debug "---- ssh3 channel " + string(li_Channel) + " finished ----"
Write-Debug loo_Ssh3.GetReceivedText(li_Channel,"ansi")
li_NumFinished = li_NumFinished + 1
li_Ssh3Finished = 1
end if
end if
loop
// --------------
// Sample output:
// ---- ssh2 channel 101 finished ----
// Fri Dec 23 00:25:48 UTC 2016
//
// ---- ssh3 channel 102 finished ----
// Thu Dec 22 18:25:12 CST 2016
//
// ---- ssh1 channel 100 finished ----
// Thu Dec 22 18:25:48 CST 2016
destroy loo_Ssh1
destroy loo_Ssh2
destroy loo_Ssh3