I try to save the output of the linux command ifconfig
into a variable and show it in my html. I have two pages, one test.html
and one ajax.php
. If i click on a link on test.html
then then an ajax request is made to ajax.php
which executes ifconfig and sends the result back to test.html
where the whole html of the body tag is replaced with the output.
I have this code in my ajax.php
. As you can see, i write the output of ifconfig
into a file, so i can read each line from the filestream and output them.
shell_exec ("/sbin/ifconfig > /tmp/ifconfig 2>&1");
$ifconfig = fopen("/tmp/ifconfig","r") or die ("Unable to open file '/tmp/ifconfig'");
while(!feof($ifconfig))
{
echo fgets($ifconfig). "<br>";
}
fclose($ifconfig);
But instead of an output like this:
eth0 Link encap:Ethernet HWaddr 0A:58:C2:71:7A:90
inet addr:172.15.47.121 Bcast:172.25.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:238319 errors:0 dropped:0 overruns:0 frame:0
TX packets:122439 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:21355179 (20.3 MiB) TX bytes:19679738 (18.7 MiB)
I get this output:
eth0 Link encap:Ethernet HWaddr 0A:58:C2:71:7A:905
inet addr:172.15.47.121 Bcast:172.15.255.223 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:23405 errors:0 dropped:14 overruns:0 frame:0
TX packets:16390 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1613474 (1.5 MiB) TX bytes:19949404 (19.0 MiB)
Can someone tell me how i can get the output in the same formatting as it actually is? Note: the addresses are not real, i changed them.
If your output should be for the website, just wrap it into <pre></pre>
tags:
echo "<pre>";
while(!feof($ifconfig))
{
echo fgets($ifconfig). "<br>";
}
echo "</pre>";
In pre
tags the most browsers use monospace
as font, so it looks a bit more like direct console output.