use std::net::TcpStream; use ssh2; use std::path::Path; use crate::config::Config; pub fn init_session(conf: &Config) -> ssh2::Session { // Connect to the local SSH server let s = String::from(conf.remote_addr.as_ref().unwrap()) + ":" + &conf.remote_port.unwrap_or(22).to_string(); let tcp = TcpStream::connect(&s).unwrap(); let mut sess = ssh2::Session::new().unwrap(); sess.set_tcp_stream(tcp); sess.handshake().unwrap(); // Try to authenticate with the first identity in the agent. // sess.userauth_agent(&conf.remote_user.as_ref().unwrap()).unwrap(); let key_path = match conf.ssh_id.as_ref() { Some(v) => v.as_path(), _ => Path::new("~/.ssh/id_rsa"), }; sess.userauth_pubkey_file(&conf.remote_user.as_ref().unwrap(), None, key_path, None ).unwrap(); sess }