php shell_exec touch 重定向和 adduser

发布于 2024-11-02 06:30:30 字数 1551 浏览 2 评论 0原文

我试图最终使用 php 的 shell_exec 函数来创建新的 Linux 用户。然而,即使进行调试,我也遇到了问题。这是我的代码

<?PHP 

function adduser($username,$password,$server){ 
    try{
        //3 debug statements
        $output=shell_exec("pwd"); 
        echo $output;
       shell_exec("touch test.txt");

        //3 debug statements are requested by Christian
        echo '<pre>';
        print_r(execute('pwd'));
        print_r(execute('touch test.txt'));

        //actuall code
        $output=shell_exec("ssh root@$server \"adduser $username; echo $password | passwd $username --stdin\"");
    }
    catch(Exception $e){
        echo 'could not add user '.$e->getMessage();
    }
} 

$servers = array('192.168.1.8'); 

foreach($servers as $server){ 
    echo $_GET['USER']."   ".$_GET['PASSWORD'];
    adduser($_GET['USER'],$_GET['PASSWORD'],$server); 
}

try-catch 语句不执行任何操作,使我相信 shell 错误不会作为 PHP 错误传播(Python 也是如此)。 $output=shell_exec("pwd") 行返回正确的目录。然而,shell_exec("touch test.txt") 行无法创建文件 test.txt (即使我给出完整路径 '/home/user/.../test.txt ')。

不用说,添加用户的实际代码也不起作用。

编辑我设法修复了一些代码。 touch test.txt 错误是由于权限不足造成的。 Apache 使用用户 www-data 登录,我只是为该用户创建了一个主文件夹,并确保触摸该主文件夹中的文件。

然而,按照 Christian 的要求添加三个调试语句现在引起了问题。

编辑 经过进一步检查,这与以用户 www-data 登录时无法以 root 身份 ssh 有关。 ssh -v 返回 debug1: read_passphrase: 无法打开 /dev/tty: No such device or address。我的猜测是 ssh 正在询问通用的“您想将 xxx 永久添加到known_hosts吗”,但我无法回应。是否可以手动将用户添加到已知主机列表中?

I am trying to ultimately use php's shell_exec function to create new Linux users. I am, however, running into problems even with the debugging. Here is my code

<?PHP 

function adduser($username,$password,$server){ 
    try{
        //3 debug statements
        $output=shell_exec("pwd"); 
        echo $output;
       shell_exec("touch test.txt");

        //3 debug statements are requested by Christian
        echo '<pre>';
        print_r(execute('pwd'));
        print_r(execute('touch test.txt'));

        //actuall code
        $output=shell_exec("ssh root@$server \"adduser $username; echo $password | passwd $username --stdin\"");
    }
    catch(Exception $e){
        echo 'could not add user '.$e->getMessage();
    }
} 

$servers = array('192.168.1.8'); 

foreach($servers as $server){ 
    echo $_GET['USER']."   ".$_GET['PASSWORD'];
    adduser($_GET['USER'],$_GET['PASSWORD'],$server); 
}

The try-catch statements don't do anything, leading me to believe that shell errors are not propagated as PHP errors (Python is this way also). The line $output=shell_exec("pwd") returns the correct directory. The line shell_exec("touch test.txt"), however, fails to create th file test.txt (even if I give the full path '/home/user/.../test.txt').

Needless to say, the actual code for adding users does not work also.

EDIT I managed to fix some of the code. The touch test.txt error was as a result of insufficient permissions. Apache logs in with user www-data, I simply created a home folder for that user, and made sure to touch a file in that home folder.

The addition of the three debug statements, as per Christian's requests, are however causing problems now.

EDIT Upon further inspection, it has to do with the inability to ssh as root when logging in as user www-data. ssh -v returns debug1: read_passphrase: can't open /dev/tty: No such device or address. My guess is that ssh is asking that generic "would you like to permanently add xxx to known_hosts" but I can't respond. Is there anyway to manually add a user to the known hosts list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故笙诉离歌 2024-11-09 06:30:30
  • 许多(大多数?) PHP 的内部函数不会抛出异常,而是会引发错误。我认为您不会看到 shell_exec() 抛出异常,
  • 我可能会 var_dump() 返回值,只是为了确保您明确知道它返回的内容。
  • 我还建议研究 escapeshellarg() 等函数以避免出现问题与您的输入。

一般情况下,我不会让 PHP 顺序执行多个命令,而是编写一个 shell 脚本来执行我需要的所有操作,然后从 PHP 调用它。调试时链条中少了一个环节,我发现我轻松了很多。

关于 SSH 命令本身,由于 apache 是以 www-data 身份执行的,那么它如何以 root 身份登录到有问题的机器呢?您是否已将 apache 用户的密钥添加到远程计算机。

  • Many (most?) of PHP's internal functions don't throw exceptions, they raise errors. I don't think you will ever see an exception thrown by shell_exec()
  • I might var_dump() the return value, just to ensure you're explicitly aware of what it's returned.
  • I would also suggest looking into functions like escapeshellarg() to avoid issues with your input.

As a general case, rather than having PHP execute several commands sequentially, I write a shell script that does everything I need, then call it from PHP. There's one fewer link in the chain when debugging, and I find I a lot easier.

With regards to your SSH command itself, since apache is executing as www-data, how is it logging into the machine in question as root? Have you added the apache user's key to the remote machine.

記柔刀 2024-11-09 06:30:30

请使用我的函数 在这里找到

运行以下代码并告诉我们其输出:

echo '<pre>';
print_r(execute('pwd'));
print_r(execute('touch test.txt'));

编辑: 如果您想让我的脚本更加面向 OO:

function execute_o($cmd,$in){
    $out=execute($cmd,$in);
    if($out['return']!=0)
        throw new Exception('Error '.$out['return'].': '.$out['stderr'].'.');
}

Please use my function found here.

Run the following code and tell us its output:

echo '<pre>';
print_r(execute('pwd'));
print_r(execute('touch test.txt'));

Edit: If you want to make my script more OO oriented:

function execute_o($cmd,$in){
    $out=execute($cmd,$in);
    if($out['return']!=0)
        throw new Exception('Error '.$out['return'].': '.$out['stderr'].'.');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文