Rust
Rust
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 Rust Downloads
// 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.
let ssh = chilkat::Ssh::new();
let port = 22;
if ssh.connect("172.16.16.100", port).is_err() {
println!("{}", ssh.last_error_text());
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.
let password = "mySshPassword".to_string();
if ssh.authenticate_pw("mySshLogin", &password).is_err() {
println!("{}", ssh.last_error_text());
return;
}
let channel_num = ssh.quick_shell();
if channel_num < 0 {
println!("{}", ssh.last_error_text());
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.set_read_timeout_ms(15000);
let case_sensitive = true;
// In unprivileged mode the switch prompt ends with ">".
if ssh.channel_receive_until_match(channel_num, ">", "utf-8", case_sensitive).is_err() {
println!("{}", ssh.last_error_text());
return;
}
let Ok(mut received) = ssh.get_received_text(channel_num, "utf-8") else {
println!("{}", ssh.last_error_text());
return;
};
println!("{}", received);
// Send "ena" to enter privileged mode. Cisco devices expect a bare CR to terminate a command.
if ssh.channel_send_string(channel_num, "ena\r", "utf-8").is_err() {
println!("{}", ssh.last_error_text());
return;
}
if ssh.channel_receive_until_match(channel_num, "Password:", "utf-8", case_sensitive).is_err() {
println!("{}", ssh.last_error_text());
return;
}
received = ssh.get_received_text(channel_num, "utf-8").unwrap_or_default();
if !ssh.last_method_success() {
println!("{}", ssh.last_error_text());
return;
}
println!("{}", received);
// The enable password is separate from the login password, and likewise should come from a
// secure source rather than being hard-coded.
let enable_password = "myEnablePassword".to_string();
if ssh.channel_send_string(channel_num, &enable_password, "utf-8").is_err() {
println!("{}", ssh.last_error_text());
return;
}
if ssh.channel_send_string(channel_num, "\r", "utf-8").is_err() {
println!("{}", ssh.last_error_text());
return;
}
// In privileged mode the prompt ends with "#" instead of ">".
if ssh.channel_receive_until_match(channel_num, "#", "utf-8", case_sensitive).is_err() {
println!("{}", ssh.last_error_text());
return;
}
received = ssh.get_received_text(channel_num, "utf-8").unwrap_or_default();
if !ssh.last_method_success() {
println!("{}", ssh.last_error_text());
return;
}
println!("{}", received);
// Run a command whose output is delivered a page at a time.
if ssh.channel_send_string(channel_num, "show running-config\r", "utf-8").is_err() {
println!("{}", ssh.last_error_text());
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.
let sa_match = chilkat::StringArray::new();
let _ = sa_match.append("MySwitchName#");
let _ = sa_match.append("--More--");
let sb_received = chilkat::StringBuilder::new();
let mut more_coming = true;
while more_coming {
if ssh.channel_receive_until_match_n(channel_num, &sa_match, "utf-8", case_sensitive).is_err() {
println!("{}", ssh.last_error_text());
return;
}
sb_received.clear();
let Ok(page_text) = ssh.get_received_text(channel_num, "utf-8") else {
println!("{}", ssh.last_error_text());
return;
};
let _ = sb_received.append(&page_text);
println!("{}", 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 {
if ssh.channel_send_string(channel_num, " ", "utf-8").is_err() {
println!("{}", ssh.last_error_text());
return;
}
}
}
ssh.disconnect();