SshClient

Struct SshClient 

Source
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

Source

pub fn new(host: impl Into<String>, port: u16) -> Result<Self>

Creates a new SSH client instance.

§Arguments
  • host - The hostname or IP address of the remote server
  • port - The SSH port (typically 22)
§Examples
use lmrc_ssh::SshClient;

let client = SshClient::new("example.com", 22)?;
Source

pub fn with_auth(self, auth: AuthMethod) -> Self

Sets the authentication method for the connection.

§Arguments
  • auth - The authentication method to use
§Examples
use lmrc_ssh::{SshClient, AuthMethod};

let client = SshClient::new("example.com", 22)?
    .with_auth(AuthMethod::Password {
        username: "user".to_string(),
        password: "pass".to_string(),
    });
Source

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()?;
Source

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);
Source

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);
}
Source

pub fn host(&self) -> &str

Returns the hostname this client is configured to connect to.

Source

pub fn port(&self) -> u16

Returns the port this client is configured to connect to.

Source

pub fn is_connected(&self) -> bool

Returns whether the client is currently connected.

Trait Implementations§

Source§

impl Debug for SshClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.