Zig
Zig
Use a PuTTY Key for SSH Authentication
See more SSH Examples
Demonstrates authenticating with an SSH server using a username and a PuTTY (.ppk) private key. The key is imported with FromPuttyPrivateKey — setting Password first if it is encrypted — and passed to AuthenticatePk.
Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.
Background: A PuTTY key can be used directly for authentication with no conversion step, which is convenient when the key was generated by PuTTYgen on Windows. Conceptually the private key takes the place of a password while the username identifies the account, and the corresponding public key must already be installed on the server. Convert to OpenSSH format only if some other tool in your pipeline requires PEM.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates authenticating with an SSH server using a username and a PuTTY (.ppk) private
// key. The private key serves as the credential; the username identifies the account on the
// server.
const ssh = try chilkat.Ssh.init();
defer ssh.deinit();
// LoadText is a convenience method that reads any text file into a string. It does not itself
// load the key.
const putty_key = try chilkat.SshKey.init();
defer putty_key.deinit();
const ppk_text = putty_key.loadText(alloc, "qa_data/ppk/putty_private_secret.ppk") catch {
std.debug.print("{s}\n", .{try putty_key.getLastErrorText(alloc)});
return;
};
// Set the password before importing an encrypted .ppk. This should come from a secure source
// rather than being hard-coded.
putty_key.setPassword("myKeyPassword");
putty_key.fromPuttyPrivateKey(ppk_text) catch {
std.debug.print("{s}\n", .{try putty_key.getLastErrorText(alloc)});
return;
};
const ssh_hostname = "ssh.example.com";
const ssh_port = 22;
ssh.connect(ssh_hostname, ssh_port) catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
// The corresponding public key must already be installed on the SSH server for the account.
ssh.authenticatePk("mySshLogin", putty_key) catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
std.debug.print("Connection and authentication with the SSH server completed.\n", .{});
ssh.disconnect();
}