#ssh-client #ssh #async #execute-command #ssh2

async-ssh2-tokio

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

28 releases

0.8.12 Aug 17, 2024
0.8.11 Jul 28, 2024
0.8.10 Jun 10, 2024
0.8.7 Feb 9, 2024
0.1.0 Nov 30, 2021

#819 in Network programming

Download history 634/week @ 2024-07-24 641/week @ 2024-07-31 492/week @ 2024-08-07 957/week @ 2024-08-14 615/week @ 2024-08-21 555/week @ 2024-08-28 714/week @ 2024-09-04 645/week @ 2024-09-11 588/week @ 2024-09-18 681/week @ 2024-09-25 671/week @ 2024-10-02 603/week @ 2024-10-09 309/week @ 2024-10-16 504/week @ 2024-10-23 531/week @ 2024-10-30 569/week @ 2024-11-06

2,025 downloads per month
Used in 2 crates

Custom license

47KB
986 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 an 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.8.12"

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

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

    Ok(())
}

Running Tests

  1. install docker and docker compose
  2. run shell script ./tests/run_unit_tests.sh

Dependencies

~17–27MB
~410K SLoC