首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PHP添加sudo useradd

使用PHP添加sudo useradd
EN

Stack Overflow用户
提问于 2020-02-03 21:07:36
回答 1查看 69关注 0票数 0

我刚刚开始检查PHP,为了解决这个问题,我想我应该制作一个表单,用PHP将用户添加到系统中。我尝试使用exec()运行该命令,但没有产生任何结果(我确保www-data是sudoer),所以我尝试使用system()来获得一些输出,这些输出可能会告诉我问题出在哪里,但我的两个输出都只返回了一个模糊的16。下面是我的代码:

代码语言:javascript
复制
<?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 useraddsudo usermod命令,尽管我不是完全sure.Is这里有什么可以给出一个想法,我做错了吗?

EN

回答 1

Stack Overflow用户

发布于 2020-02-03 21:17:24

你有没有尝试过proc_open

就像这样..。

代码语言:javascript
复制
        $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密码?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60039868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档