Visual FoxPro
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loSsh
LOCAL lnSshPort
LOCAL lcPassword
LOCAL lnChannel1
LOCAL lnChannel2
LOCAL lnPollTimeoutMs
LOCAL lnCompletedChannel
LOCAL lcOutput
lnSuccess = 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.
loSsh = CreateObject('Chilkat.Ssh')
lnSshPort = 22
lnSuccess = loSsh.Connect("ssh.example.com",lnSshPort)
IF (lnSuccess = 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
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.
lcPassword = "mySshPassword"
lnSuccess = loSsh.AuthenticatePw("mySshLogin",lcPassword)
IF (lnSuccess = 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
* Start two commands that will finish at different times.
lnChannel1 = loSsh.QuickCmdSend("sleep 2; echo first done")
IF (lnChannel1 < 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
lnChannel2 = loSsh.QuickCmdSend("echo second done")
IF (lnChannel2 < 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
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).
lnPollTimeoutMs = 5000
lnCompletedChannel = loSsh.QuickCmdCheck(lnPollTimeoutMs)
DO WHILE lnCompletedChannel <> -2
IF (lnCompletedChannel >= 0) THEN
lcOutput = loSsh.GetReceivedText(lnCompletedChannel,"utf-8")
IF (loSsh.LastMethodSuccess = 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
? "Channel " + STR(lnCompletedChannel) + " finished: " + lcOutput
ENDIF
IF (lnCompletedChannel = -1) THEN
? "Still waiting for a command to finish..."
ENDIF
lnCompletedChannel = loSsh.QuickCmdCheck(lnPollTimeoutMs)
ENDDO
? "All quick commands have been collected."
loSsh.Disconnect()
RELEASE loSsh