Sample code for 30+ languages & platforms
Zig

Authenticate an SSH Tunnel with a Private Key

See more POP3 Examples

Demonstrates the Chilkat MailMan.SshAuthenticatePk method, which authenticates with the SSH server using public-key authentication after an SSH tunnel has been opened. The first argument is the SSH account name and the second is the SSH private key (an SshKey object). This example opens a tunnel, loads an OpenSSH private key, authenticates with it, and runs a POP3 operation through the tunnel.

Background: SSH public-key authentication replaces a password with a key pair: the public key is installed on the SSH server, and the client proves it holds the matching private key without ever transmitting a secret. This is the preferred method for automated systems — there is no password to embed or rotate, and the key can be revoked server-side. Chilkat reads the key with an SshKey object, which understands common formats such as OpenSSH and PuTTY.

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 MailMan.SshAuthenticatePk method, which authenticates with the SSH server
    // using public-key authentication after an SSH tunnel has been opened.  The 1st argument is
    // the SSH account name and the 2nd is the SSH private key.

    const mailman = try chilkat.MailMan.init();
    defer mailman.deinit();

    // Configure the POP3 server connection.  These connections will travel through the tunnel.
    mailman.setMailHost("pop.example.com");
    mailman.setMailPort(995);
    mailman.setPopSsl(true);
    mailman.setPopUsername("user@example.com");
    mailman.setPopPassword("myPassword");

    // Open the SSH tunnel first.
    mailman.sshOpenTunnel("ssh.example.com", 22) catch {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
        return;
    };

    // Load the SSH private key from an OpenSSH-format key file.
    const ssh_key = try chilkat.SshKey.init();
    defer ssh_key.deinit();
    const key_text = ssh_key.loadText(alloc, "qa_data/ssh/id_rsa") catch {
        std.debug.print("{s}\n", .{try ssh_key.getLastErrorText(alloc)});
        return;
    };

    ssh_key.fromOpenSshPrivateKey(key_text) catch {
        std.debug.print("{s}\n", .{try ssh_key.getLastErrorText(alloc)});
        return;
    };

    // Authenticate with the SSH server using the private key.
    mailman.sshAuthenticatePk("sshUser", ssh_key) catch {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
        return;
    };

    // POP3 operations now travel through the SSH tunnel.
    const count = mailman.getMailboxCount();
    if (count < 0) {
        std.debug.print("Failed to get the mailbox count.\n", .{});
    } else {
        std.debug.print("Mailbox count: {d}\n", .{count});
    }

    mailman.sshCloseTunnel() catch {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
        return;
    };

    // Note: The path "qa_data/ssh/id_rsa" is a relative local filesystem path,
    // relative to the current working directory of the running application.
}