Unable to use std::process::Command - No such file or directory

会有一股神秘感。 提交于 2021-01-27 21:50:13

问题


I am trying to use the following Rust code to connect to EC2 instances.

#[test]
fn client_ssh_timeout2() {
    match Command::new("/usr/bin/ssh -i /tmp/.ssh/25.pem ubuntu@ip").spawn() {
        Ok(_) => println!("Able to ssh"),
        Err(e) => println!("{:?}", e),
    };
}

But I am getting the following error

Error { repr: Os { code: 2, message: "No such file or directory" } }

Has anyone been able to use std::process::Command or any other Rust library to connect to EC2 instances using PEM files? I tried using ssh2-rs(libssh2) but couldn't connect to EC2 instances.


回答1:


This appears to be a misunderstanding of how to use std::process:Command. Command::new takes just the program:

fn new<S: AsRef<OsStr>>(program: S) -> Command

Command::arg or Command::args are used to provide the arguments.

You will want something like

Command::new("/usr/bin/ssh")
    .args(&["-i", "/tmp/.ssh/25.pem", "ubuntu@ip")
    .spawn()


来源:https://stackoverflow.com/questions/40836973/unable-to-use-stdprocesscommand-no-such-file-or-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!