php shell_exec touch redirect and adduser

后端 未结 2 1422
慢半拍i
慢半拍i 2021-01-21 16:15

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

相关标签:
2条回答
  • 2021-01-21 16:56
    • 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.

    0 讨论(0)
  • 2021-01-21 17:04

    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'].'.');
    }
    
    0 讨论(0)
提交回复
热议问题