Zig
Zig
Authenticate an SSH Tunnel with a Private Key
See more Socket/SSL/TLS Examples
Demonstrates Socket.SshAuthenticatePk, which authenticates an SSH tunnel session using a private key loaded into an SshKey object. Call SshOpenTunnel first.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The SSH server must have the corresponding public key authorized for the account. If the private key is encrypted, set the SshKey.Password property before loading it.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const socket = try chilkat.Socket.init();
defer socket.deinit();
socket.sshOpenTunnel("ssh.example.com", 22) catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
return;
};
// Load the SSH private key. The file path is relative to the application's current working
// directory. If the key is encrypted, set the SshKey.Password property before calling
// FromOpenSshPrivateKey.
const ssh_key = try chilkat.SshKey.init();
defer ssh_key.deinit();
const key_text = ssh_key.loadText(alloc, "qa_data/id_ed25519") 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 the SSH tunnel session using public-key authentication. The SSH server must have the
// corresponding public key authorized for the account.
socket.sshAuthenticatePk("sshUser", ssh_key) catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
return;
};
std.debug.print("SSH public-key authentication succeeded.\n", .{});
}