1

我无法在 apache 上的 PHP 中使用 exec() 执行 ifconfig。我可以执行所有类型的命令,包括 ip addr list,但是那个很糟糕,所以我真的需要能够执行 ifconfig。

我已阅读其他帖子并确保 disable_functions 为 = "" [空白],但仍然没有运气(我重新启动了 apache )。感觉像是某种权限问题,因为所有其他命令都可以正常工作。

在第一个文件中,我只是执行 exec,然后根据需要清理结果,我很确定这不是问题所在,因为它对其他命令运行良好

第一个文件

function execute($command){
  exec($command, $output);

  $output_array;                                    // each row contains a array representing all elements in a output row
  for ($i = 0; $i < sizeof($output); $i++){
    $tmp_array = explode(" ",$output[$i]);      
    $output_array[$i] = $tmp_array;
}
// clear the array from all ""
  $clean_array;                             
  for ($i = 0; $i < sizeof($output_array); $i++){
    $tmp;
    $index = 0;
    for ($j = 0; $j < sizeof($output_array[$i]); $j++){
        if (strlen($output_array[$i][$j]) > 0){
            $tmp[$index++] = $output_array[$i][$j];
        }
    }
    $clean_array[$i] = $tmp;
  }
  return $clean_array;

第二个文件

include "../app_includes/application_functions.php";
$command = "ifconfig";
//$command = "ip addr list";
$arr = execute($command);
// print the result
for ($i = 0; $i < sizeof($arr); $i++){
    for ($j = 0; $j < sizeof($arr[$i]); $j++){
        echo $arr[$i][$j]." ";
    }
    echo "<br>";
}

已解决:通过执行“/sbin/ifconfig”而不是仅执行 ifconfig 解决了问题

4

1 回答 1

4

普通用户不能在某些 Gnu/Linux 上使用 ifconfig,但可以使用 /sbin/ifconfig。所以试试这个

<?php
$command="/sbin/ifconfig";
exec($command, $output);
?>

要确定路径,请将其粘贴在终端中

whereis ifconfig
于 2014-05-07T08:57:37.083 回答