pub struct Client { /* private fields */ }
Expand description

A ssh connection to a remote server.

After creating a Client by connecting to a remote host, use execute to send commands and receive results through the connections.

Examples

use async_ssh2_tokio::{Client, AuthMethod, ServerCheckMethod};
#[tokio::main]
async fn main() -> Result<(), async_ssh2_tokio::Error> {
    let mut client = Client::connect(
        ("10.10.10.2", 22),
        "root",
        AuthMethod::with_password("root"),
        ServerCheckMethod::NoCheck,
    ).await?;

    let result = client.execute("echo Hello SSH").await?;
    assert_eq!(result.output, "Hello SSH\n");
    assert_eq!(result.exit_status, 0);

    Ok(())
}

Implementations§

source§

impl Client

source

pub async fn connect( addr: impl ToSocketAddrs, username: &str, auth: AuthMethod, server_check: ServerCheckMethod ) -> Result<Self, Error>

Open a ssh connection to a remote host.

addr is an address of the remote host. Anything which implements ToSocketAddrs trait can be supplied for the address; see this trait documentation for concrete examples.

If addr yields multiple addresses, connect will be attempted with each of the addresses until a connection is successful. Authentification is tried on the first successful connection and the whole process aborted if this fails.

source

pub async fn connect_with_config( addr: impl ToSocketAddrs, username: &str, auth: AuthMethod, server_check: ServerCheckMethod, config: Config ) -> Result<Self, Error>

Same as connect, but with the option to specify a non default russh::client::Config.

source

pub async fn execute( &self, command: &str ) -> Result<CommandExecutedResult, Error>

Execute a remote command via the ssh connection.

Returns both the stdout output and the exit code of the command, packaged in a CommandExecutedResult struct. If you need the stderr output, consider prefixing the command with a redirection, e.g. 2>&1 echo foo >>/dev/stderr. If you don’t need the output, use something like echo foo >/dev/null. Make sure your commands don’t read from stdin and exit after bounded time.

Can be called multiple times, but every invocation is a new shell context. Thus cd, setting variables and alike have no effect on future invocations.

source

pub fn get_connection_username(&self) -> &String

A debugging function to get the username this client is connected as.

source

pub fn get_connection_address(&self) -> &SocketAddr

A debugging function to get the address this client is connected to.

source

pub async fn disconnect(&self) -> Result<(), Error>

Auto Trait Implementations§

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> Same<T> for T

§

type Output = T

Should always be Self
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V