【问题标题】:Output results from Linux command into a table with PHP使用 PHP 将 Linux 命令的结果输出到表中
【发布时间】:2014-02-04 16:42:49
【问题描述】:

我正在拼凑一个强制门户以在 Raspberry Pi 上运行并且基本工作正常,但我现在在创建一些管理页面时遇到了困难。

我实际上想要做的是在 HTML/PHP 中创建一个表,以便我能够从服务中“踢”用户。我已经有一个脚本可以执行此操作,但我正在努力将 exec 语句的结果回显到表中。

这是我设法达到的程度:

<?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>

这会输出到一个表,但不会输出到每个 IP 的一行。我希望这是一个新行,以便我可以添加第三列以包含类似

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

非常感谢任何建议。

【问题讨论】:

  • 也许您应该从您的 iptables 和 arp 命令链发布几行输出?
  • 在 PHP 中 grep 和 cut 后 iptables 的输出显示为: 00:15:5D:10:25:11 00:15:5D:10:25:02 用 implode 打破这个将它们放在单独的行中,但我希望它们转到表中的单独行。带有 cut 和 tr 的 arp 命令的输出是相似的,只是两个内联的 IP 地址用空格分隔,而不是在新行中,内爆将它们放在不同的行上。

标签: php html arrays linux html-table


【解决方案1】:

通过以下方式解决了这个问题:

<?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>

现在输出为:

<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>

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2020-04-05
    • 1970-01-01
    • 2016-04-30
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    相关资源
    最近更新 更多