提问人:Khadim Fall 提问时间:1/6/2023 最后编辑:Khadim Fall 更新时间:1/7/2023 访问量:193
Rust russh 异步读/写
Rust russh async read/write
问:
嘿,我从 rust 开始,我正在尝试实现 SSH 直通程序。 我只想通过 ssh 将所有 stdin 传递到远程服务器并打印 stdout。 现在的问题是某些命令的返回值不是终止的,而是流式的。
我目前不起作用的方法是这样的:
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();
}
}
我收到以下错误:。有没有办法在任务中复制频道?borrow of moved value: 'channel' value borrowed here after move
答: 暂无答案
评论
channel
channel