init commit

This commit is contained in:
2025-06-23 09:39:45 -07:00
commit 415d1536fb
10 changed files with 333 additions and 0 deletions

47
files/lldp-api.py Normal file
View File

@ -0,0 +1,47 @@
import requests
import subprocess
from lxml import html
from flask import Flask, request, jsonify
import json
from datetime import datetime
app = Flask(__name__)
@app.route('/test', methods=['GET'])
def test():
return {"message": "Hello There"}
@app.route('/data', methods=['GET'])
def get_data():
return get_lldp_data()
def get_lldp_data():
# Set commands here
commands = {
"switch_name": "lldpcli show neighbors | grep SysName | cut -d ':' -f 2 | tr -d ' '",
"port_name": "lldpcli show neighbors | grep PortDes | cut -d ':' -f 2 | tr -d ' '",
"port_speed": "ethtool eth0 | grep Speed | cut -d ':' -f 2 | tr -d ' '",
"vlan_id": "lldpcli show neighbors details | grep VLAN | cut -d ':' -f 2 | cut -d ',' -f 1 | tr -d ' '"
}
# Create an empty dictionary to store the results
results = {}
# Loop through the commands, run them, and store the output in the results dictionary
for key, command in commands.items():
try:
# Run the command and capture the output
result = subprocess.run(command, shell=True, capture_output=True, text=True)
# Store the output in the dictionary, removing any trailing newlines
results[key] = result.stdout.strip()
except Exception as e:
# Handle any errors by storing the error message in the results
results[key] = f"Error: {str(e)}"
results['timestamp'] = datetime.now().strftime("%H:%M:%S")
# Convert the results dictionary to a JSON string
# json_output =
return json.dumps(results, indent=4)
if __name__ == '__main__':
app.run(debug=True, host='127.0.0.1', port=5000)