Download a file using "scp" and std::process::Command

Hi everyone! I've been fighting with this problem for 2 days already...
The problem is - I can't upload a log file from my linux server. Here is the full command if I launch it from my terminal on MacOS : "scp root@*********:/root/admin/logs/logs.txt /Users/user/Desktop/". The tricky thing is that after it's launched it asks for a password from server as an input and then downloads a file. It works when I launch it from terminal. And if I try to launch my code it always says that "Permission denied, please try again." and prints it for 3 times.

How can I make it work? Do you have any ideas? Any thoughts would be greatly appreciated.

Here is my code.

use std::io::Write;
use std::process::{Command, Stdio};

fn main() {
    match Command::new("scp")
        .stdin(Stdio::piped())
        .arg("root@********:/root/admin/logs/logs.txt")
        .arg("/Users/user/Desktop/")
        .spawn()
    {
        Ok(mut answer) => {
            println!("{:?}", answer);
            answer.stdin.as_mut().unwrap().write("my_password".as_bytes()).unwrap();
            answer.wait().unwrap();
        },
        Err(error) => {
            println!("{}", error)
        }
    }
}

Use public key encryption instead of a password.

1 Like

So it's impossible to build it with password? Honestly, I can't even believe it. It's too strange :smiley: But okay, I'll have to do it cause there is no other choice.

Utilities which prompt for passwords usually don't when stdin or stdout are redirected. Sometimes they have command-line options to bypass this check.

3 Likes

Were I in your shoes, I just would not bother. In general, public key encryption is a better choice.

1 Like

Use the -B option to block it from asking for a password, and use the -i option to specify a single key file to use.

thx, I'll give it a try!

The problem is not specific to Rust, see for example

2 Likes

I want to thank everyone who answered here. This problem is solved for me.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.