Skip to content
Advertisement

Output results from Linux command into a table with PHP

I’m mid way through clobbering together a captive portal to run on a Raspberry Pi and have the basics working, but I’m now stumbling whilst creating some administration pages.

What I am essentially trying to do is create a table in HTML/PHP so that I have the ability to “kick” users from the service. I have a script already in place that does this, but I’m struggling to echo the results of an exec statement into a table.

This is how far I’ve managed to get:

<?php
    $mac = array();
    exec( "sudo iptables -L -t mangle | grep MAC | cut -d' ' -f37", $mac );
    $ip = array();
    exec( "sudo arp -i eth1 -a | cut -d' ' -f2 | tr -d '()'", $ip );
?>
<style>
    table, tr, td, th
    {
        font-family:verdana,sans-serif;
        font-size:11px;
        border:1px solid black;
        border-collapse:collapse;
        padding:5px;
    }
</style>
<html>
    <body>
        <div style="font-family:verdana,sans-serif;font-size:11px;">
            Currently connected:<br><br>
            <table>
                <tr>
                    <th>MAC</th>
                    <th>IP</th>
                </tr>
                <tr>
                    <td><?php echo implode("<br />", $mac); ?></td>
                    <td><?php echo implode("<br />", $ip); ?></td>
                </tr>
            </table>
        </div>
    </body>
</html>

This outputs to a table, but not into a row per IP. I’d like this to be a new row so that I can then add a third column to contain something like

echo "<td><a href='/scripts/block.php?IP=" . $row['IP'] . "'>Block</a></td>";

Any suggestions greatly appreciated.

Advertisement

Answer

Got this working with the following:

<?php
    $mac = array();
    exec( "sudo iptables -L -t mangle | grep MAC | cut -d' ' -f37", $mac );
    $ip = array();
    exec( "sudo arp -i eth1 -a | cut -d' ' -f2 | tr -d '()'", $ip );
?>

<style>
    table, tr, td, th
    {
        font-family:verdana,sans-serif;
        font-size:11px;
        border:1px solid black;
        border-collapse:collapse;
        padding:5px;
    }
</style>
<html>
    <body>
        <table>
            <tr>
                <th>MAC</th>
                <th>IP</th>
            </tr>
            <?php foreach(array_combine($ip, $mac) as $ipaddress => $macaddress){
    echo "<tr><td>".$ipaddress."</td><td>".$macaddress."</td></tr>";
}
?>
        </table>
    </body>
</html>

Which now outputs as:

<style>
    table, tr, td, th
    {
        font-family:verdana,sans-serif;
        font-size:11px;
        border:1px solid black;
        border-collapse:collapse;
        padding:5px;
    }
</style>
<html>
    <body>
        <table>
            <tr>
                <th>MAC</th>
                <th>IP</th>
            </tr>
            <tr><td>10.0.128.107</td><td>00:15:5D:10:25:11</td></tr><tr><td>10.0.128.106</td><td>00:15:5D:10:25:02</td></tr>        </table>
    </body>
</html>

Thanks for the assistance!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement