Last active
June 18, 2025 00:36
-
-
Save Yeraze/6417ea6fcb3e7b8c2ee126640b1283cb to your computer and use it in GitHub Desktop.
MeshDash - restart if no activity for 5 minutes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/python3 | |
| import requests | |
| from datetime import datetime, timedelta | |
| # Base URL for the MeshDash API | |
| BASE_URL = "https://meshdash.yeraze.online" | |
| USERNAME = "<USERNAME>" | |
| PASSWORD = "<PASSWORD>" | |
| def get_unix_time(): | |
| """Retrieve the system Unix time from api/status.""" | |
| url = f"{BASE_URL}/api/status" | |
| response = requests.get(url) | |
| response.raise_for_status() | |
| data = response.json() | |
| # Assuming the Unix time is in the 'time' field | |
| return data.get('server_time_unix') | |
| def get_most_recent_message_time(): | |
| """Retrieve the timestamp of the most recent message from api/packets/history?limit=1.""" | |
| url = f"{BASE_URL}/api/packets/history?limit=1" | |
| response = requests.get(url) | |
| response.raise_for_status() | |
| data = response.json() | |
| # Assuming the data contains a list of packets | |
| # Assuming each packet has a 'timestamp' field | |
| latest_packet = data[0] | |
| return latest_packet.get('timestamp') | |
| def restart_system(): | |
| """Call the api/system/restart endpoint.""" | |
| # First authenticate | |
| url = f"{BASE_URL}/login" | |
| payload={"username": USERNAME, | |
| "password": PASSWORD} | |
| response = requests.post(url, data=payload) | |
| print("Authentication: %s" % response.status_code) | |
| url = f"{BASE_URL}/api/system/restart" | |
| response = requests.post(url, cookies=response.cookies) | |
| response.raise_for_status() | |
| print("System restart initiated.") | |
| print("Code: %s" % response.status_code) | |
| def main(): | |
| try: | |
| system_time = get_unix_time() | |
| recent_message_time = get_most_recent_message_time() | |
| if system_time is None: | |
| print("Failed to retrieve system time.") | |
| return | |
| if recent_message_time is None: | |
| print("No recent message found.") | |
| return | |
| # Parse recent_message_time if needed | |
| # Assuming the timestamp is in Unix time format | |
| elapsed_seconds = system_time - recent_message_time | |
| print(f"System time: {system_time}, Most recent message time: {recent_message_time}, Elapsed time: {elapsed_seconds} seconds") | |
| if elapsed_seconds > 300: | |
| print("Elapsed time exceeds 5 minutes. Restarting system...") | |
| restart_system() | |
| else: | |
| print("Elapsed time is within acceptable range.") | |
| except requests.HTTPError as e: | |
| print(f"HTTP error occurred: {e}") | |
| except Exception as e: | |
| print(f"An error occurred: {e}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment