Untitled
# -*- coding: utf-8 -*- import json from flask import Flask, render_template, request, jsonify from wakeonlan import send_magic_packet import subprocess import platform import time from threading import Thread app = Flask(__name__) # Initialize saved PCs try: with open("pcs.json", "r") as file: pcs = json.load(file) except FileNotFoundError: pcs = [] def save_pcs(): """Save the list of registered PCs to pcs.json.""" with open("pcs.json", "w") as file: json.dump(pcs, file, indent=4) def ping_device(ip): """Check if a device is reachable via ping.""" param = "-n" if platform.system().lower() == "windows" else "-c" command = ["ping", param, "1", ip] try: subprocess.check_output(command, stderr=subprocess.STDOUT, universal_newlines=True) return True except subprocess.CalledProcessError: return False def determine_status(pc): """Determine the status and uptime of a device.""" ip = pc["ip"] try: if ping_device(ip): if "last_online" not in pc: pc["last_online"] = time.time() uptime_seconds = int(time.time() - pc["last_online"]) pc["uptime"] = f"{uptime_seconds // 3600}h {uptime_seconds % 3600 // 60}m {uptime_seconds % 60}s" pc["status"] = "Online" else: pc["uptime"] = "N/A" pc["status"] = "Offline" pc.pop("last_online", None) except Exception as e: pc["status"] = "Error" pc["uptime"] = "N/A" def scan_network(): """Scan the local network for devices.""" devices = [] try: result = subprocess.run(['sudo', 'arp-scan', '--localnet'], stdout=subprocess.PIPE, text=True) for line in result.stdout.split("\n"): if "192.168." in line: parts = line.split() if len(parts) >= 3: ip = parts[0] mac = parts[1] name = parts[2] if len(parts) > 2 else "Unknown" devices.append({"ip": ip, "mac": mac, "name": name}) except Exception as e: print(f"[ERROR] Network scan failed: {e}") return devices def update_status(): """Continuously update the status of saved devices.""" while True: for pc in pcs: determine_status(pc) time.sleep(5) # Start the status update thread Thread(target=update_status, daemon=True).start() @app.route("/") def home(): """Render the main page.""" return render_template("index.html") @app.route("/api/pcs", methods=["GET"]) def get_pcs(): """Return the current list of saved PCs and available network devices.""" return jsonify({"saved": pcs, "available": scan_network()}) @app.route("/add", methods=["POST"]) def add_pc(): """Add a device to the list of saved PCs.""" name = request.form["name"] device_data = request.form["device"] ip, mac = device_data.split("|") pcs.append({"name": name, "ip": ip, "mac": mac, "status": "Offline", "uptime": "N/A"}) save_pcs() return jsonify({"success": True}) @app.route("/wake", methods=["POST"]) def wake_pc(): """Send a Wake-on-LAN packet to a device.""" mac = request.form["mac"] try: send_magic_packet(mac) return jsonify({"success": True, "message": f"Wake-on-LAN packet sent to {mac}."}) except Exception as e: return jsonify({"success": False, "error": str(e)}) @app.route("/delete", methods=["POST"]) def delete_pc(): """Remove a device from the list of saved PCs.""" name = request.form["name"] global pcs pcs = [pc for pc in pcs if pc["name"] != name] save_pcs() return jsonify({"success": True}) if __name__ == "__main__": app.run(host="0.0.0.0", port=5000, debug=True)
Leave a Comment