douweng3564 2014-02-04 16:42
浏览 81
已采纳

使用PHP将Linux命令输出到表中

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.

  • 写回答

1条回答 默认 最新

  • duanmei2459 2014-02-07 09:37
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看