From 841c2f47edfb2967f8ad18e6c9568c8dd8be6298 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 18 Mar 2022 10:48:37 +0100 Subject: Added basic ssh client to run commands --- src/ssh.zig | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/ssh.zig (limited to 'src/ssh.zig') diff --git a/src/ssh.zig b/src/ssh.zig new file mode 100644 index 0000000..d8b1fda --- /dev/null +++ b/src/ssh.zig @@ -0,0 +1,78 @@ +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 = ssh.ssh_channel_read(channel, &buffer, buffer.len, 0); + while (nbytes > 0) : (nbytes = ssh.ssh_channel_read(channel, &buffer, buffer.len, 0)) { + var w = try writer.write(buffer[0..@intCast(usize, nbytes)]); + if (w != nbytes) { + std.os.exit(5); + } + } + _ = ssh.ssh_channel_send_eof(channel); + } else { + std.log.info("Error creating channel", .{}); + } + } + } +}; -- cgit v1.2.3