【问题标题】:Unable to run a command from php to linux无法运行从 php 到 linux 的命令
【发布时间】:2021-02-23 09:58:30
【问题描述】:

我试图从 PHP 向我的 Linux 服务器运行一个命令,但我在使用一个命令时遇到了一些问题: 这是脚本:

<?php
  $output=null;
  $retval=null;
  exec('speedtest -L -f, --format=json 2>&1', $output, $retval);
  echo "Returned with status $retval and output:\n";
  print_r($output);
?>

输出是:

Returned with status 134 and output: 
       Array ( [0] => terminate called after throwing an instance of 'std::logic_error' 
               [1] => what(): basic_string::_M_construct null not valid 
               [2] => Aborted (core dumped))

运行脚本的用户是:www-data 如果我尝试直接在终端上运行它就可以了。

command from terminal: sudo -u www-data speedtest -L
output: {"type":"serverList","timestamp":"2021-02-23T09:20:51Z","servers":[{"id":4302,"name":"Vodafone IT","location":"Milan","country":"Italy","host":"speedtest.vodafone.it","port":8080},{"id":7839,"name":"Fastweb SpA","location":"Milan","country":"Italy","host":"spd-pub-mi-01-01.fastwebnet.it","port":8080},{"id":11427,"name":"EOLO","location":"Milan","country":"Italy","host":"test.eolo.it","port":8080},{"id":1434,"name":"CWNET","location":"Milan","country":"Italy","host":"speedtest.cheapnet.it","port":8080},{"id":3667,"name":"TIM SpA","location":"Milan","country":"Italy","host":"speedtestmi1.telecomitalia.it","port":8080},{"id":8211,"name":"CDLAN S.R.L.","location":"Milan","country":"Italy","host":"speedtest.cdlan.it","port":8080},{"id":11675,"name":"Italiaonline Supernap","location":"Milan","country":"Italy","host":"speedtest-supernap.italiaonline.it","port":8080},{"id":20551,"name":"Optima Italia","location":"Milan","country":"Italy","host":"mi-speedtest.optimaitalia.com","port":8080},{"id":26415,"name":"P-Lab","location":"Milan","country":"Italy","host":"speed.speedymilan.net","port":8080},{"id":19177,"name":"Seeweb","location":"Milan","country":"Italy","host":"ookla-mil.seeweb.it","port":8080}]}

如果我运行另一个命令,它会完美运行:

<?php
  $output=null;
  $retval=null;
  exec('whoami', $output, $retval);
  sleep(1);
  echo "Returned with status $retval and output:\n";
  print_r($output);
?>

输出是:

Returned with status 0 and output: Array ( [0] => www-data )

大家有什么建议吗?

【问题讨论】:

  • www-data 没有外壳,直到您设置一个。不要那样做。为此创建一个额外的用户并使用 su。
  • @MarkusZeller 感谢您的回答。你能解释一下如何正确设置吗?我试图这样做,但我被卡住了。
  • 当`sudo -u www-data speedtest -L`正常工作时,也许我错了。如果您尝试exec('sudo -u www-data speedtest -L -f, --format=json 2&gt;&amp;1', $output, $retval);,会发生什么?也许这是一个 apache 模块的东西?
  • @MarkusZeller 它说:“返回状态 1 并输出:www-data 不在 sudoers 文件中。将报告此事件。”。
  • @MarkusZeller 它说:“返回状态 1 并输出:”

标签: php html linux


【解决方案1】:

我想出了一种让它工作的方法,但可能不是最好的:

<?php
// attributes
        private $output;
        private $retval;
        private $command;

        // methods
        function get_serverUpdates($user, $password) {
            $command = 'echo '.$password.' | su -l '.$user.'-s /bin/bash -c "speedtest -L -f, --format=json" 2>&1';
            $resl = exec($command, $output, $retval);
            return json_decode(substr($resl, 10));
        }
?>

它确实返回了正确的信息。

我认为问题在于,正如@MarkusZeller 所说,www-data 没有运行所有已安装程序的权限。

【讨论】:

    猜你喜欢
    • 2012-02-01
    • 2019-07-10
    • 2013-12-28
    • 1970-01-01
    • 2018-04-25
    • 2014-01-15
    • 2022-01-18
    • 1970-01-01
    相关资源
    最近更新 更多