import requests import logging from datetime import datetime from hcloud import Client from hcloud.zones.domain import ZoneRecord # --- KONFIGURATION --- TOKEN = "DEIN_HETZNER_API_TOKEN" DOMAIN = "domain.de" SUBDOMAINS = ["forms", "api", "cloud"] # Hier einfach erweitern LOG_FILE = "/home/ddns_update.log" # --- LOGGING SETUP --- logging.basicConfig( filename=LOG_FILE, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S' ) def update_ddns(): try: client = Client(token=TOKEN) # 1. Aktuelle öffentliche IP abrufen try: current_ip = requests.get('https://api.ipify.org', timeout=10).text.strip() except Exception as e: logging.error(f"Konnte öffentliche IP nicht abrufen: {e}") return # 2. Zone laden zone = client.zones.get(DOMAIN) # 3. Alle Subdomains durchgehen for sub in SUBDOMAINS: try: rrset = client.zones.get_rrset(zone, name=sub, type="A") existing_ip = rrset.records[0].value if rrset.records else None if current_ip != existing_ip: client.zones.set_rrset_records(rrset, records=[ZoneRecord(value=current_ip)]) logging.info(f"UPDATE: {sub}.{DOMAIN} von {existing_ip} auf {current_ip} geändert.") else: # Optional: Loggen, dass alles okay ist (kann man auch weglassen, um Log klein zu halten) logging.info(f"OK: {sub}.{DOMAIN} ist aktuell ({current_ip}).") except Exception as e: logging.error(f"Fehler bei Subdomain {sub}: {e}") except Exception as e: logging.critical(f"Kritischer Fehler im Hauptprozess: {e}") if __name__ == "__main__": update_ddns()