forked from 1148118271/ssh-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
54 lines (46 loc) · 1.48 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
44
45
46
47
48
49
50
51
52
53
54
use ssh::{self, LocalShell, SshError};
use tracing::Level;
use tracing_subscriber::FmtSubscriber;
use std::time::Duration;
fn main() {
// a builder for `FmtSubscriber`.
let subscriber = FmtSubscriber::builder()
// all spans/events with a level higher than INFO (e.g, info, warn, etc.)
// will be written to stdout.
.with_max_level(Level::INFO)
// completes the builder.
.finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
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 SshError::TimeoutError = e {
break;
} else {
panic!("{}", e.to_string())
}
}
}
}
}