Sample code for 30+ languages & platforms
Android™

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 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.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.

    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;
        }

    //  Start two commands that will finish at different times.
    int channel1 = ssh.QuickCmdSend("sleep 2; echo first done");
    if (channel1 < 0) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    int channel2 = ssh.QuickCmdSend("echo second done");
    if (channel2 < 0) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    //  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).
    int pollTimeoutMs = 5000;
    int completedChannel = ssh.QuickCmdCheck(pollTimeoutMs);
    while (completedChannel != -2) {
        if (completedChannel >= 0) {
            String output = ssh.getReceivedText(completedChannel,"utf-8");
            if (ssh.get_LastMethodSuccess() == false) {
                Log.i(TAG, ssh.lastErrorText());
                return;
                }

            Log.i(TAG, "Channel " + String.valueOf(completedChannel) + " finished: " + output);
            }

        if (completedChannel == -1) {
            Log.i(TAG, "Still waiting for a command to finish...");
            }

        completedChannel = ssh.QuickCmdCheck(pollTimeoutMs);
        }

    Log.i(TAG, "All quick commands have been collected.");

    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."
  }
}