Router-tools/interfaces.py

172 lines
No EOL
5.4 KiB
Python

import json
import subprocess
from jinja2 import Template
from flask import Flask, render_template_string, jsonify
app = Flask(__name__)
def run_curl_command():
curl_command = [
"curl", "-k", "--location", "--request", "POST", "https://ip:port/show",
"--form", "data={\"op\": \"show\", \"path\": [\"interfaces\"]}",
"--form", "key=key"
]
response = subprocess.check_output(curl_command, text=True)
return json.loads(response)
def parse_interface_data(data):
interface_table = []
if "data" in data:
raw_data = data["data"]
for line in raw_data.split("\n"):
if line.startswith("Interface") and "IP Address" in line:
continue
if line.startswith("Codes:"):
continue
if line.strip().startswith('-'):
continue
if line.strip():
interface_info = line.split()
if len(interface_info) >= 6:
interface_table.append({
"interface": interface_info[0],
"ip_address": interface_info[1] if interface_info[1] != '-' else 'N/A',
"mac_address": interface_info[2],
"vrf": interface_info[3],
"mtu": interface_info[4],
"status": interface_info[5],
"description": " ".join(interface_info[6:])
})
return interface_table
def generate_html_table(interface_table):
html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interface Table</title>
<!-- Materialize CSS -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet">
<style>
.container {
margin-top: 30px;
}
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f4f4f4;
}
footer {
height: 50px;
background-color: #f4f4f4;
margin-top: 30px;
}
input[type="text"] {
margin-bottom: 20px;
padding: 10px;
width: 100%;
max-width: 400px;
}
</style>
</head>
<body>
<div class="container">
<h1 class="center-align">Core1.Doet.pixelHosting.nl Interface Table</h1>
<input type="text" id="interfaceSearch" placeholder="Search in Interface table..." onkeyup="filterTable('interfaceSearch', 'interfaceTable')">
<p>Codes: S - State, L - Link, u - Up, D - Down, A - Admin Down</p>
<table class="striped" id="interfaceTable">
<thead>
<tr>
<th>Interface</th>
<th>IP Address</th>
<th>MAC Address</th>
<th>VRF</th>
<th>MTU</th>
<th>Status</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{% for entry in interface_table %}
<tr>
<td>{{ entry.interface }}</td>
<td>{{ entry.ip_address }}</td>
<td>{{ entry.mac_address }}</td>
<td>{{ entry.vrf }}</td>
<td>{{ entry.mtu }}</td>
<td>{{ entry.status }}</td>
<td>{{ entry.description }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<!-- Footer -->
<footer></footer>
<!-- Materialize JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<script>
function filterTable(searchInputId, tableId) {
var input, filter, table, tr, td, i, j, txtValue;
input = document.getElementById(searchInputId);
filter = input.value.toUpperCase();
table = document.getElementById(tableId);
tr = table.getElementsByTagName("tr");
for (i = 1; i < tr.length; i++) {
tr[i].style.display = "none";
td = tr[i].getElementsByTagName("td");
for (j = 0; j < td.length; j++) {
if (td[j]) {
txtValue = td[j].textContent || td[j].innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
break;
}
}
}
}
}
</script>
</body>
</html>
"""
template = Template(html_template)
html_output = template.render(interface_table=interface_table)
return html_output
@app.route('/')
def interface_table_summary():
data = run_curl_command()
interface_table = parse_interface_data(data)
html_output = generate_html_table(interface_table)
return render_template_string(html_output)
@app.route('/json')
def interface_table_summary_json():
data = run_curl_command()
interface_table = parse_interface_data(data)
return jsonify({"interface_table": interface_table})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)