note 118111 added to function.proc-open
| From: | aaronw at catalyst dot net dot nz | Date: | Mon, 05 Oct 2015 23:16:44 +0000 |
| Subject: | note 118111 added to function.proc-open | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203913@lists.php.net to get a copy of this message | ||
If you have a CLI script that prompts you for a password via STDIN, and you need to run it from PHP,
proc_open() can get you there. It's better than doing "echo $password | command.sh",
because then your password will be visible in the process list to any user who runs "ps".
Alternately you could print the password to a file and use cat: "cat passwordfile.txt |
command.sh", but then you've got to manage that file in a secure manner.
If your command will always prompt you for responses in a specific order, then proc_open() is quite
simple to use and you don't really have to worry about blocking & non-blocking streams. For
instance, to run the "passwd" command:
<?php
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$process = proc_open(
'/usr/bin/passwd ' . escapeshellarg($username),
$descriptorspec,
$pipes
);
// It wil prompt for existing password, then new password twice.
// You don't need to escapeshellarg() these, but you should whitelist
// them to guard against control characters, perhaps by using ctype_print()
fwrite($pipes[0], "$oldpassword\n$newpassword\n$newpassword\n");
// Read the responses if you want to look at them
$stdout = fread($pipes[1], 1024);
$stderr = fread($pipes[2], 1024);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
$exit_status = proc_close($process);
// It returns 0 on successful password change
$success = ($exit_status === 0);
?>
----
Server IP: 72.52.91.14
Probable Submitter: 2404:130:0:1000:f09c:d761:95da:d475
----
Manual Page -- http://php.net/manual/en/function.proc-open.php
Edit -- https://master.php.net/note/edit/118111
Del: integrated -- https://master.php.net/note/delete/118111/integrated
Del: useless -- https://master.php.net/note/delete/118111/useless
Del: bad code -- https://master.php.net/note/delete/118111/bad+code
Del: spam -- https://master.php.net/note/delete/118111/spam
Del: non-english -- https://master.php.net/note/delete/118111/non-english
Del: in docs -- https://master.php.net/note/delete/118111/in+docs
Del: other reasons-- https://master.php.net/note/delete/118111
Reject -- https://master.php.net/note/reject/118111
Search -- https://master.php.net/manage/user-notes.php