Client

Struct Client 

Source
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.stdout, "Hello SSH\n");
    assert_eq!(result.exit_status, 0);

    Ok(())
}

Implementations§

Source§

impl Client

Source

pub async fn connect( addr: impl ToSocketAddrsWithHostname, 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 ToSocketAddrsWithHostname trait can be supplied for the address; ToSocketAddrsWithHostname reimplements all of [ToSocketAddrs]; see this trait’s 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 ToSocketAddrsWithHostname, 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 get_channel(&self) -> Result<Channel<Msg>, Error>

Source

pub async fn open_direct_tcpip_channel<T: ToSocketAddrsWithHostname, S: Into<Option<SocketAddr>>>( &self, target: T, src: S, ) -> Result<Channel<Msg>, Error>

Open a TCP/IP forwarding channel.

This opens a direct-tcpip channel to the given target.

Source

pub async fn upload_file<T: AsRef<Path>, U: Into<String>>( &self, src_file_path: T, dest_file_path: U, ) -> Result<(), Error>

Upload a file with sftp to the remote server.

src_file_path is the path to the file on the local machine. dest_file_path is the path to the file on the remote machine. Some sshd_config does not enable sftp by default, so make sure it is enabled. A config line like a Subsystem sftp internal-sftp or Subsystem sftp /usr/lib/openssh/sftp-server is needed in the sshd_config in remote machine.

Source

pub async fn download_file<T: AsRef<Path>, U: Into<String>>( &self, remote_file_path: U, local_file_path: T, ) -> Result<(), Error>

Download a file from the remote server using sftp.

remote_file_path is the path to the file on the remote machine. local_file_path is the path to the file on the local machine. Some sshd_config does not enable sftp by default, so make sure it is enabled. A config line like a Subsystem sftp internal-sftp or Subsystem sftp /usr/lib/openssh/sftp-server is needed in the sshd_config in remote machine.

Source

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

Execute a remote command via the ssh connection.

Returns stdout, stderr and the exit code of the command, packaged in a CommandExecutedResult struct. If you need the stderr output interleaved within stdout, you should postfix the command with a redirection, e.g. echo foo 2>&1. If you dont want any output at all, use something like echo foo >/dev/null 2>&1.

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 async fn execute_streaming( &self, command: &str, ch: Sender<SteamingOutput>, ) -> Result<u32, Error>

Execute a remote command via the ssh connection.

Command output is stream to the provided channel. Returns the exit code. The channel sends SteamingOutput enum variants to distinguish stdout, stderr and exit code so message arrive interleaved and in the order they are received. See execute for more details.

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>

Source

pub fn is_closed(&self) -> bool

Trait Implementations§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Client

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Client

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Client

§

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 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

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

Source§

fn vzip(self) -> V