Sample code for 30+ languages & platforms
Classic ASP

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 Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 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.

set ssh = Server.CreateObject("Chilkat.Ssh")

sshPort = 22
success = ssh.Connect("ssh.example.com",sshPort)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>"
    Response.End
End If

'  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 = "mySshPassword"

success = ssh.AuthenticatePw("mySshLogin",password)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>"
    Response.End
End If

channelNum = ssh.OpenSessionChannel()
If (channelNum < 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>"
    Response.End
End If

success = ssh.SendReqExec(channelNum,"grep -q root /etc/passwd")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>"
    Response.End
End If

'  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.
ssh.ReadTimeoutMs = 15000

success = ssh.ChannelReceiveToClose(channelNum)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>"
    Response.End
End If

'  Retrieve the exit status immediately, before calling unrelated SSH methods or releasing the
'  channel, because the completed channel record can be discarded.
If (ssh.ChannelReceivedExitStatus(channelNum)) Then
    exitStatus = ssh.GetChannelExitStatus(channelNum)
    Response.Write "<pre>" & Server.HTMLEncode( "Exit status: " & exitStatus) & "</pre>"

    '  By convention, 0 means the remote command succeeded.
    If (exitStatus = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( "The remote command succeeded.") & "</pre>"
    Else
        Response.Write "<pre>" & Server.HTMLEncode( "The remote command reported a failure.") & "</pre>"
    End If

End If

ssh.Disconnect 

%>
</body>
</html>