PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkSsh.pb"
Procedure ChilkatExample()
success.i = 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.
ssh.i = CkSsh::ckCreate()
If ssh.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
sshPort.i = 22
success = CkSsh::ckConnect(ssh,"ssh.example.com",sshPort)
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
; 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.
password.s = "mySshPassword"
success = CkSsh::ckAuthenticatePw(ssh,"mySshLogin",password)
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
; Start two commands that will finish at different times.
channel1.i = CkSsh::ckQuickCmdSend(ssh,"sleep 2; echo first done")
If channel1 < 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
channel2.i = CkSsh::ckQuickCmdSend(ssh,"echo second done")
If channel2 < 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
; 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).
pollTimeoutMs.i = 5000
completedChannel.i = CkSsh::ckQuickCmdCheck(ssh,pollTimeoutMs)
While completedChannel <> -2
If completedChannel >= 0
output.s = CkSsh::ckGetReceivedText(ssh,completedChannel,"utf-8")
If CkSsh::ckLastMethodSuccess(ssh) = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
Debug "Channel " + Str(completedChannel) + " finished: " + output
EndIf
If completedChannel = -1
Debug "Still waiting for a command to finish..."
EndIf
completedChannel = CkSsh::ckQuickCmdCheck(ssh,pollTimeoutMs)
Wend
Debug "All quick commands have been collected."
CkSsh::ckDisconnect(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndProcedure