【发布时间】: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>&1', $output, $retval);,会发生什么?也许这是一个 apache 模块的东西? -
@MarkusZeller 它说:“返回状态 1 并输出:www-data 不在 sudoers 文件中。将报告此事件。”。
-
@MarkusZeller 它说:“返回状态 1 并输出:”