Sample code for 30+ languages & platforms
Zig

Disconnect All SSH Tunnel Clients

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.DisconnectAllClients method, which disconnects all active tunnel clients while leaving the listener and SSH connection available. The only argument (waitForThreads) selects whether to wait for the client threads to exit.

Background: This is the complement of StopAccepting: it clears out current clients but keeps accepting new ones, so the listener and SSH transport stay up. It is useful for forcibly resetting stuck or unwanted connections without tearing down the whole tunnel — for example, dropping all sessions before a maintenance action while leaving the tunnel ready to serve fresh connections immediately afterward.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // Demonstrates the SshTunnel.DisconnectAllClients method, which disconnects all active tunnel
    // clients while leaving the listener and SSH connection available.  The only argument
    // (waitForThreads) selects whether to wait for the client threads to exit.

    const tunnel = try chilkat.SshTunnel.init();
    defer tunnel.deinit();

    tunnel.setDestHostname("db.internal.example.com");
    tunnel.setDestPort(5432);

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

    const listen_port = 1080;
    tunnel.beginAccepting(listen_port) catch {
        std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
        return;
    };

    // ... clients connect and transfer data ...

    // Disconnect all current clients.  The listener keeps running and will accept new clients.
    const wait_for_threads = true;
    tunnel.disconnectAllClients(wait_for_threads) catch {
        std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("All current clients disconnected; still accepting new ones.\n", .{});

    const wait_for_thread_exit = true;
    tunnel.closeTunnel(wait_for_thread_exit) catch {
        std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
        return;
    };
}