const std = @import("std"); const ssh = @cImport(@cInclude("libssh/libssh.h")); pub const Client = struct { session: ?ssh.ssh_session, pub fn deinit(self: *Client) void { if (self.session) |session| { if (ssh.ssh_is_connected(session) == 1) { ssh.ssh_disconnect(session); } ssh.ssh_free(session); self.session = null; } } pub fn init(hostname: [*]const u8) !Client { var client = Client{ .session = ssh.ssh_new(), }; var port: i64 = 22; if (client.session) |session| { _ = ssh.ssh_options_set(session, ssh.SSH_OPTIONS_HOST, hostname); _ = ssh.ssh_options_set(session, ssh.SSH_OPTIONS_PORT, &port); //var verbosity = ssh.SSH_LOG_PROTOCOL; //_ = ssh.ssh_options_set(session, ssh.SSH_OPTIONS_LOG_VERBOSITY, &verbosity); var rc = ssh.ssh_connect(session); if (rc != ssh.SSH_OK) { std.log.info("Error connecting to localhost: {s}\n", .{ssh.ssh_get_error(session)}); std.os.exit(2); } //if (ssh.verify_knownhost(session) < 0) { // std.log.info("knownhost verification error", .{}); // std.os.exit(3); //} if (ssh.ssh_userauth_publickey_auto(session, null, null) == ssh.SSH_AUTH_ERROR) { std.log.info("Error authenticating with public key: {s}\n", .{ssh.ssh_get_error(session)}); std.os.exit(3); } } else { std.log.info("failed to initialise ssh session\n", .{}); std.os.exit(1); } return client; } pub fn run(self: *Client, cmd: [*]const u8, writer: anytype) !void { if (self.session) |session| { if (ssh.ssh_channel_new(session)) |channel| { defer ssh.ssh_channel_free(channel); defer _ = ssh.ssh_channel_close(channel); if (ssh.ssh_channel_open_session(channel) != ssh.SSH_OK) { std.log.info("Error opening channel session: {s}\n", .{ssh.ssh_get_error(session)}); std.os.exit(3); } if (ssh.ssh_channel_request_exec(channel, cmd) != ssh.SSH_OK) { std.log.info("Error executing command: {s}\n", .{ssh.ssh_get_error(session)}); std.os.exit(4); } var buffer: [256]u8 = undefined; var nbytes: usize = @intCast(ssh.ssh_channel_read(channel, &buffer, buffer.len, 0)); while (nbytes > 0) : (nbytes = @intCast(ssh.ssh_channel_read(channel, &buffer, buffer.len, 0))) { var w = try writer.write(buffer[0..nbytes]); if (w != nbytes) { std.os.exit(5); } } _ = ssh.ssh_channel_send_eof(channel); } else { std.log.info("Error creating channel", .{}); } } } };