barndog
barndog

Reputation: 7173

Send Input To SCP Command from system call in C

#include <stdlib.h>
#include <unistd.h>

int main()
{
    int euid = geteuid();
    setreuid(euid, euid);
    system("echo hi");
    system("cp test_file ~/dump.txt");
    system("scp ~/dump.txt host@ipaddress:~");
    return 0;

}

The above code is a short program I wrote that copies a file to a dump file and then uses the terminal scp command to send it to someone. When the scp is used for the first time, a prompt comes up on the terminal asking if the user wants to add the key to the list of hostnames or something like that. It then will prompt for a password. How can I send input from this program so that it's received by the password prompt? What does that code look like?

EDIT: The problem that I'm facing is when there's a system call to scp, the program is paused until the entire scp command is executed, so while that's happening, input isn't being taking from the program anymore, but from the user. I don't know how to pass in a password for scp from my program.

Upvotes: 0

Views: 1381

Answers (1)

Barmar
Barmar

Reputation: 782130

Use:

system("scp -o StrictHostKeyChecking=no ~/dump.txt host@ipaddress:~");

This option will automatically add new keys to the known_hosts file without prompting.

Upvotes: 1

Related Questions