Delete bgp-summ.py

This commit is contained in:
Blackwhitebear8 2025-06-09 20:48:29 +02:00
parent a6f2c177d9
commit 38bfa7c133

View file

@ -1,280 +0,0 @@
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\": [\"bgp\", \"summ\"]}",
"--form", "key=key"
]
response = subprocess.check_output(curl_command, text=True)
return json.loads(response)
def parse_bgp_data(data):
ipv4_section = ""
ipv6_section = ""
ipv4_info = {}
ipv6_info = {}
if "data" in data:
raw_data = data["data"]
if "IPv4 Unicast Summary" in raw_data:
ipv4_raw = raw_data.split("IPv4 Unicast Summary (VRF default):")[1]
ipv4_section = ipv4_raw.split("IPv6 Unicast Summary (VRF default):")[0].strip()
ipv4_info = extract_bgp_info(ipv4_raw)
if "IPv6 Unicast Summary" in raw_data:
ipv6_raw = raw_data.split("IPv6 Unicast Summary (VRF default):")[1].strip()
ipv6_section = ipv6_raw
ipv6_info = extract_bgp_info(ipv6_raw)
def process_peers(peer_data):
peers = []
for line in peer_data.split("\n"):
if line.strip().startswith("Neighbor"):
continue
if line.strip():
peer_info = line.split()
if len(peer_info) >= 12:
peers.append({
"neighbor": peer_info[0],
"version": peer_info[1],
"as_number": peer_info[2],
"msg_received": peer_info[3],
"msg_sent": peer_info[4],
"table_version": peer_info[5],
"in_queue": peer_info[6],
"out_queue": peer_info[7],
"up_down": peer_info[8],
"state_pfx_rcd": peer_info[9],
"prefix_sent": peer_info[10],
"description": " ".join(peer_info[11:])
})
return peers
ipv4_peers = process_peers(ipv4_section)
ipv6_peers = process_peers(ipv6_section)
return ipv4_info, ipv4_peers, ipv6_info, ipv6_peers
def extract_bgp_info(raw_data):
lines = raw_data.split("\n")
info = {}
for line in lines:
if "BGP router identifier" in line:
parts = line.split(",")
info["router_id"] = parts[0].split("identifier")[1].strip()
info["local_as"] = parts[1].split("number")[1].strip().split(" ")[0]
if "vrf-id" in parts[-1]:
info["vrf_id"] = parts[-1].split("vrf-id")[1].strip()
if "BGP table version" in line:
info["table_version"] = line.split("version")[1].strip()
if "RIB entries" in line:
parts = line.split(",")
info["rib_entries"] = parts[0].split("entries")[1].strip()
info["rib_memory"] = parts[1].split("using")[1].strip()
if "Peers" in line:
parts = line.split(",")
info["peers"] = parts[0].split("Peers")[1].strip()
info["peers_memory"] = parts[1].split("using")[1].strip()
return info
def generate_html_table(ipv4_info, ipv4_peers, ipv6_info, ipv6_peers):
html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BGP Peer Summary</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 BGP summary</h1>
<h4>IPv4 Unicast Summary</h4>
<p>BGP Router ID: {{ ipv4_info.router_id }}, Local AS Number: {{ ipv4_info.local_as }} VRF ID: {{ ipv4_info.vrf_id }}</p>
<p>BGP Table Version: {{ ipv4_info.table_version }}</p>
<p>RIB Entries: {{ ipv4_info.rib_entries }}, using {{ ipv4_info.rib_memory }}</p>
<p>Peers: {{ ipv4_info.peers }}, using {{ ipv4_info.peers_memory }}</p>
<input type="text" id="ipv4Search" placeholder="Search in IPv4 table..." onkeyup="filterTable('ipv4Search', 'ipv4Table')">
<table class="striped" id="ipv4Table">
<thead>
<tr>
<th>Neighbor</th>
<th>Version (V)</th>
<th>AS Number</th>
<th>Messages Received</th>
<th>Messages Sent</th>
<th>Table Version</th>
<th>Inbound Queue (InQ)</th>
<th>Outbound Queue (OutQ)</th>
<th>Up/Down</th>
<th>State/PfxRcd</th>
<th>Prefix Sent (PfxSnt)</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{% for peer in ipv4_peers %}
<tr>
<td>{{ peer.neighbor }}</td>
<td>{{ peer.version }}</td>
<td>{{ peer.as_number }}</td>
<td>{{ peer.msg_received }}</td>
<td>{{ peer.msg_sent }}</td>
<td>{{ peer.table_version }}</td>
<td>{{ peer.in_queue }}</td>
<td>{{ peer.out_queue }}</td>
<td>{{ peer.up_down }}</td>
<td>{{ peer.state_pfx_rcd }}</td>
<td>{{ peer.prefix_sent }}</td>
<td>{{ peer.description }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<h4>IPv6 Unicast Summary</h4>
<p>BGP Router ID: {{ ipv6_info.router_id }}, Local AS Number: {{ ipv6_info.local_as }} VRF ID: {{ ipv6_info.vrf_id }}</p>
<p>BGP Table Version: {{ ipv6_info.table_version }}</p>
<p>RIB Entries: {{ ipv6_info.rib_entries }}, using {{ ipv6_info.rib_memory }}</p>
<p>Peers: {{ ipv6_info.peers }}, using {{ ipv6_info.peers_memory }}</p>
<input type="text" id="ipv6Search" placeholder="Search in IPv6 table..." onkeyup="filterTable('ipv6Search', 'ipv6Table')">
<table class="striped" id="ipv6Table">
<thead>
<tr>
<th>Neighbor</th>
<th>Version (V)</th>
<th>AS Number</th>
<th>Messages Received</th>
<th>Messages Sent</th>
<th>Table Version</th>
<th>Inbound Queue (InQ)</th>
<th>Outbound Queue (OutQ)</th>
<th>Up/Down</th>
<th>State/PfxRcd</th>
<th>Prefix Sent (PfxSnt)</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{% for peer in ipv6_peers %}
<tr>
<td>{{ peer.neighbor }}</td>
<td>{{ peer.version }}</td>
<td>{{ peer.as_number }}</td>
<td>{{ peer.msg_received }}</td>
<td>{{ peer.msg_sent }}</td>
<td>{{ peer.table_version }}</td>
<td>{{ peer.in_queue }}</td>
<td>{{ peer.out_queue }}</td>
<td>{{ peer.up_down }}</td>
<td>{{ peer.state_pfx_rcd }}</td>
<td>{{ peer.prefix_sent }}</td>
<td>{{ peer.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(ipv4_info=ipv4_info, ipv4_peers=ipv4_peers, ipv6_info=ipv6_info, ipv6_peers=ipv6_peers)
return html_output
@app.route('/')
def bgp_peer_summary():
data = run_curl_command()
ipv4_info, ipv4_peers, ipv6_info, ipv6_peers = parse_bgp_data(data)
html_output = generate_html_table(ipv4_info, ipv4_peers, ipv6_info, ipv6_peers)
return render_template_string(html_output)
@app.route('/json')
def bgp_peer_summary_json():
data = run_curl_command()
ipv4_info, ipv4_peers, ipv6_info, ipv6_peers = parse_bgp_data(data)
json_data = {
"ipv4_info": ipv4_info,
"ipv4_peers": ipv4_peers,
"ipv6_info": ipv6_info,
"ipv6_peers": ipv6_peers
}
return jsonify(json_data)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)