Zig
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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.
const tunnel = try chilkat.SshTunnel.init();
defer tunnel.deinit();
// The tunnel forwards accepted local connections to this destination through the SSH server.
tunnel.setDestHostname("db.internal.example.com");
tunnel.setDestPort(5432);
// Connect to the SSH server. This performs the TCP/proxy connection and SSH handshake, but
// does not authenticate yet.
const ssh_port = 22;
tunnel.connect("ssh.example.com", ssh_port) catch {
std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
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.
const password = "mySshPassword";
tunnel.authenticatePw("mySshLogin", password) catch {
std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
return;
};
// Begin accepting so there is live tunnel state to inspect.
const listen_port = 1080;
tunnel.beginAccepting(listen_port) catch {
std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
return;
};
// Get a diagnostic snapshot of the tunnel's current state as XML.
const state_xml = tunnel.getCurrentState(alloc) catch {
std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
return;
};
std.debug.print("{s}\n", .{state_xml});
const wait_for_thread_exit = true;
tunnel.closeTunnel(wait_for_thread_exit) catch {
std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
return;
};
}