Files
inwx-scripts/inwx_add_record.py
T
2026-04-28 21:55:16 +02:00

105 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
import sys
import os
import subprocess
# Import-Fix für die Library-Struktur
try:
from INWX.Domrobot import ApiClient
except ImportError:
from inwx.Domrobot import ApiClient
# --- KONFIGURATION ---
API_URL = 'https://api.domrobot.com'
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
KP_DB_PATH = os.path.join(BASE_DIR, "hosting.kdbx")
KP_ENTRY_NAME = "inwx"
# Daten für den neuen Record
DOMAIN = "ma151.de"
NEW_NAME = "srv1" # Subdomain
NEW_TYPE = "A" # Record-Typ
NEW_VALUE = "167.86.90.246"
NEW_TTL = 3600 # Time to Live in Sekunden
def get_creds():
"""Liest User/Passwort sicher aus KeePassXC."""
try:
cmd = ["keepassxc-cli", "show", "-s", KP_DB_PATH, KP_ENTRY_NAME]
output = subprocess.check_output(cmd, text=True)
creds = {}
for line in output.splitlines():
if ":" in line:
k, v = line.split(":", 1)
creds[k.strip().lower()] = v.strip()
return creds.get("username") or creds.get("benutzername"), creds.get("password") or creds.get("passwort")
except Exception as e:
print(f"❌ KeePass-Fehler: {e}")
return None, None
def main():
user, password = get_creds()
if not user or not password:
sys.exit(1)
api = ApiClient(api_url=API_URL)
# 1. Login
login_res = api.login(user, password)
if login_res['code'] != 1000:
print(f"❌ Login fehlgeschlagen: {login_res['msg']}")
sys.exit(1)
print(f"✅ Login erfolgreich. Prüfe Record: {NEW_NAME}.{DOMAIN} ({NEW_TYPE} -> {NEW_VALUE})")
# 2. Prüfen, ob der Record bereits existiert
# Wir rufen alle Records der Domain ab
info_res = api.call_api('nameserver.info', {'domain': DOMAIN})
if info_res['code'] != 1000:
print(f"❌ Fehler beim Abrufen der Domain-Info: {info_res['msg']}")
api.logout()
sys.exit(1)
records = info_res['resData'].get('record', [])
# Den FQDN (Full Qualified Domain Name) zusammenbauen, wie INWX ihn speichert
full_name = f"{NEW_NAME}.{DOMAIN}"
# Suche nach Dubletten
already_exists = False
for rec in records:
# INWX gibt Namen oft mit oder ohne Punkt am Ende zurück, wir prüfen beides
rec_name = rec['name'].rstrip('.')
if (rec_name == full_name and
rec['type'] == NEW_TYPE and
rec['content'] == NEW_VALUE):
already_exists = True
break
if already_exists:
print(f"️ Record existiert bereits exakt so. Keine Änderung nötig.")
else:
print(f"🚀 Record nicht gefunden. Erstelle {full_name}...")
# 3. Record erstellen
create_params = {
'domain': DOMAIN,
'name': NEW_NAME,
'type': NEW_TYPE,
'content': NEW_VALUE,
'ttl': NEW_TTL
}
create_res = api.call_api('nameserver.createRecord', create_params)
if create_res['code'] == 1000:
print(f"✅ Erfolg! Record wurde mit ID {create_res['resData']['id']} angelegt.")
else:
print(f"❌ Fehler beim Erstellen: {create_res['msg']} (Code: {create_res['code']})")
api.logout()
if __name__ == "__main__":
main()