< 返回版块

1148118271 发表于 2022-12-07 15:36

Tags:rust,ssh,ssh2,client

ssh-rs ✨

github地址: https://github.com/1148118271/ssh-rs

连接方式:

1. 密码连接:

fn main() {
    let session = ssh::create_session()
        .username("ubuntu")
        .password("password")
        .connect("ip:port")
        .unwrap()
        .run_local();
}

2. 公钥连接:

fn main() {
    let session = ssh::create_session()
        .username("ubuntu")
        .password("password")
        .private_key_path("./id_rsa") // 文件地址
        .connect("ip:port")
        .unwrap()
        .run_local();
}    
fn main() {
    let session = ssh::create_session()
        .username("ubuntu")
        .password("password")
        .private_key("rsa_string") // 文件字符串
        .connect("ip:port")
        .unwrap()
        .run_local();
}

启用全局日志:

 ssh::debug();

设置超时时间:

ssh::create_session().timeout(50);

示例:

use ssh_rs::ssh;

fn main() {
    ssh::enable_log();

    let mut session = ssh::create_session()
        .username("ubuntu")
        .password("password")
        .private_key_path("./id_rsa")
        .connect("127.0.0.1:22")
        .unwrap()
        .run_local();
    let exec = session.open_exec().unwrap();
    let vec: Vec<u8> = exec.send_command("ls -all").unwrap();
    println!("{}", String::from_utf8(vec).unwrap());
    // Close session.
    session.close();
}

评论区

写评论
作者 1148118271 2023-01-13 17:58

😁

--
👇
rustdesk: 不错不错

rustdesk 2022-12-09 20:01

不错不错

1 共 2 条评论, 1 页