Hkachhia
Hkachhia

Reputation: 4539

How to get whole output of php system function

I want to get all the router list of my network using php on linux. I have tried php exec and system function but it is give only one router in output.

How to get whole list of router?

$last_line = system('iwlist scan', $retval);
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;

$last_line = system('iwlist scan | grep ESSID', $retval);

echo '

<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;

Upvotes: 0

Views: 2333

Answers (2)

Paul
Paul

Reputation: 6871

Use passthru to retrieve all of the output.

You must understand that there are different output streams to consider here:

  • iwlist will probably produce text on and (due to other interfaces that don't support wireless)
  • grep will only receive the output through the pipe from
  • passthru will receive both and from iwlist and grep.

You can redirect the output so that you only get the successful output that has been grep'ed. The whole thing then becomes:

echo passthru('iwlist scan 2>/dev/null | grep ESSID');

Upvotes: 1

Arjan
Arjan

Reputation: 9874

You can use exec() instead of system. The second parameter for exec() is an (optional) array. Each line of output that your command returns will be in that array.

$output = array();
exec('iwlist scan', $output, $retval);
print_r($output);

Upvotes: 0

Related Questions