Android™
Android™
Disconnect from an SSH Server
See more SSH Examples
Demonstrates the Chilkat Ssh.Disconnect method, which closes the current SSH transport. It takes no arguments, is harmless to call when nothing is connected, and may be called repeatedly.
Background: Disconnecting tears down the transport and every channel opened on it; afterward
IsConnected is false and NumOpenChannels is 0. Bytes already buffered for a former channel can still be read after disconnect, which is useful when you want to drain a command's final output before releasing the session.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;
// Demonstrates the Ssh.Disconnect method, which closes the current SSH transport. It takes no
// arguments, is safe to call when no connection exists, and may be called repeatedly.
CkSsh ssh = new CkSsh();
// Connect to the SSH server. Port 22 is the usual SSH port.
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;
}
// ... use the SSH connection ...
// Close the SSH connection.
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."
}
}