Zig
Zig
Route Socket Connections through an SSH Object
See more Socket/SSL/TLS Examples
Demonstrates Socket.UseSsh, which configures a socket to route subsequent Connect calls through an already connected and authenticated Ssh object using SSH port forwarding.
Background. After UseSsh, the destination host and port passed to Connect are reached through the SSH tunnel rather than by a direct TCP connection. This is one of several SSH-tunneling approaches the Socket class supports.
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 ssh = try chilkat.Ssh.init();
defer ssh.deinit();
// Connect and authenticate a standalone SSH object.
ssh.connect("ssh.example.com", 22) catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
// The SSH password should come from a secure source rather than being hard-coded.
const ssh_password = "mySshPassword";
ssh.authenticatePw("sshUser", ssh_password) catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
const socket = try chilkat.Socket.init();
defer socket.deinit();
// Route subsequent Connect calls through the already connected and authenticated SSH object. The
// destination is reached by SSH port forwarding rather than by a direct TCP connection.
socket.useSsh(ssh) catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
return;
};
// Connect to the destination through the SSH tunnel.
const b_tls = false;
const max_wait_ms = 5000;
socket.connect("db.internal", 5432, b_tls, max_wait_ms) catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
return;
};
std.debug.print("Connected to the destination through the SSH tunnel.\n", .{});
}