Replaced OS ping with python ping3

This commit is contained in:
Stefan Nilsson 2025-02-11 14:53:50 +01:00
parent edbee69a8f
commit a0bdb57b79
2 changed files with 3 additions and 2 deletions

View File

@ -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):

View File

@ -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