AutoIt
AutoIt
Get the Exit Status of a Remote SSH Command
See more SSH Examples
Demonstrates the Chilkat Ssh.GetChannelExitStatus method, which returns the remote process exit status for a channel. The only argument is the channel number. Call it only after ChannelReceivedExitStatus returns true.
Background: The exit status is how you learn whether a remote command actually succeeded — recall that a successful
exec request only means the request was accepted. By Unix convention 0 means success and any nonzero value indicates a failure. Retrieve it promptly after the receive operation and before calling unrelated SSH methods or releasing the channel, since the completed channel record can be discarded.Chilkat AutoIt Downloads
Local $bSuccess = False
; Demonstrates the Ssh.GetChannelExitStatus method, which returns the remote process exit
; status for a channel. The only argument is the channel number. Call it only after
; ChannelReceivedExitStatus returns True.
$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
Local $iChannelNum = $oSsh.OpenSessionChannel()
If ($iChannelNum < 0) Then
ConsoleWrite($oSsh.LastErrorText & @CRLF)
Exit
EndIf
$bSuccess = $oSsh.SendReqExec($iChannelNum,"grep -q root /etc/passwd")
If ($bSuccess = False) Then
ConsoleWrite($oSsh.LastErrorText & @CRLF)
Exit
EndIf
; IMPORTANT: Set a read timeout. ReadTimeoutMs defaults to 0, which means no limit -- without
; it, this call waits forever if the server never sends channel close.
$oSsh.ReadTimeoutMs = 15000
$bSuccess = $oSsh.ChannelReceiveToClose($iChannelNum)
If ($bSuccess = False) Then
ConsoleWrite($oSsh.LastErrorText & @CRLF)
Exit
EndIf
; Retrieve the exit status immediately, before calling unrelated SSH methods or releasing the
; channel, because the completed channel record can be discarded.
If ($oSsh.ChannelReceivedExitStatus($iChannelNum)) Then
Local $iExitStatus = $oSsh.GetChannelExitStatus($iChannelNum)
ConsoleWrite("Exit status: " & $iExitStatus & @CRLF)
; By convention, 0 means the remote command succeeded.
If ($iExitStatus = 0) Then
ConsoleWrite("The remote command succeeded." & @CRLF)
Else
ConsoleWrite("The remote command reported a failure." & @CRLF)
EndIf
EndIf
$oSsh.Disconnect