php shell_exec touch redirect and adduser

[亡魂溺海] 提交于 2019-12-02 01:20:14
  • 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.

Christian

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'].'.');
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!