MENU

给 ss-bash 写一个 WEB 端查看流量的页面

March 22, 2017 • Read: 5030 • 代码

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

JavaScript 版本

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

function successFunction(data) {
  var allRows = data.split(/\r?\n|\r/);
  var table = '<table class="table table-hover" style="width: 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="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(/\t| /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="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("\n", $traffic);
echo "<table class='table table-hover' style='width: 50%; margin: auto;''>\n";
echo "<thead>\n";
echo "<tr>\n";
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>\n";
        }
        echo "</tr>\n";
        echo "</thead>\n";
        echo "<tbody>\n";
    }
    else {
        $str = preg_replace('/\t/', ',', $traffic[$i]);
        $str = explode(',', $str);
        echo "<tr>\n";
        for ($j=0; $j < sizeof($str); $j++) { 
            echo "<td class='text-right'>" . $str[$j] . "</td>\n";
        }
        echo "</tr>\n";
    }
}
echo "</tbody>\n";
echo "</table>\n";
?>
</body>
</html>

最终效果

Archives QR Code
QR Code for this page
Tipping QR Code
Leave a Comment

2 Comments
  1. 学习了

  2. Bear_lele Bear_lele

    求一个统计流量的sh脚本@(太开心)