Zig
Zig
SSH to a Cisco Switch - Processing "More" Responses
See more SSH Examples
Demonstrates connecting to a Cisco switch, entering privileged mode with the ena command, and running a command whose response is paged. Each page ends with --More-- and requires a SPACE character to request the next page.
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: Paged output is the console equivalent of a pager like
more, and a client must reproduce the keypress a human would make. Matching a set of patterns is what makes this tractable: each read ends either at the device prompt (output complete) or at --More-- (another page waiting), and the code branches accordingly. Note the prompt pattern includes the device name rather than a bare #, because that character also appears inside configuration text and would otherwise match far too early.Chilkat Zig Downloads
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 connecting to a Cisco switch, entering privileged mode, and running a command
// whose response is paged -- each page ends with "--More--" and requires a SPACE character to
// request the next page.
const ssh = try chilkat.Ssh.init();
defer ssh.deinit();
const port = 22;
ssh.connect("172.16.16.100", 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;
// In unprivileged mode the switch prompt ends with ">".
ssh.channelReceiveUntilMatch(channel_num, ">", "utf-8", case_sensitive) catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
var received: [: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", .{received});
// Send "ena" to enter privileged mode. Cisco devices expect a bare CR to terminate a command.
ssh.channelSendString(channel_num, "ena\r", "utf-8") catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
ssh.channelReceiveUntilMatch(channel_num, "Password:", "utf-8", case_sensitive) catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
received = ssh.getReceivedText(alloc, channel_num, "utf-8") catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
std.debug.print("{s}\n", .{received});
// The enable password is separate from the login password, and likewise should come from a
// secure source rather than being hard-coded.
const enable_password = "myEnablePassword";
ssh.channelSendString(channel_num, enable_password, "utf-8") catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
ssh.channelSendString(channel_num, "\r", "utf-8") catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
// In privileged mode the prompt ends with "#" instead of ">".
ssh.channelReceiveUntilMatch(channel_num, "#", "utf-8", case_sensitive) catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
received = ssh.getReceivedText(alloc, channel_num, "utf-8") catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
std.debug.print("{s}\n", .{received});
// Run a command whose output is delivered a page at a time.
ssh.channelSendString(channel_num, "show running-config\r", "utf-8") catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
// Each read ends either at the device prompt (output complete) or at "--More--" (another page
// is waiting). Match the full prompt rather than a bare "#", because "#" also appears inside
// configuration text and would match too early.
const sa_match = try chilkat.StringArray.init();
defer sa_match.deinit();
sa_match.append("MySwitchName#") catch {};
sa_match.append("--More--") catch {};
const sb_received = try chilkat.StringBuilder.init();
defer sb_received.deinit();
var more_coming: bool = true;
while (more_coming) {
ssh.channelReceiveUntilMatchN(channel_num, sa_match, "utf-8", case_sensitive) catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
sb_received.clear();
const page_text = ssh.getReceivedText(alloc, channel_num, "utf-8") catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
sb_received.append(page_text) catch {};
std.debug.print("{s}\n", .{page_text});
// If this page ended with "--More--", send a SPACE to request the next page.
more_coming = sb_received.contains("--More--", case_sensitive);
if (more_coming) {
ssh.channelSendString(channel_num, " ", "utf-8") catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
}
}
ssh.disconnect();
}