use ssh2::Session;
use std::io::prelude::*;
use std::net::TcpStream;

// // Connect to the local SSH server
// let tcp = TcpStream::connect("127.0.0.1:22").unwrap();
// let mut sess = Session::new().unwrap();
// sess.set_tcp_stream(tcp);
// sess.handshake().unwrap();
// sess.userauth_password("username", "password").unwrap();
// assert!(sess.authenticated());

// https://docs.rs/ssh2/0.8.3/ssh2/
fn main() {
    // Almost all APIs require a `Session` to be available
    let sess = Session::new().unwrap();
    let mut agent = sess.agent().unwrap();

    // Connect the agent and request a list of identities
    agent.connect().unwrap();
    agent.list_identities().unwrap();

    for identity in agent.identities().unwrap() {
        let pubkey = identity.blob();
        println!("{}", identity.comment());
        println!("- {:?}", pubkey);
    }

    // Connect to the SSH server
    let tcp = TcpStream::connect("hatter.ink:22").unwrap();
    let mut sess = Session::new().unwrap();
    sess.set_tcp_stream(tcp);
    sess.handshake().unwrap();

    // Try to authenticate with the first identity in the agent.
    sess.userauth_agent("root").unwrap();

    // Make sure we succeeded
    println!("authenticated: {}", sess.authenticated());

    // Run command
    let mut channel = sess.channel_session().unwrap();
    channel.exec("ls -l --color").unwrap();
    let mut s = String::new();
    channel.read_to_string(&mut s).unwrap();
    println!("{}", s);
    channel.wait_close().unwrap();
    println!("{}", channel.exit_status().unwrap());
}