Nikita U.
Nikita U.

Reputation: 3618

How to pass answers to PHP proc_open function?

I'm trying to make php script that will execute scp command and deliver some file. The problem is that command requires answer "yes/no" question and password. I can't get how to pass that from script.

My code:

$command = "scp $filename {$config['Username']}@{$config['Server']}:{$config['Basedir']}";
echo $command;

$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("file", sys_get_temp_dir() . "/error.txt", "w"),  // stderr is a file to write to
);

$cwd = sys_get_temp_dir();
$env = array();
$process = proc_open($command, $descriptorspec, $pipes, $cwd, $env);
fwrite($pipes[0], "yes\n");
fwrite($pipes[0], $config['Password'] . "\n");
proc_close($process);

It does not work. The script ask me "Are you sure you want to continue connecting (yes/no)?" every time.

Upvotes: 2

Views: 593

Answers (1)

jami
jami

Reputation: 300

This is a security confirmation, because you connecting to a remote server that is n ot listed in your known hosts file. Please open the ssh or scp connection by hand. ssh will ask you to accept the key from the remote server. After that your command should work.

There are also options that help you doing ssh batch processing.

-oStrictHostKeyChecking=[yes|no]
-oUserKnownHostsFile=[/dev/null]

I would not recommend this. ssh ask you for a very very good reason!

Upvotes: 1

Related Questions