pub struct SshClient { /* private fields */ }Expand description
An SSH client for executing remote commands.
§Examples
use lmrc_ssh::{SshClient, AuthMethod};
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("hostname")?;
println!("Hostname: {}", output.stdout);Implementations§
Source§impl SshClient
impl SshClient
Sourcepub fn with_auth(self, auth: AuthMethod) -> Self
pub fn with_auth(self, auth: AuthMethod) -> Self
Sourcepub fn connect(self) -> Result<Self>
pub fn connect(self) -> Result<Self>
Establishes the SSH connection and authenticates.
§Errors
Returns an error if:
- The TCP connection fails
- The SSH handshake fails
- Authentication fails
- No authentication method was set
§Examples
use lmrc_ssh::{SshClient, AuthMethod};
let mut client = SshClient::new("example.com", 22)?
.with_auth(AuthMethod::Password {
username: "user".to_string(),
password: "pass".to_string(),
})
.connect()?;Sourcepub fn execute(&mut self, command: &str) -> Result<CommandOutput>
pub fn execute(&mut self, command: &str) -> Result<CommandOutput>
Executes a command on the remote server.
§Arguments
command- The command to execute
§Errors
Returns an error if:
- The client is not connected
- Failed to open an SSH channel
- Failed to execute the command
§Examples
use lmrc_ssh::{SshClient, AuthMethod};
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("ls -la /tmp")?;
println!("Files:\n{}", output.stdout);Sourcepub fn execute_batch(&mut self, commands: &[&str]) -> Result<Vec<CommandOutput>>
pub fn execute_batch(&mut self, commands: &[&str]) -> Result<Vec<CommandOutput>>
Executes multiple commands sequentially.
§Arguments
commands- A slice of commands to execute
§Returns
Returns a vector of CommandOutput for each executed command.
§Examples
use lmrc_ssh::{SshClient, AuthMethod};
let mut client = SshClient::new("example.com", 22)?
.with_auth(AuthMethod::Password {
username: "user".to_string(),
password: "pass".to_string(),
})
.connect()?;
let outputs = client.execute_batch(&["whoami", "hostname", "pwd"])?;
for output in outputs {
println!("{}", output.stdout);
}Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Returns whether the client is currently connected.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SshClient
impl !RefUnwindSafe for SshClient
impl Send for SshClient
impl Sync for SshClient
impl Unpin for SshClient
impl !UnwindSafe for SshClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more