PowerBuilder
PowerBuilder
Collect Completed SSH Commands with QuickCmdCheck
See more SSH Examples
Demonstrates the Chilkat Ssh.QuickCmdCheck method, which waits for any command started by QuickCmdSend to complete. The only argument is the poll timeout in milliseconds; 0 performs a nonblocking check. It returns the channel number of a completed command, -1 when commands are still pending but none finished before the timeout, or -2 when no quick commands remain or an error occurred.
Background: This is the collection half of the concurrent pattern. Each completed channel is reported only once, so looping until
-2 drains every outstanding command exactly once. Distinguish the two negative values carefully: -1 simply means "still working, ask again," while -2 means there is nothing left to wait for — or that the connection failed, so check LastErrorText when the distinction matters. Manually opened exec channels are not reported here.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_SshPort
string ls_Password
integer li_Channel1
integer li_Channel2
integer li_PollTimeoutMs
integer li_CompletedChannel
string ls_Output
li_Success = 0
// Demonstrates the Ssh.QuickCmdCheck method, which waits for any command started by
// QuickCmdSend to complete. The only argument is the poll timeout in milliseconds; 0 performs
// a nonblocking check.
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_SshPort = 22
li_Success = loo_Ssh.Connect("ssh.example.com",li_SshPort)
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
// Start two commands that will finish at different times.
li_Channel1 = loo_Ssh.QuickCmdSend("sleep 2; echo first done")
if li_Channel1 < 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
li_Channel2 = loo_Ssh.QuickCmdSend("echo second done")
if li_Channel2 < 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
// Collect the results as they complete. The return value is the channel number of a completed
// command, -1 if commands are still pending but none finished before the timeout, or -2 when
// no quick commands remain (or an error occurred).
li_PollTimeoutMs = 5000
li_CompletedChannel = loo_Ssh.QuickCmdCheck(li_PollTimeoutMs)
do while li_CompletedChannel <> -2
if li_CompletedChannel >= 0 then
ls_Output = loo_Ssh.GetReceivedText(li_CompletedChannel,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
Write-Debug "Channel " + string(li_CompletedChannel) + " finished: " + ls_Output
end if
if li_CompletedChannel = -1 then
Write-Debug "Still waiting for a command to finish..."
end if
li_CompletedChannel = loo_Ssh.QuickCmdCheck(li_PollTimeoutMs)
loop
Write-Debug "All quick commands have been collected."
loo_Ssh.Disconnect()
destroy loo_Ssh