Sample code for 30+ languages & platforms
Rust

Get the Type of an Active SSH Channel

See more SSH Examples

Demonstrates the Chilkat Ssh.GetChannelType method, which returns the type of the channel at a zero-based enumeration index. The only argument is the index — an enumeration position, not a channel number. Values include session, x11, forwarded-tcpip, and direct-tcpip.

Background: Paired with GetChannelNumber, this turns the channel collection into a readable inventory of what a connection is currently doing — which channels are running commands (session) versus tunneling TCP traffic (direct-tcpip). That is valuable for diagnostics and for application code that manages several channel kinds at once. An out-of-range index causes the string-returning method to report failure.

Chilkat Rust Downloads

Rust

// Demonstrates the Ssh.GetChannelType method, which returns the type of the channel at a
// zero-based enumeration index.  The only argument is the index, which is an enumeration
// position and not a channel number.  Values include "session" and "direct-tcpip".

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

let channel_num = ssh.open_session_channel();
if channel_num < 0 {
    println!("{}", ssh.last_error_text());
    return;
}

// Enumerate the active channels and report each one's number and type.
let n = ssh.num_open_channels();
for i in 0..n {
    let ch_num = ssh.get_channel_number(i);
    let Ok(ch_type) = ssh.get_channel_type(i) else {
        println!("{}", ssh.last_error_text());
        return;
    };

    println!("Channel {} is of type {}", ch_num, ch_type);
}

ssh.disconnect();