Sample code for 30+ languages & platforms
Zig

Get the Exit Status of a Remote SSH Command

See more SSH Examples

Demonstrates the Chilkat Ssh.GetChannelExitStatus method, which returns the remote process exit status for a channel. The only argument is the channel number. Call it only after ChannelReceivedExitStatus returns true.

Background: The exit status is how you learn whether a remote command actually succeeded — recall that a successful exec request only means the request was accepted. By Unix convention 0 means success and any nonzero value indicates a failure. Retrieve it promptly after the receive operation and before calling unrelated SSH methods or releasing the channel, since the completed channel record can be discarded.

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.GetChannelExitStatus method, which returns the remote process exit
    // status for a channel.  The only argument is the channel number.  Call it only after
    // ChannelReceivedExitStatus returns true.

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

    ssh.sendReqExec(channel_num, "grep -q root /etc/passwd") catch {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    };

    // IMPORTANT: Set a read timeout.  ReadTimeoutMs defaults to 0, which means no limit -- without
    // it, this call waits forever if the server never sends channel close.
    ssh.setReadTimeoutMs(15000);

    ssh.channelReceiveToClose(channel_num) catch {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    };

    // Retrieve the exit status immediately, before calling unrelated SSH methods or releasing the
    // channel, because the completed channel record can be discarded.
    if (ssh.channelReceivedExitStatus(channel_num)) {
        const exit_status = ssh.getChannelExitStatus(channel_num);
        std.debug.print("Exit status: {d}\n", .{exit_status});

        // By convention, 0 means the remote command succeeded.
        if (exit_status == 0) {
            std.debug.print("The remote command succeeded.\n", .{});
        } else {
            std.debug.print("The remote command reported a failure.\n", .{});
        }
    }

    ssh.disconnect();
}