Sample code for 30+ languages & platforms
Android™

Clear Accumulated SSH TTY Modes

See more SSH Examples

Demonstrates the Chilkat Ssh.ClearTtyModes method, which clears all TTY modes previously added with SetTtyMode. It takes no arguments. The next SendReqPty will not include the cleared modes.

Background: Because TTY modes accumulate on the Ssh object, they would otherwise carry over into every later PTY request on that object. ClearTtyModes resets that state — useful when one Ssh object opens several channels and each needs different terminal settings, or when you want a PTY created with the server's defaults.

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.ClearTtyModes method, which clears all TTY modes previously added with
    //  SetTtyMode.  It takes no arguments.

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

    //  Add a TTY mode, then discard it before requesting the PTY.
    success = ssh.SetTtyMode("ECHO",0);
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    //  Clear the accumulated TTY modes.  The next SendReqPty will not include them.
    ssh.ClearTtyModes();

    //  Open a session channel.  A negative return value indicates failure.
    int channelNum = ssh.OpenSessionChannel();
    if (channelNum < 0) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    //  This PTY request carries no custom TTY modes.
    success = ssh.SendReqPty(channelNum,"xterm",80,24,0,0);
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

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