Rust
Rust
Route Socket Connections through an SSH Object
See more Socket/SSL/TLS Examples
Demonstrates Socket.UseSsh, which configures a socket to route subsequent Connect calls through an already connected and authenticated Ssh object using SSH port forwarding.
Background. After UseSsh, the destination host and port passed to Connect are reached through the SSH tunnel rather than by a direct TCP connection. This is one of several SSH-tunneling approaches the Socket class supports.
Chilkat Rust Downloads
let ssh = chilkat::Ssh::new();
// Connect and authenticate a standalone SSH object.
if ssh.connect("ssh.example.com", 22).is_err() {
println!("{}", ssh.last_error_text());
return;
}
// The SSH password should come from a secure source rather than being hard-coded.
let ssh_password = "mySshPassword".to_string();
if ssh.authenticate_pw("sshUser", &ssh_password).is_err() {
println!("{}", ssh.last_error_text());
return;
}
let socket = chilkat::Socket::new();
// Route subsequent Connect calls through the already connected and authenticated SSH object. The
// destination is reached by SSH port forwarding rather than by a direct TCP connection.
if socket.use_ssh(&ssh).is_err() {
println!("{}", socket.last_error_text());
return;
}
// Connect to the destination through the SSH tunnel.
let b_tls = false;
let max_wait_ms = 5000;
if socket.connect("db.internal", 5432, b_tls, max_wait_ms).is_err() {
println!("{}", socket.last_error_text());
return;
}
println!("Connected to the destination through the SSH tunnel.");