use directories::UserDirs; use ssh2::KnownHostFileKind; use crate::{Session, SessionSource}; impl SessionSource for ssh2::Session { type Error = ssh2::Error; type Iter = Vec; fn sessions(&self) -> Result { let mut known_hosts = self.known_hosts()?; let file = UserDirs::new() .ok_or_else(ssh2::Error::unknown)? .home_dir() .join(".ssh/known_hosts"); known_hosts.read_file(&file, KnownHostFileKind::OpenSSH)?; let sessions = known_hosts .hosts()? .into_iter() .filter_map(|h| match h.name() { Some(name) => Some(name.to_owned().into()), None => { tracing::warn!("Invalid host: No plain text host name exists"); None } }) .collect(); Ok(sessions) } }