Sample code for 30+ languages & platforms
Rust

Set a TTY Mode for an SSH PTY Request

See more SSH Examples

Demonstrates the Chilkat Ssh.SetTtyMode method, which adds or updates one TTY mode to be included in a later SendReqPty request. The first argument is the mode name and the second is its integer value. Call it once per mode, before requesting the PTY.

Background: TTY modes are the terminal settings a PTY is created with — the same knobs stty exposes. The classic use is SetTtyMode("ECHO", 0), which stops the terminal from echoing input; that is how a password prompt keeps typed characters off the screen. Modes accumulate on the object and are sent with the next PTY request.

Chilkat Rust Downloads

Rust

// Demonstrates the Ssh.SetTtyMode method, which adds or updates one TTY mode to be included in
// a later SendReqPty request.  The 1st argument is the mode name and the 2nd is its integer
// value.  Call it once per mode, before SendReqPty.

let ssh = chilkat::Ssh::new();

let ssh_port = 22;
if ssh.connect("ssh.example.com", ssh_port).is_err() {
    println!("{}", ssh.last_error_text());
    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.
let password = "mySshPassword".to_string();

if ssh.authenticate_pw("mySshLogin", &password).is_err() {
    println!("{}", ssh.last_error_text());
    return;
}

// Open a session channel.  A negative return value indicates failure.
let channel_num = ssh.open_session_channel();
if channel_num < 0 {
    println!("{}", ssh.last_error_text());
    return;
}

// Request that the allocated terminal not echo input.
if ssh.set_tty_mode("ECHO", 0).is_err() {
    println!("{}", ssh.last_error_text());
    return;
}

// The modes set above are included in the PTY request.
if ssh.send_req_pty(channel_num, "xterm", 80, 24, 0, 0).is_err() {
    println!("{}", ssh.last_error_text());
    return;
}

if ssh.send_req_shell(channel_num).is_err() {
    println!("{}", ssh.last_error_text());
    return;
}

ssh.disconnect();