Sample code for 30+ languages & platforms
Android™

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 Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = 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.

    CkSsh ssh = new CkSsh();

    int sshPort = 22;
    success = ssh.Connect("ssh.example.com",sshPort);
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    //  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.
    String password = "mySshPassword";

    success = ssh.AuthenticatePw("mySshLogin",password);
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    int channelNum = ssh.OpenSessionChannel();
    if (channelNum < 0) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    success = ssh.SendReqExec(channelNum,"grep -q root /etc/passwd");
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    //  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.put_ReadTimeoutMs(15000);

    success = ssh.ChannelReceiveToClose(channelNum);
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    //  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)) {
        int exitStatus = ssh.GetChannelExitStatus(channelNum);
        Log.i(TAG, "Exit status: " + String.valueOf(exitStatus));

        //  By convention, 0 means the remote command succeeded.
        if (exitStatus == 0) {
            Log.i(TAG, "The remote command succeeded.");
            }
        else {
            Log.i(TAG, "The remote command reported a failure.");
            }

        }

    ssh.Disconnect();

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}