diff --git a/main.py b/main.py index 30306bf..5a30d8d 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ import sqlite3 from wakeonlan import send_magic_packet from pydantic import BaseModel from fastapi.openapi.utils import get_openapi +from ping3 import ping # Database setup def init_db(): @@ -162,8 +163,7 @@ def remove_cluster(cluster_name: str): def check_power_state(server_ip): """Check if the server is online by pinging it.""" - response = subprocess.run(["ping", "-c", "1", server_ip], stdout=subprocess.DEVNULL) - return "on" if response.returncode == 0 else "off" + return "on" if ping(server_ip, timeout=1) else "off" @app.get("/states/{server_name}") def get_power_status(server_name: str): diff --git a/requirements.txt b/requirements.txt index 9643a1c..ad60086 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,6 +6,7 @@ click==8.1.8 fastapi==0.115.8 h11==0.14.0 idna==3.10 +ping3==4.0.8 pydantic==2.10.6 pydantic_core==2.27.2 requests==2.32.3