Sample code for 30+ languages & platforms
Zig

Get the Type of an Active SSH Channel

See more SSH Examples

Demonstrates the Chilkat Ssh.GetChannelType method, which returns the type of the channel at a zero-based enumeration index. The only argument is the index — an enumeration position, not a channel number. Values include session, x11, forwarded-tcpip, and direct-tcpip.

Background: Paired with GetChannelNumber, this turns the channel collection into a readable inventory of what a connection is currently doing — which channels are running commands (session) versus tunneling TCP traffic (direct-tcpip). That is valuable for diagnostics and for application code that manages several channel kinds at once. An out-of-range index causes the string-returning method to report failure.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // Demonstrates the Ssh.GetChannelType method, which returns the type of the channel at a
    // zero-based enumeration index.  The only argument is the index, which is an enumeration
    // position and not a channel number.  Values include "session" and "direct-tcpip".

    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;
    };

    const channel_num = ssh.openSessionChannel();
    if (channel_num < 0) {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    }

    // Enumerate the active channels and report each one's number and type.
    const n = ssh.getNumOpenChannels();
    var i: i32 = 0;
    i = 0;
    while (i < n) : (i += 1) {
        const ch_num = ssh.getChannelNumber(i);
        const ch_type = ssh.getChannelType(alloc, i) catch {
            std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
            return;
        };

        std.debug.print("Channel {d} is of type {s}\n", .{ ch_num, ch_type });
    }

    ssh.disconnect();
}