Hey Guys I am very new to Rust and I am trying to connect to a remote host via ssh
and just pipe all stdin and stdout between client and remote. This is my current approach that is not working at all, there is no output or input:
// Starts the ssh bridge driver
async fn start_ssh_driver(host: String, user: String, private_key_path: String, run_command: String) -> Result<(), anyhow::Error> {
// Open SSH Session
let key_pair = load_secret_key(private_key_path, None)?;
let config = client::Config {
connection_timeout: Some(Duration::from_secs(5)),
..<_>::default()
};
let config = Arc::new(config);
let sh = Client {};
let mut session = client::connect(config, SocketAddr::from_str(&host).unwrap(), sh).await?;
let _auth_res = session
.authenticate_publickey(user, Arc::new(key_pair))
.await?;
// Create new channel
let channel = session.channel_open_session().await.unwrap();
let mut stream = channel.into_stream();
// First Command
let mut first_com = run_command;
first_com.push_str("\n");
stream.write_all(&first_com.as_bytes()).await.unwrap();
// Start async stuff
let stdin = stdin();
let mut reader = BufReader::new(stdin);
let mut line_in= String::new();
let mut line_out = String::new();
match tokio::spawn(async move {
loop {
tokio::select! {
res = stream.read_to_string(&mut line_out) => {
match res {
Err(_) => break,
_ => println!("{}", line_out),
}
},
res = reader.read_line(&mut line_in) => {
match res {
Err(_) => break,
_ => stream.write_all(&line_in.as_bytes()).await.unwrap(),
}
}
}
}
anyhow::Ok::<()>(())
}).await? {
_ => {},
}
return Ok(());
}
struct Client {}
#[async_trait]
impl client::Handler for Client {
type Error = anyhow::Error;
async fn check_server_key(
self,
_server_public_key: &key::PublicKey,
) -> Result<(Self, bool), Self::Error> {
Ok((self, true))
}
}
Does any has a hint why its not working, I double checked the certificate, that should not be the issue...