我刚刚开始检查PHP,为了解决这个问题,我想我应该制作一个表单,用PHP将用户添加到系统中。我尝试使用exec()运行该命令,但没有产生任何结果(我确保www-data是sudoer),所以我尝试使用system()来获得一些输出,这些输出可能会告诉我问题出在哪里,但我的两个输出都只返回了一个模糊的1和6。下面是我的代码:
<?php $username = $_POST["username"];
$password = $_POST["password"];
?>
<html>
<body>
Welcome <?php echo $username; ?><br>
Your password will be set to: <?php echo $password; ?>
<?php system("sudo useradd -m $username", $output1);
system("sudo usermod -g user $username", $output2);
?>
<br>
<?php echo $output1;?>
<br>
<?php echo $output2;?>
</body>
</html>它正确地打印出用户名和密码变量,但似乎无法运行sudo useradd和sudo usermod命令,尽管我不是完全sure.Is这里有什么可以给出一个想法,我做错了吗?
发布于 2020-02-03 21:17:24
你有没有尝试过proc_open
就像这样..。
$cmd = "echo 1";
$descriptorspec = array(
0 => array('pipe', 'r'), // stdin is a pipe that the child will read from
1 => array('file', '/var/www/html/proc-output.txt', 'a'), // stdout is a pipe that the child will write to
2 => array('file', '/var/www/html/error-output.txt', 'a') // stderr is a file to write to
);
$pipes = [];
$process = proc_open($cmd, $descriptorspec, $pipes);
$return_value = is_resource($process) ? proc_close($process) : die('this does not work');如何输入www-data密码?
https://stackoverflow.com/questions/60039868
复制相似问题