ls/
ls.rs

1extern crate async_ssh;
2extern crate futures;
3extern crate thrussh;
4extern crate thrussh_keys;
5extern crate tokio_core;
6extern crate tokio_io;
7
8use tokio_core::net::TcpStream;
9use async_ssh::Session;
10use futures::Future;
11
12fn main() {
13    // async:
14
15    let mut core = tokio_core::reactor::Core::new().unwrap();
16    let handle = core.handle();
17
18    let cmd = ::std::env::args().skip(1).next().unwrap();
19    let key = thrussh_keys::load_secret_key("/home/jon/aws-test.pem", None).unwrap();
20
21    let ls_out = TcpStream::connect(&"52.23.157.12:22".parse().unwrap(), &handle)
22        .map_err(thrussh::Error::IO)
23        .map_err(thrussh::HandlerError::Error)
24        .and_then(|c| Session::new(c, &handle))
25        .and_then(|session| session.authenticate_key("ec2-user", key))
26        .and_then(|mut session| session.open_exec(&cmd));
27
28    let channel = core.run(ls_out).unwrap();
29    let (channel, data) = core.run(tokio_io::io::read_to_end(channel, Vec::new()))
30        .unwrap();
31    println!("{}", ::std::str::from_utf8(&data[..]).unwrap());
32    let status = core.run(channel.exit_status()).unwrap();
33    println!("{}", status);
34
35    /*
36
37    // sync:
38
39    // Connect to the local SSH server
40    let tcp = TcpStream::connect("127.0.0.1:22").unwrap();
41    let mut sess = Session::new().unwrap();
42    sess.handshake(&tcp).unwrap();
43    sess.userauth_agent("username").unwrap();
44
45    let mut channel = sess.channel_session().unwrap();
46    channel.exec("ls").unwrap();
47    let mut s = String::new();
48    channel.read_to_string(&mut s).unwrap();
49    println!("{}", s);
50    channel.wait_close();
51    println!("{}", channel.exit_status().unwrap());
52    */
53}