| Crates.io | lmrc-ssh |
| lib.rs | lmrc-ssh |
| version | 0.3.16 |
| created_at | 2025-11-26 17:44:11.933883+00 |
| updated_at | 2025-12-11 13:27:42.023386+00 |
| description | SSH client library for the LMRC Stack - comprehensive library for executing remote SSH commands programmatically |
| homepage | https://gitlab.com/lemarco/lmrc-stack/tree/main/libs/ssh-manager |
| repository | https://gitlab.com/lemarco/lmrc-stack |
| max_upload_size | |
| id | 1951916 |
| size | 51,261 |
Part of the LMRC Stack - Infrastructure-as-Code toolkit for building production-ready Rust applications
A comprehensive Rust library for executing SSH commands programmatically. Built on top of the robust ssh2 library, lmrc-ssh provides a clean, intuitive API for managing SSH connections and executing remote commands.
Add this to your Cargo.toml:
[dependencies]
lmrc-ssh = "0.1"
use lmrc_ssh::{SshClient, AuthMethod};
fn main() -> Result<(), lmrc_ssh::Error> {
let mut client = SshClient::new("example.com", 22)?
.with_auth(AuthMethod::Password {
username: "user".to_string(),
password: "password".to_string(),
})
.connect()?;
let output = client.execute("hostname")?;
println!("Hostname: {}", output.stdout);
Ok(())
}
use lmrc_ssh::{SshClient, AuthMethod};
fn main() -> Result<(), lmrc_ssh::Error> {
let mut client = SshClient::new("example.com", 22)?
.with_auth(AuthMethod::PublicKey {
username: "user".to_string(),
private_key_path: "/home/user/.ssh/id_rsa".to_string(),
passphrase: None,
})
.connect()?;
let output = client.execute("ls -la")?;
println!("Output:\n{}", output.stdout);
Ok(())
}
use lmrc_ssh::{SshClient, AuthMethod};
fn main() -> Result<(), lmrc_ssh::Error> {
let mut client = SshClient::new("192.168.1.100", 22)?
.with_auth(AuthMethod::Password {
username: "admin".to_string(),
password: "secret".to_string(),
})
.connect()?;
let commands = vec!["whoami", "hostname", "pwd"];
let outputs = client.execute_batch(&commands)?;
for (cmd, output) in commands.iter().zip(outputs.iter()) {
println!("Command: {}", cmd);
println!("Output: {}", output.stdout);
println!("---");
}
Ok(())
}
use lmrc_ssh::{SshClient, AuthMethod};
fn main() -> Result<(), lmrc_ssh::Error> {
let mut client = SshClient::new("example.com", 22)?
.with_auth(AuthMethod::Password {
username: "user".to_string(),
password: "pass".to_string(),
})
.connect()?;
let output = client.execute("some-command")?;
if output.is_success() {
println!("Success! Output: {}", output.stdout);
} else {
eprintln!("Command failed with exit code: {}", output.exit_status);
eprintln!("Error: {}", output.stderr);
}
Ok(())
}
use lmrc_ssh::{SshClient, AuthMethod, Error};
fn main() {
let result = SshClient::new("example.com", 22)
.and_then(|client| {
client.with_auth(AuthMethod::Password {
username: "user".to_string(),
password: "wrong_password".to_string(),
})
.connect()
});
match result {
Ok(_) => println!("Connected successfully"),
Err(Error::AuthenticationFailed { username, reason }) => {
eprintln!("Authentication failed for {}: {}", username, reason);
}
Err(Error::ConnectionFailed { host, port, .. }) => {
eprintln!("Failed to connect to {}:{}", host, port);
}
Err(e) => eprintln!("Error: {}", e),
}
}
SshClientThe main client for SSH connections.
new(host, port) - Create a new client instancewith_auth(auth_method) - Set the authentication methodconnect() - Establish the SSH connectionexecute(command) - Execute a single commandexecute_batch(commands) - Execute multiple commandsis_connected() - Check if the client is connectedAuthMethodAuthentication methods for SSH:
Password { username, password } - Password authenticationPublicKey { username, private_key_path, passphrase } - Public key authenticationCommandOutputRepresents the output of an executed command:
stdout - Standard outputstderr - Standard errorexit_status - Exit status codeis_success() - Check if the command succeededis_failure() - Check if the command failedcombined_output() - Get stdout + stderr combinedComprehensive error types for better error handling:
ConnectionFailed - Failed to establish TCP connectionAuthenticationFailed - Authentication failedExecutionFailed - Command execution failedNotConnected - Client is not connectedInvalidConfig - Invalid configurationCheck out the examples directory for more usage examples:
simple_connection.rs - Basic SSH connection and command executionbatch_commands.rs - Execute multiple commandserror_handling.rs - Comprehensive error handling examplesRun an example with:
cargo run --example simple_connection
Run the test suite:
cargo test
Run tests with output:
cargo test -- --nocapture
Generate and view the documentation:
cargo doc --open
Contributions are welcome! Please feel free to submit a Pull Request.
Part of the LMRC Stack project. Licensed under either of:
at your option.
This library is built on top of the excellent ssh2 crate.
If you discover a security vulnerability, please email lemarc.dev@gmail.com.
See CHANGELOG.md for release history.