Sample code for 30+ languages & platforms
Rust

Open a Forwarded Channel through an SSH Tunnel

See more Socket/SSL/TLS Examples

Demonstrates Socket.SshNewChannel, which opens a port-forwarded channel through an authenticated SSH tunnel to a destination host and port, populating a Socket with the connected channel.

Background. Set the ssl argument to true to establish TLS to the destination inside the SSH tunnel. The returned channel socket is then used to send and receive with the destination.

Chilkat Rust Downloads

Rust

let socket = chilkat::Socket::new();

// Connect to the SSH server and initialize an SSH tunneling session.  Port 22 is conventional but
// not required.
if socket.ssh_open_tunnel("ssh.example.com", 22).is_err() {
    println!("{}", socket.last_error_text());
    return;
}

// Authenticate the SSH session before opening forwarded channels.
// The SSH password should come from a secure source rather than being hard-coded.
let ssh_password = "mySshPassword".to_string();
if socket.ssh_authenticate_pw("sshUser", &ssh_password).is_err() {
    println!("{}", socket.last_error_text());
    return;
}

// Open a port-forwarded channel through the authenticated SSH tunnel to the destination host and
// port, populating a Socket with the connected channel.  Set the 3rd argument to true to establish
// TLS to the destination inside the tunnel.
let b_tls = false;
let max_wait_ms = 20000;
let channel = chilkat::Socket::new();
if socket.ssh_new_channel("db.internal", 5432, b_tls, max_wait_ms, &channel).is_err() {
    println!("{}", socket.last_error_text());
    return;
}

// Use the channel socket to send and receive with the destination through the tunnel.
println!("Forwarded channel opened to the destination.");