forked from 1148118271/ssh-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
43 lines (37 loc) · 1.07 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use ssh_rs::{ssh, LocalShell, SshErrorKind};
use std::time::Duration;
fn main() {
ssh::enable_log();
let mut session = ssh::create_session()
.username("ubuntu")
.password("password")
.timeout(Some(Duration::from_millis(1000)))
.private_key_path("./id_rsa")
.connect("127.0.0.1:22")
.unwrap()
.run_local();
// Usage 1
let mut shell = session.open_shell().unwrap();
run_shell(&mut shell);
// Close channel.
shell.close().unwrap();
// Close session.
session.close();
}
fn run_shell(shell: &mut LocalShell<std::net::TcpStream>) {
let out = shell.read().unwrap();
print!("{}", String::from_utf8(out).unwrap());
shell.write(b"ls -lah\n").unwrap();
loop {
match shell.read() {
Ok(out) => print!("{}", String::from_utf8(out).unwrap()),
Err(e) => {
if let SshErrorKind::Timeout = e.kind() {
break;
} else {
panic!("{}", e.to_string())
}
}
}
}
}