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)