Sample code for 30+ languages & platforms
Zig

Start SSH Tunnel Keyboard-Interactive Authentication

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.StartKeyboardAuth method, which begins keyboard-interactive authentication. The only argument is the login name. It returns XML describing the server's prompts, which are answered with ContinueKeyboardAuth.

Background: Keyboard-interactive is the prompt-driven authentication method: the server sends one or more questions — a password, a one-time code, a security question — and the client answers each. This is how a tunnel supports two-factor and other challenge-response schemes. The returned XML says exactly what to ask and whether to mask the input.

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.StartKeyboardAuth method, which begins keyboard-interactive
    // authentication.  The only argument is the login name.  It returns XML describing the server's
    // prompts, which are answered with ContinueKeyboardAuth.

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

    // Begin keyboard-interactive authentication.  The returned XML describes the server's prompts.
    const info_request_xml = tunnel.startKeyboardAuth(alloc, "mySshLogin") catch {
        std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{info_request_xml});

    // Answer the prompt(s) with ContinueKeyboardAuth, then call BeginAccepting.

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