Skip to content

Commit

Permalink
Add SshShell::with_any_key (#4)
Browse files Browse the repository at this point in the history
This checks every ssh key in $HOME/.ssh for a key that works with the desired host.

Signed-off-by: Bijan Tabatabai <bijan311@gmail.com>
  • Loading branch information
BijanT committed Sep 11, 2020
1 parent 72035d3 commit 38b02d0
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions spurs/src/lib.rs
Expand Up @@ -234,6 +234,48 @@ impl SshShell {
SshShell::with_key(username, remote, home.join(DEFAULT_KEY_SUFFIX))
}

/// Returns a shell connected via the first private key found at `$HOME/.ssh/` to the given
/// SSH server as the given user.
///
/// ```rust,ignore
/// SshShell::with_any_key("markm", "myhost:22")?;
/// ```
pub fn with_any_key<A: Copy + ToSocketAddrs + std::fmt::Debug>(
username: &str,
remote: A,
) -> Result<Self, SshError> {
const DEFAULT_KEY_DIR: &str = ".ssh/";
let home = if let Some(home) = dirs::home_dir() {
home
} else {
return Err(SshError::KeyNotFound {
file: DEFAULT_KEY_DIR.into(),
});
};
let key_dir = home.join(DEFAULT_KEY_DIR);

for entry in std::fs::read_dir(&key_dir)? {
let entry = entry?;
let name = entry.file_name().into_string().unwrap();

// To find the private keys, find the public keys then chop off ".pub"
if !name.ends_with(".pub") {
continue;
}

let (priv_key, _) = name.split_at(name.len() - 4);
let shell = SshShell::with_key(username, remote, key_dir.join(priv_key));

if shell.is_ok() {
return shell;
}
}

Err(SshError::KeyNotFound {
file: DEFAULT_KEY_DIR.into(),
})
}

/// Returns a shell connected via private key file `key` to the given SSH server as the given
/// user.
///
Expand Down

0 comments on commit 38b02d0

Please sign in to comment.