Skip to content

Commit 00fbbac

Browse files
authored
Added SessionBuilder::user_known_hosts_file (#54)
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
1 parent 5c19410 commit 00fbbac

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/builder.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::process_impl;
77
use super::native_mux_impl;
88

99
use std::borrow::Cow;
10+
use std::ffi::OsString;
1011
use std::fs;
1112
use std::path::{Path, PathBuf};
1213
use std::process::Stdio;
@@ -50,6 +51,7 @@ pub struct SessionBuilder {
5051
control_dir: Option<PathBuf>,
5152
config_file: Option<PathBuf>,
5253
compression: Option<bool>,
54+
user_known_hosts_file: Option<Box<Path>>,
5355
}
5456

5557
impl Default for SessionBuilder {
@@ -64,6 +66,7 @@ impl Default for SessionBuilder {
6466
control_dir: None,
6567
config_file: None,
6668
compression: None,
69+
user_known_hosts_file: None,
6770
}
6871
}
6972
}
@@ -154,6 +157,18 @@ impl SessionBuilder {
154157
self
155158
}
156159

160+
/// Specify the path to the `known_hosts` file.
161+
///
162+
/// The path provided may use tilde notation (`~`) to refer to the user's
163+
/// home directory.
164+
///
165+
/// The default is `~/.ssh/known_hosts` and `~/.ssh/known_hosts2`.
166+
pub fn user_known_hosts_file(&mut self, user_known_hosts_file: impl AsRef<Path>) -> &mut Self {
167+
self.user_known_hosts_file =
168+
Some(user_known_hosts_file.as_ref().to_owned().into_boxed_path());
169+
self
170+
}
171+
157172
/// Connect to the host at the given `host` over SSH using process impl, which will
158173
/// spawn a new ssh process for each `Child` created.
159174
///
@@ -300,6 +315,12 @@ impl SessionBuilder {
300315
init.arg("-o").arg(format!("Compression={}", arg));
301316
}
302317

318+
if let Some(user_known_hosts_file) = &self.user_known_hosts_file {
319+
let mut option: OsString = "UserKnownHostsFile=".into();
320+
option.push(&**user_known_hosts_file);
321+
init.arg("-o").arg(option);
322+
}
323+
303324
init.arg(destination);
304325

305326
// we spawn and immediately wait, because the process is supposed to fork.

0 commit comments

Comments
 (0)