Sample code for 30+ languages & platforms
Zig

SSH Commands to a Sophos Router Device Console

See more SSH Examples

Demonstrates establishing an SSH session with a Sophos XG router and sending commands in its device console session. The router presents a numbered main menu; the example selects the Device Console entry and then runs commands at the resulting console> prompt.

Note: QuickShell allocates a PTY, which is what a network device console expects. The device presents an interactive prompt, and each command's output is read up to the next prompt.

Background: Menu-driven appliances require the client to navigate the interface exactly as a human would — read the menu, send the keystroke that selects an entry, then wait for the next prompt. Because the prompt changes as you move between menu levels, the pattern you match on must change too, which is why this example matches the menu text first and console> afterward.

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();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // Demonstrates establishing an SSH session with a Sophos XG router and sending commands in its
    // device console session.
    //
    // Note: QuickShell allocates a PTY, which is what a device console expects.  Navigating the
    // device's menus is a matter of reading up to each prompt and sending the expected keystrokes.

    const ssh = try chilkat.Ssh.init();
    defer ssh.deinit();

    const port = 22;
    ssh.connect("172.16.16.16", 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.quickShell();
    if (channel_num < 0) {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    }

    // IMPORTANT: Set a read timeout before receiving until a match.  ReadTimeoutMs defaults to 0,
    // which means no limit -- without it, this call waits forever if the received data never
    // contains a match.
    ssh.setReadTimeoutMs(15000);

    const case_sensitive = true;

    // The router presents a numbered main menu ending with "Select Menu Number [0-7]: ".
    ssh.channelReceiveUntilMatch(channel_num, "Select Menu Number", "utf-8", case_sensitive) catch {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    };

    var cmd_output: [:0]const u8 = ssh.getReceivedText(alloc, channel_num, "utf-8") catch {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{cmd_output});

    // Choose menu item 4 (Device Console), exactly as if typing it in a terminal.
    ssh.channelSendString(channel_num, "4\n", "utf-8") catch {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    };

    // The device console prompt is "console>".
    ssh.channelReceiveUntilMatch(channel_num, "console>", "utf-8", case_sensitive) catch {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    };

    cmd_output = ssh.getReceivedText(alloc, channel_num, "utf-8") catch {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{cmd_output});

    // Send a console command and read its output up to the next prompt.
    ssh.channelSendString(channel_num, "dnslookup host google.com\n", "utf-8") catch {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    };

    ssh.channelReceiveUntilMatch(channel_num, "console>", "utf-8", case_sensitive) catch {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    };

    cmd_output = ssh.getReceivedText(alloc, channel_num, "utf-8") catch {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{cmd_output});

    // Additional commands follow the same pattern.
    ssh.channelSendString(channel_num, "dnslookup host microsoft.com\n", "utf-8") catch {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    };

    ssh.channelReceiveUntilMatch(channel_num, "console>", "utf-8", case_sensitive) catch {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    };

    cmd_output = ssh.getReceivedText(alloc, channel_num, "utf-8") catch {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{cmd_output});

    ssh.disconnect();
}