PHP输出在终端中正确但在浏览器中没有

问题描述 投票:1回答:1

我刚刚开始在php中使用shell_exec并且停留在这一点上。下面是我的php脚本,它正确地在终端中运行但不在浏览器中运行。

<?php
  echo shell_exec("ssh -tq root@192.168.31.5 \"whoami\"");
?>

终端输出是

$ php /var/www/html/monitor/ssh.php 
root

但在浏览器中,Browser Output

有趣的是,whoami就像魅力一样

<?php
    echo shell_exec("whoami");
?>

enter image description here

任何建议表示赞赏。谢谢!

编辑: - 使用OB_START()和OB_GET_CONTENT

<?php
  ob_start();
  echo shell_exec("ssh -tq root@192.168.31.5 \"whoami\"");
  $out1 = ob_get_contents();
  ob_end_clean();
  var_dump($out1);
?>

输入终端: -

php /var/www/html/monitor/ssh.php 
string(6) "root"

浏览器输出(铬): -

string(0) ""
ssh shell-exec php-7.2 sshpass
1个回答
0
投票

那是因为在CLI中你是从SSH执行脚本的用户(在你的情况下是root)但是在浏览器中,执行脚本的是你的WebServer(apache / nginx)。要让您在浏览器中获取root作为输出,您可能需要查看ob_start ob_get_contents ob_flush函数。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.