diff --git a/interfaces.py b/interfaces.py new file mode 100644 index 0000000..3ef5015 --- /dev/null +++ b/interfaces.py @@ -0,0 +1,172 @@ +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 = """ + + + + + + Interface Table + + + + + +
+

Core1.Doet.pixelHosting.nl Interface Table

+ + +

Codes: S - State, L - Link, u - Up, D - Down, A - Admin Down

+ + + + + + + + + + + + + + {% for entry in interface_table %} + + + + + + + + + + {% endfor %} + +
InterfaceIP AddressMAC AddressVRFMTUStatusDescription
{{ entry.interface }}{{ entry.ip_address }}{{ entry.mac_address }}{{ entry.vrf }}{{ entry.mtu }}{{ entry.status }}{{ entry.description }}
+
+ + + + + + + + + + """ + + 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) \ No newline at end of file