@@ -7,6 +7,7 @@ use super::process_impl;
7
7
use super :: native_mux_impl;
8
8
9
9
use std:: borrow:: Cow ;
10
+ use std:: ffi:: OsString ;
10
11
use std:: fs;
11
12
use std:: path:: { Path , PathBuf } ;
12
13
use std:: process:: Stdio ;
@@ -50,6 +51,7 @@ pub struct SessionBuilder {
50
51
control_dir : Option < PathBuf > ,
51
52
config_file : Option < PathBuf > ,
52
53
compression : Option < bool > ,
54
+ user_known_hosts_file : Option < Box < Path > > ,
53
55
}
54
56
55
57
impl Default for SessionBuilder {
@@ -64,6 +66,7 @@ impl Default for SessionBuilder {
64
66
control_dir : None ,
65
67
config_file : None ,
66
68
compression : None ,
69
+ user_known_hosts_file : None ,
67
70
}
68
71
}
69
72
}
@@ -154,6 +157,18 @@ impl SessionBuilder {
154
157
self
155
158
}
156
159
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
+
157
172
/// Connect to the host at the given `host` over SSH using process impl, which will
158
173
/// spawn a new ssh process for each `Child` created.
159
174
///
@@ -300,6 +315,12 @@ impl SessionBuilder {
300
315
init. arg ( "-o" ) . arg ( format ! ( "Compression={}" , arg) ) ;
301
316
}
302
317
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
+
303
324
init. arg ( destination) ;
304
325
305
326
// we spawn and immediately wait, because the process is supposed to fork.
0 commit comments