Rust
Rust
SSH Commands to a Cisco Switch
See more SSH Examples
Demonstrates establishing an SSH session with a Cisco switch (or similar device) and sending commands in a device console session. Each command is sent, and its output is read up to the device's next prompt.
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: Network devices differ from general-purpose servers in that they expose a menu- or prompt-driven console rather than a Unix shell, so
exec requests are usually unavailable and driving the interactive console is the only option. That makes prompt matching the synchronization mechanism, and it is why a PTY is appropriate here despite the general advice to avoid one. Retrieving the received text between commands is essential: a prompt left in the buffer would make the next receive return immediately with the wrong output.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates establishing an SSH session with a Cisco switch (or similar device) and sending
// commands in a device console session.
//
// 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.
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;
}
// Start a shell session.
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;
// The switch presents a prompt ending in "#". Reading up to it confirms the session is ready.
if ssh.channel_receive_until_match(channel_num, "#", "utf-8", case_sensitive).is_err() {
println!("{}", ssh.last_error_text());
return;
}
let Ok(mut cmd_output) = ssh.get_received_text(channel_num, "utf-8") else {
println!("{}", ssh.last_error_text());
return;
};
println!("{}", cmd_output);
// Send a command and read its output up to the next prompt.
if ssh.channel_send_string(channel_num, "show clock\n", "utf-8").is_err() {
println!("{}", ssh.last_error_text());
return;
}
if ssh.channel_receive_until_match(channel_num, "#", "utf-8", case_sensitive).is_err() {
println!("{}", ssh.last_error_text());
return;
}
// Retrieving the text also clears the receive buffer, which matters: if the prompt were left
// buffered, the next receive would match it immediately and return the wrong output.
cmd_output = ssh.get_received_text(channel_num, "utf-8").unwrap_or_default();
if !ssh.last_method_success() {
println!("{}", ssh.last_error_text());
return;
}
println!("{}", cmd_output);
// Additional commands follow the same send / read-to-prompt / retrieve pattern.
if ssh.channel_send_string(channel_num, "show version\n", "utf-8").is_err() {
println!("{}", ssh.last_error_text());
return;
}
if ssh.channel_receive_until_match(channel_num, "#", "utf-8", case_sensitive).is_err() {
println!("{}", ssh.last_error_text());
return;
}
cmd_output = ssh.get_received_text(channel_num, "utf-8").unwrap_or_default();
if !ssh.last_method_success() {
println!("{}", ssh.last_error_text());
return;
}
println!("{}", cmd_output);
ssh.disconnect();