Rust
Rust
Get an SSH Tunnel's Current State (Diagnostics)
See more SSH Tunnel Examples
Demonstrates the Chilkat SshTunnel.GetCurrentState method, which returns an XML snapshot describing the tunnel manager, SSH channels, and current tunnel clients. It takes no arguments and is intended for diagnostics and monitoring.
Background: Because the tunnel forwards traffic on a background thread, there is no natural place in your code to observe what it is doing at a given moment.
GetCurrentState provides that visibility — byte counts, active channels, and connected clients — as structured XML suited to logging or a monitoring dashboard. It is a read-only snapshot and does not affect the tunnel.Chilkat Rust Downloads
// Demonstrates the SshTunnel.GetCurrentState method, which returns an XML snapshot describing the
// tunnel manager, SSH channels, and current tunnel clients. It takes no arguments and is
// intended for diagnostics and monitoring.
let tunnel = chilkat::SshTunnel::new();
// The tunnel forwards accepted local connections to this destination through the SSH server.
tunnel.set_dest_hostname("db.internal.example.com");
tunnel.set_dest_port(5432);
// Connect to the SSH server. This performs the TCP/proxy connection and SSH handshake, but
// does not authenticate yet.
let ssh_port = 22;
if tunnel.connect("ssh.example.com", ssh_port).is_err() {
println!("{}", tunnel.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 tunnel.authenticate_pw("mySshLogin", &password).is_err() {
println!("{}", tunnel.last_error_text());
return;
}
// Begin accepting so there is live tunnel state to inspect.
let listen_port = 1080;
if tunnel.begin_accepting(listen_port).is_err() {
println!("{}", tunnel.last_error_text());
return;
}
// Get a diagnostic snapshot of the tunnel's current state as XML.
let Ok(state_xml) = tunnel.get_current_state() else {
println!("{}", tunnel.last_error_text());
return;
};
println!("{}", state_xml);
let wait_for_thread_exit = true;
if tunnel.close_tunnel(wait_for_thread_exit).is_err() {
println!("{}", tunnel.last_error_text());
return;
}