#async #asynchronous #ssh #ssh2 #ssh-client

async-ssh2-tokio

Asynchronous and easy-to-use high level ssh client library for rust

10 releases (4 breaking)

0.6.4 Mar 15, 2023
0.6.3 Mar 9, 2023
0.6.1 Feb 24, 2023
0.4.0 Jan 28, 2023
0.1.0 Nov 30, 2021

#740 in Network programming

Download history 23/week @ 2023-02-07 23/week @ 2023-02-14 52/week @ 2023-02-21 11/week @ 2023-02-28 48/week @ 2023-03-07 41/week @ 2023-03-14 4/week @ 2023-03-21 10/week @ 2023-03-28 24/week @ 2023-04-04 41/week @ 2023-04-11 35/week @ 2023-04-18 16/week @ 2023-04-25 22/week @ 2023-05-02 57/week @ 2023-05-09 35/week @ 2023-05-16 11/week @ 2023-05-23

129 downloads per month

Custom license

28KB
551 lines

async-ssh2-tokio

Unit Test Status Lint Status Docs.rs, Crates.io

This library is an asynchronous and easy-to-use high level ssh client library for rust with the tokio runtime. Powered by the rust ssh implementation russh.

Features

  • Connect to a SSH Host
  • Execute commands on the remote host
  • Get the stdout and exit code of the command

Install

[dependencies]
tokio = "1"
async-ssh2-tokio = "0.6.4"

Usage

use async_ssh2_tokio::client::{Client, AuthMethod, ServerCheckMethod};
#[tokio::main]
async fn main() -> Result<(), async_ssh2_tokio::Error> {
    // if you want to use key auth, then use following:
    // AuthMethod::with_key_file("key_file_name", Some("passphrase"));
    // or
    // AuthMethod::with_key_file("key_file_name", None);
    // or
    // AuthMethod::with_key(key: &str, passphrase: Option<&str>)
    let auth_method = AuthMethod::with_password("root");
    let mut client = Client::connect(
        ("10.10.10.2", 22),
        "root",
        auth_method,
        ServerCheckMethod::NoCheck,
    ).await?;

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

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

    Ok(())
}

Running Tests

In order to run the tests, either set up docker compose on your machine or set the following environment variables for a working ssh host:

  • ASYNC_SSH2_TEST_HOST_IP: The ip, e.g. 127.0.0.1 when testing with localhost.
  • ASYNC_SSH2_TEST_HOST_USER: The username to connect as.
  • ASYNC_SSH2_TEST_HOST_PW: The corresponding password. Since this is plain text, creating a new unpriviledged user is recommended.

Note: The doc tests do not use these variables and are therefore not run, but only compiled.

Dependencies

~9–15MB
~253K SLoC