zoukankan      html  css  js  c++  java
  • 如何给ss bash 写一个 WEB 端查看流量的页面

     

    由于刚毕业的穷大学生,和朋友合租了一台服务器开了多个端口提供 ss 服务,懒得配置 ss-panel,就使用了 ss-bash 来监控不同端口的流量,但每次都要等上服务器才能看到流量使用情况,很麻烦,于是就写了个简单的页面来提供 WEB 访问,具体内容一起通过本文学习吧
     

    由于刚毕业的穷大学生,和朋友合租了一台服务器开了多个端口提供 ss 服务,懒得配置 ss-panel,就使用了 ss-bash 来监控不同端口的流量,但每次都要等上服务器才能看到流量使用情况,很麻烦,于是就写了个简单的页面来提供 WEB 访问。

    JavaScript 版本

    用 crontab 定时把流量记录文件复制到 WEB 目录下,写个 JS 脚本作数据处理。

    function successFunction(data) {
     var allRows = data.split(/
    ?
    |
    /);
     var table = '<table class="table table-hover" style=" 50%; margin: auto;">';
     for (var singleRow = 0; singleRow < allRows.length; singleRow++) {
      if (singleRow === 0) {
       table += '<thead>';
       table += '<tr>';
      } else {
       table += '<tr>';
      }
      var rowCells = allRows[singleRow].split(',');
      for (var rowCell = 0; rowCell < rowCells.length; rowCell++) {
       if (singleRow === 0) {
        table += '<th class="text-right">';
        table += rowCells[rowCell];
        table += '</th>';
       } else {
        table += '<td class="text-right">';
        table += rowCells[rowCell];
        table += '</td>';
       }
      }
      if (singleRow === 0) {
       table += '</tr>';
       table += '</thead>';
       table += '<tbody>';
      } else {
       table += '</tr>';
      }
     } 
     table += '</tbody>';
     table += '</table>';
     $('body').append(table);
    }

    首页

    <!DOCTYPE html>
    <html>
    <head>
      <title>Traffic</title>
      <script src=//cdn.bootcss.com/jquery/3.2.0/jquery.min.js></script>
      <link href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" rel="stylesheet">
      <script src="//cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      <script src="test.js"></script>
    </head>
    <body>
    <script type="text/javascript">
    var text="";
    $.ajax({
      url: "traffic.txt",
      method: "GET",
      success: function(data){
        text = data.replace(' ', '').replace(/	| /g, ',');
        successFunction(text);
      }
    })
    </script>
    </body>
    </html>

    PHP 版本

    服务器本来就安装了 PHP,所以用 PHP 也是很理所当然的事情了。

    <!DOCTYPE html>
    <html>
    <head>
      <title>Traffic</title>
      <script src=//cdn.bootcss.com/jquery/3.2.0/jquery.min.js></script>
      <link href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" rel="stylesheet">
      <script src="//cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    <body>
    <?php
    $traffic = file_get_contents("d:\traffic.txt");
    $traffic = explode("
    ", $traffic);
    echo "<table class='table table-hover' style=' 50%; margin: auto;''>
    ";
    echo "<thead>
    ";
    echo "<tr>
    ";
    for ($i=0; $i < sizeof($traffic); $i++) {
      if ($i === 0) {
        $str = preg_replace('/ /','',$traffic[0],1);
        $str = preg_replace('/ /', ',', $str);
        $str = explode(',', $str);
        for ($j=0; $j < sizeof($str); $j++) { 
          echo "<th class='text-right'>" . $str[$j] . "</th>
    ";
        }
        echo "</tr>
    ";
        echo "</thead>
    ";
        echo "<tbody>
    ";
      }
      else {
        $str = preg_replace('/	/', ',', $traffic[$i]);
        $str = explode(',', $str);
        echo "<tr>
    ";
        for ($j=0; $j < sizeof($str); $j++) { 
          echo "<td class='text-right'>" . $str[$j] . "</td>
    ";
        }
        echo "</tr>
    ";
      }
    }
    echo "</tbody>
    ";
    echo "</table>
    ";
    ?>
    </body>
    </html>

    以上所述是小编给大家介绍的给ss bash 写一个 WEB 端查看流量的页面,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

  • 相关阅读:
    2018 Wannafly summer camp Day2--New Game!
    2018 Wannafly summer camp Day8--区间权值
    2018 Wannafly summer camp Day3--Shopping
    2018 Wannafly summer camp Day3--Travel
    HDU 6354--Everything Has Changed(判断两圆关系+弧长计算)
    Spring boot-(2) Spring Boot使用
    Spring boot-(1) Spring Boot快速开始
    Quartz使用(5)
    Quartz使用(4)
    Quartz使用(3)
  • 原文地址:https://www.cnblogs.com/73tong/p/7988474.html
Copyright © 2011-2022 走看看