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

async-ssh2-tokio

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

23 releases

0.8.7 Feb 9, 2024
0.8.5 Jan 9, 2024
0.8.4 Nov 25, 2023
0.7.0 Jul 10, 2023
0.1.0 Nov 30, 2021

#1694 in Network programming

Download history 20/week @ 2024-01-01 222/week @ 2024-01-08 367/week @ 2024-01-15 236/week @ 2024-01-22 90/week @ 2024-01-29 87/week @ 2024-02-05 50/week @ 2024-02-12 181/week @ 2024-02-19 200/week @ 2024-02-26 114/week @ 2024-03-04 121/week @ 2024-03-11 116/week @ 2024-03-18 56/week @ 2024-03-25 168/week @ 2024-04-01 163/week @ 2024-04-08 65/week @ 2024-04-15

453 downloads per month
Used in legba

Custom license

34KB
687 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.8.7"

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 shellscript ./tests/run_unit_tests.sh

Dependencies

~11–24MB
~319K SLoC