Question

I can't execute ifconfig with exec() in PHP on apache. I can execute all kind of commands including ip addr list but that one sucks balls so I would really need to be able to execute ifconfig.

I've read other posts and have made sure that the disable_functions is = "" [blank], but still no luck (I restarted apache ). It feels like it some kind of permission problem since all other commands works just fine.

In the first file i just do exec and then cleans up the result as I want it, and I'm quite sure that isn't where the problem is, since it works fine fore other commands

First file

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;

second file

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>";
}

SOLVED: The problem was solved by executing "/sbin/ifconfig" instead of only ifconfig

Was it helpful?

Solution

Normal user can't use ifconfig on some Gnu/Linux but can use /sbin/ifconfig. So try this

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

To be sure about the path, tape this in your terminal

whereis ifconfig
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top