AutoIt
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; 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.
$oSsh = ObjCreate("Chilkat.Ssh")
Local $iSshPort = 22
$bSuccess = $oSsh.Connect("ssh.example.com",$iSshPort)
If ($bSuccess = False) Then
ConsoleWrite($oSsh.LastErrorText & @CRLF)
Exit
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.
Local $sPassword = "mySshPassword"
$bSuccess = $oSsh.AuthenticatePw("mySshLogin",$sPassword)
If ($bSuccess = False) Then
ConsoleWrite($oSsh.LastErrorText & @CRLF)
Exit
EndIf
; Start two commands that will finish at different times.
Local $iChannel1 = $oSsh.QuickCmdSend("sleep 2; echo first done")
If ($iChannel1 < 0) Then
ConsoleWrite($oSsh.LastErrorText & @CRLF)
Exit
EndIf
Local $iChannel2 = $oSsh.QuickCmdSend("echo second done")
If ($iChannel2 < 0) Then
ConsoleWrite($oSsh.LastErrorText & @CRLF)
Exit
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).
Local $iPollTimeoutMs = 5000
Local $iCompletedChannel = $oSsh.QuickCmdCheck($iPollTimeoutMs)
While $iCompletedChannel <> -2
If ($iCompletedChannel >= 0) Then
Local $sOutput = $oSsh.GetReceivedText($iCompletedChannel,"utf-8")
If ($oSsh.LastMethodSuccess = False) Then
ConsoleWrite($oSsh.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("Channel " & $iCompletedChannel & " finished: " & $sOutput & @CRLF)
EndIf
If ($iCompletedChannel = -1) Then
ConsoleWrite("Still waiting for a command to finish..." & @CRLF)
EndIf
$iCompletedChannel = $oSsh.QuickCmdCheck($iPollTimeoutMs)
Wend
ConsoleWrite("All quick commands have been collected." & @CRLF)
$oSsh.Disconnect