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 在 网络编程
每月 2,220 次下载
在 legba 中使用
47KB
986 行
async-ssh2-tokio
此库是使用 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(())
}
运行测试
- 安装 docker 和 docker compose
- 运行 shell 脚本
./tests/run_unit_tests.sh
依赖项
~16–27MB
~402K SLoC