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

async-ssh2-tokio

适用于 Rust 的异步、易于使用的高级 SSH 客户端库

28 个版本

新增 0.8.12 2024 年 8 月 17 日
0.8.11 2024 年 7 月 28 日
0.8.10 2024 年 6 月 10 日
0.8.7 2024 年 2 月 9 日
0.1.0 2021 年 11 月 30 日

1051网络编程

Download history 249/week @ 2024-04-26 292/week @ 2024-05-03 108/week @ 2024-05-10 88/week @ 2024-05-17 110/week @ 2024-05-24 153/week @ 2024-05-31 297/week @ 2024-06-07 212/week @ 2024-06-14 340/week @ 2024-06-21 356/week @ 2024-06-28 576/week @ 2024-07-05 476/week @ 2024-07-12 375/week @ 2024-07-19 689/week @ 2024-07-26 630/week @ 2024-08-02 465/week @ 2024-08-09

每月 2,220 次下载
legba 中使用

自定义许可证

47KB
986

async-ssh2-tokio

Unit Test Status Lint Status Docs.rs Crates.io

此库是使用 tokio 运行时为 Rust 提供的异步和易于使用的高级 SSH 客户端库。由 rust ssh 实现 russh 支持。

特性

  • 连接到 SSH 服务器
  • 在远程主机上执行命令
  • 获取命令的 stdout 和退出代码

安装

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

使用方法

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(())
}

运行测试

  1. 安装 docker 和 docker compose
  2. 运行 shell 脚本 ./tests/run_unit_tests.sh

依赖项

~16–27MB
~402K SLoC