Zig
Zig
Check Whether an SSH Channel is Open
See more SSH Examples
Demonstrates the Chilkat Ssh.ChannelIsOpen method, which reports whether a channel number identifies a channel Chilkat currently considers open for application I/O. The only argument is the channel number. This example checks the channel before and after sending CLOSE.
Background: Because one SSH connection can carry many channels with independent lifetimes, code that manages several needs a way to ask whether a given one is still usable. This returns false for invalid or nonexistent channel numbers, after a local
Disconnect, once remote closure is processed, and immediately after ChannelSendClose — which marks the channel locally closed without waiting for the remote side.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.ChannelIsOpen method, which reports whether a channel number identifies
// a channel Chilkat currently considers open for application I/O. The only argument is the
// channel 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;
}
if (ssh.channelIsOpen(channel_num)) {
std.debug.print("The channel is open.\n", .{});
} else {
std.debug.print("The channel is not open.\n", .{});
}
// Send CLOSE, which immediately marks the channel locally closed.
ssh.channelSendClose(channel_num) catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
if (ssh.channelIsOpen(channel_num)) {
std.debug.print("Still open.\n", .{});
} else {
std.debug.print("The channel is now closed.\n", .{});
}
ssh.disconnect();
}