由于剛畢業的窮大學生,和朋友合租了一臺服務器開了多個端口提供 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 rel="external nofollow" rel="external nofollow" rel="stylesheet"> <script src="https://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 rel="external nofollow" rel="external nofollow" rel="stylesheet"> <script src="https://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>
新聞熱點
疑難解答
圖片精選