Visual FoxPro
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loSsh
LOCAL lnSshPort
LOCAL lcPassword
LOCAL lnChannelNum
LOCAL lnExitStatus
lnSuccess = 0
* 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 1.
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
lnChannelNum = loSsh.OpenSessionChannel()
IF (lnChannelNum < 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
lnSuccess = loSsh.SendReqExec(lnChannelNum,"grep -q root /etc/passwd")
IF (lnSuccess = 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
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.
loSsh.ReadTimeoutMs = 15000
lnSuccess = loSsh.ChannelReceiveToClose(lnChannelNum)
IF (lnSuccess = 0) THEN
? loSsh.LastErrorText
RELEASE loSsh
CANCEL
ENDIF
* Retrieve the exit status immediately, before calling unrelated SSH methods or releasing the
* channel, because the completed channel record can be discarded.
IF (loSsh.ChannelReceivedExitStatus(lnChannelNum)) THEN
lnExitStatus = loSsh.GetChannelExitStatus(lnChannelNum)
? "Exit status: " + STR(lnExitStatus)
* By convention, 0 means the remote command succeeded.
IF (lnExitStatus = 0) THEN
? "The remote command succeeded."
ELSE
? "The remote command reported a failure."
ENDIF
ENDIF
loSsh.Disconnect()
RELEASE loSsh