Last active
February 3, 2026 14:19
-
-
Save thimslugga/c6b08b4152ca9b39671554ad5e851887 to your computer and use it in GitHub Desktop.
Python Script for GL-iNet KVM Comet API
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
| #!/usr/bin/env python3 | |
| # GL-iNet KVM Comet API | |
| # https://forum.gl-inet.com/t/is-there-an-api/64357 | |
| # https://docs.pikvm.org/api/ | |
| # https://github.com/guanana/pikvm-lib | |
| import os | |
| import sys | |
| import requests | |
| import urllib3 | |
| # Suppress SSL warnings | |
| urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) | |
| # Configuration | |
| # https://specifications.freedesktop.org/basedir/latest/ | |
| PASS_FILE = os.path.expanduser("~/.config/glinet-kvm/secret.txt") | |
| DEVICE_IP = "x.x.x.x" | |
| BASE_URL = f"https://{DEVICE_IP}/api" | |
| def get_password(): | |
| """Reads the password from a local file.""" | |
| try: | |
| with open(PASS_FILE, "r") as f: | |
| # .strip() removes trailing newlines or spaces | |
| return f.read().strip() | |
| except FileNotFoundError: | |
| print(f"Error: Password file not found at {PASS_FILE}") | |
| sys.exit(1) | |
| def get_session(password): | |
| """Authenticates and returns a session.""" | |
| session = requests.Session() | |
| session.verify = False | |
| login_url = f"{BASE_URL}/auth/login" | |
| login_data = {'user': 'admin', 'passwd': password} | |
| response = session.post(login_url, data=login_data) | |
| if response.status_code == 200: | |
| return session | |
| else: | |
| print(f"Error: Login failed (Status {response.status_code})") | |
| sys.exit(1) | |
| def control_power(action): | |
| password = get_password() | |
| session = get_session(password) | |
| atx_url = f"{BASE_URL}/atx/click" | |
| button_type = "power" if action == "on" else "power_long" | |
| params = {'button': button_type} | |
| print(f"[*] Sending {action.upper()} signal...") | |
| res = session.post(atx_url, params=params) | |
| if res.status_code == 200: | |
| print(f"[+] Success: {action.upper()} signal sent.") | |
| else: | |
| print(f"[-] Failed: {res.status_code}") | |
| if __name__ == "__main__": | |
| if len(sys.argv) < 2 or sys.argv[1] not in ["on", "off"]: | |
| print("Usage: python3 glinet_kvm.py [on|off]") | |
| sys.exit(1) | |
| control_power(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment