6

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.

kmdreko
  • 42,554
  • 6
  • 57
  • 106
mchapala
  • 91
  • 1
  • 5
  • See also [Connect SSH command to stdout](http://stackoverflow.com/q/32641699/155423) and [TCP tunnel over SSH in Rust](http://stackoverflow.com/q/36865667/155423) for tangentially related questions. – Shepmaster Nov 28 '16 at 04:08
  • @Shepmaster Thanks. Initially I started with ssh2-rs based on the above links but encountered issues with pem based authorization. So refactored the code to make of use std::process:Command – mchapala Nov 28 '16 at 04:48

1 Answers1

17

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()
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366