Hey I started with rust and I am trying to implement a SSH passthrough program. I want to just pass through all stdin via ssh to a remote server and print the stdout. The problem now is the return value of some commands are not terminating but are streaming.
My current not working approach is this:
async fn start_ssh_driver(host: String, user: String, private_key_path: String, run_command: String) -> Result<(), Box<dyn std::error::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 mut channel = session.channel_open_session().await.unwrap();
// Remote stdin
tokio::spawn(async move {
println!("Waiting for data");
while let Some(msg) = channel.wait().await {
match msg {
russh::ChannelMsg::Data { ref data } => {
match str::from_utf8(data) {
Ok(v) => println!("{}", v),
Err(_) => {/* ignored */},
};
}
_ => {}
}
}
println!("Exited reading");
});
channel.exec(false, &run_command).await.unwrap();
let stdin = stdin();
let mut reader = BufReader::new(stdin);
let mut line = String::new();
loop {
println!("> ");
reader.read_line(&mut line).await.unwrap();
channel.exec(false, &line).await.unwrap();
line.clear();
}
}
I get following error: borrow of moved value: 'channel' value borrowed here after move. Is there a way to copy the channel inside the task?