PowerBuilder
PowerBuilder
SSH Parallel Remote Commands on a Single Server
See more SSH Examples
Demonstrates running several commands in parallel on one SSH server and collecting each command's output as it finishes. QuickCmdSend starts each command and returns immediately; QuickCmdCheck reports them as they complete.
Background: Because SSH multiplexes channels, several commands can run at once over one authenticated connection — far faster than running them one after another when each involves waiting. Results arrive in completion order rather than the order the commands were started, so the loop keys off the returned channel number. Distinguish the two negative returns carefully:
-1 means "still working, ask again," while -2 means nothing remains to collect or the connection failed.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_Port
string ls_Password
integer li_Channel1
integer li_Channel2
integer li_Channel3
integer li_PollTimeoutMs
integer li_NumFinished
integer li_Channel
string ls_CmdOutput
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates running several commands in parallel on a single SSH server and collecting each
// command's output as it finishes.
loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")
if li_rc < 0 then
destroy loo_Ssh
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Port = 22
li_Success = loo_Ssh.Connect("ssh.example.com",li_Port)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
// 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.
ls_Password = "mySshPassword"
li_Success = loo_Ssh.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
// QuickCmdSend starts a command and returns immediately, so all three run concurrently, each on
// its own session channel.
li_Channel1 = loo_Ssh.QuickCmdSend("df")
if li_Channel1 < 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
li_Channel2 = loo_Ssh.QuickCmdSend("date")
if li_Channel2 < 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
li_Channel3 = loo_Ssh.QuickCmdSend("echo hello world")
if li_Channel3 < 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
// Collect the results. QuickCmdCheck returns the channel number of a completed command,
// -1 when commands are still pending but none finished within the timeout, or -2 when nothing
// remains to be checked (or an error occurred).
li_PollTimeoutMs = 50
li_NumFinished = 0
do while li_NumFinished < 3
li_Channel = loo_Ssh.QuickCmdCheck(li_PollTimeoutMs)
if li_Channel = -2 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
if li_Channel >= 0 then
Write-Debug "---- channel " + string(li_Channel) + " finished ----"
ls_CmdOutput = loo_Ssh.GetReceivedText(li_Channel,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
Write-Debug ls_CmdOutput
li_NumFinished = li_NumFinished + 1
end if
loop
loo_Ssh.Disconnect()
destroy loo_Ssh