Sample code for 30+ languages & platforms
Zig

Check the SSH Connection (IsConnected)

See more SSH Examples

Demonstrates how to check whether the connection to the SSH server still exists. The IsConnected property is checked first, and SendIgnore is then used to confirm the link for certain.

Background: These checks differ in strength. IsConnected simply reports the state the object already knows and involves no network activity, so it can stay true after a silent network failure the operating system has not yet noticed. SendIgnore sends an SSH IGNORE message — discarded by the peer by design — which exercises the real write path and is therefore much stronger evidence the link is usable. CheckConnection sits between the two, peeking at the socket without sending anything.

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 how to check whether the connection to the SSH server still exists.

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

    const port = 22;
    ssh.connect("ssh.example.com", port) catch {
        std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
        return;
    };

    // IsConnected reports the state already known to the Ssh object.  It does not perform a
    // network round trip.
    var connected: bool = ssh.getIsConnected();
    if (connected) {
        // Verify for certain by sending an SSH IGNORE message, which exercises the actual write
        // path.  The server sends no reply, but a successful send proves the link is writable.
        connected = if (ssh.sendIgnore()) true else |_| false;
    }

    std.debug.print("connected = {}\n", .{connected});

    ssh.disconnect();

    // After disconnecting, the object reports that it is no longer connected.
    connected = ssh.getIsConnected();
    std.debug.print("connected = {}\n", .{connected});
}