Android™
Android™
SSH Parallel Remote Commands on a Single Server
See more SSH Examples
Demonstrates running several commands in parallel on one SSH server and collecting each command's output as it finishes. QuickCmdSend starts each command and returns immediately; QuickCmdCheck reports them as they complete.
Background: Because SSH multiplexes channels, several commands can run at once over one authenticated connection — far faster than running them one after another when each involves waiting. Results arrive in completion order rather than the order the commands were started, so the loop keys off the returned channel number. Distinguish the two negative returns carefully:
-1 means "still working, ask again," while -2 means nothing remains to collect or the connection failed.Chilkat Android™ Downloads
// 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;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates running several commands in parallel on a single SSH server and collecting each
// command's output as it finishes.
CkSsh ssh = new CkSsh();
int port = 22;
success = ssh.Connect("ssh.example.com",port);
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;
}
// QuickCmdSend starts a command and returns immediately, so all three run concurrently, each on
// its own session channel.
int channel1 = ssh.QuickCmdSend("df");
if (channel1 < 0) {
Log.i(TAG, ssh.lastErrorText());
return;
}
int channel2 = ssh.QuickCmdSend("date");
if (channel2 < 0) {
Log.i(TAG, ssh.lastErrorText());
return;
}
int channel3 = ssh.QuickCmdSend("echo hello world");
if (channel3 < 0) {
Log.i(TAG, ssh.lastErrorText());
return;
}
// Collect the results. QuickCmdCheck returns the channel number of a completed command,
// -1 when commands are still pending but none finished within the timeout, or -2 when nothing
// remains to be checked (or an error occurred).
int pollTimeoutMs = 50;
int numFinished = 0;
while (numFinished < 3) {
int channel = ssh.QuickCmdCheck(pollTimeoutMs);
if (channel == -2) {
Log.i(TAG, ssh.lastErrorText());
return;
}
if (channel >= 0) {
Log.i(TAG, "---- channel " + String.valueOf(channel) + " finished ----");
String cmdOutput = ssh.getReceivedText(channel,"utf-8");
if (ssh.get_LastMethodSuccess() == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
Log.i(TAG, cmdOutput);
numFinished = numFinished + 1;
}
}
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."
}
}