Zig
Zig
Request SSH X11 Forwarding
See more SSH Examples
Demonstrates the Chilkat Ssh.SendReqX11Forwarding method, which sends an X11-forwarding request on a session channel. The arguments are the channel number, whether forwarding is limited to a single connection, and the X11 authentication protocol, cookie, and screen number.
Background: X11 forwarding lets a graphical program running on the remote host display its window on your local machine, tunneled over SSH. This is a low-level method that only sends the protocol request — your application is still responsible for the local X server side of the arrangement. The authentication cookie guards the forwarded display, so it should be a freshly generated value rather than a fixed constant.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// Demonstrates the Ssh.SendReqX11Forwarding method, which sends an SSH X11-forwarding request
// on a session channel. The 1st argument is the channel number, the 2nd limits forwarding to a
// single connection, and the 3rd through 5th supply the X11 authentication protocol, cookie,
// and screen number.
const ssh = try chilkat.Ssh.init();
defer ssh.deinit();
const ssh_port = 22;
ssh.connect("ssh.example.com", ssh_port) catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
const password = "mySshPassword";
ssh.authenticatePw("mySshLogin", password) catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
// Open a session channel. A negative return value indicates failure.
const channel_num = ssh.openSessionChannel();
if (channel_num < 0) {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
}
// Request X11 forwarding. Named variables make each argument's meaning clear.
const single_connection = false;
const auth_prot = "MIT-MAGIC-COOKIE-1";
const auth_cookie = "d92f5e1a7c8b4306af17c2e5b9d43081";
const screen_num = 0;
ssh.sendReqX11Forwarding(channel_num, single_connection, auth_prot, auth_cookie, screen_num) catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
std.debug.print("X11 forwarding requested.\n", .{});
ssh.disconnect();
}