使用旧的 Rust 2015
0.5.0 |
|
---|---|
0.4.0 |
|
0.3.3 |
|
0.2.0 |
|
0.1.0 |
|
#11 in #thrussh
用于 thrussh_scp
21KB
419 lines
这是一个 SSH 客户端库,旨在读取与 OpenSSH 格式相同的配置文件。
extern crate thrussh_client;
extern crate env_logger;
use thrussh_client::*;
use std::io::Write;
fn main() {
env_logger::init().unwrap();
let mut client = thrussh_client::Client::new();
client.set_host("localhost");
client.default_ssh_config().unwrap();
let mut client = client.connect().unwrap();
if let Some(key) = client.authenticate().unwrap() {
client.learn_host(&key).unwrap();
assert!(client.authenticate().unwrap().is_none());
}
struct C;
impl thrussh_client::Handler for C {
fn data(&mut self,
_: u32,
_: Option<u32>,
data: &[u8],
_: &mut Session)
-> Result<(), Error> {
try!(std::io::stdout().write(data));
Ok(())
}
}
let mut c = C;
let channel0 = client.channel_open_session().unwrap();
client.wait_channel_open(&mut c, channel0).unwrap();
client.exec(channel0, false, "ls -l");
client.eof(channel0);
let channel1 = client.channel_open_session().unwrap();
client.wait_channel_open(&mut c, channel1).unwrap();
client.data(channel1, None, b"blabla blibli").unwrap();
client.eof(channel1);
client.close(channel1);
client.wait_channel_close(&mut c, channel0).unwrap();
}
依赖项
~23MB
~226K SLoC