Rolle DNS Registrierung hinzugefügt

This commit is contained in:
Stefan Mewes
2026-03-20 18:16:35 +01:00
parent f8aeb9c1e6
commit 63a6aec81d
5 changed files with 248 additions and 0 deletions

View File

@@ -0,0 +1,96 @@
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
DNS_SERVER="{{ dns_server }}"
DNS_ZONE="{{ dns_zone }}"
API_TOKEN="{{ api_token }}"
HOSTNAME=$(hostname)
IP_ADDRESS=$(hostname -I | awk '{print $1}')
DOMAIN="$HOSTNAME.$DNS_ZONE"
LOGFILE="/var/log/update-dns.log"
DEBUG=true # bei Bedarf auf false setzen
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') | $1" | tee -a "$LOGFILE"
}
debug() {
if [ "$DEBUG" = true ]; then
log "DEBUG: $1"
fi
}
log "🔄 Starte DNS-Update für $DOMAIN ($IP_ADDRESS)"
# --- CURL REQUEST -------------------------------------------------------------
API_URL="http://$DNS_SERVER:5380/api/zones/records/add"
QUERY="domain=$DOMAIN&zone=$DNS_ZONE&type=A&overwrite=true&IPAddress=$IP_ADDRESS&token=$API_TOKEN"
debug "API-URL: $API_URL?$QUERY"
response=$(curl \
--silent \
--show-error \
--write-out "%{http_code}" \
--output /tmp/dns_api_response.txt \
--max-time 10 \
"$API_URL?$QUERY" 2>/tmp/dns_api_error.txt || true)
curl_exit=$?
debug "Curl Exit-Code: $curl_exit"
debug "HTTP-Code: $response"
debug "Curl stderr: $(cat /tmp/dns_api_error.txt || true)"
if [ "$curl_exit" -ne 0 ]; then
log "❌ Curl-Fehler beim DNS-Update (Exit-Code $curl_exit)"
log " → $(cat /tmp/dns_api_error.txt || echo 'kein Fehlertext')"
exit 10
fi
if [ "$response" -ne 200 ]; then
log "❌ DNS-Server antwortete mit HTTP $response"
log " API Response: $(cat /tmp/dns_api_response.txt || echo 'keine Antwort')"
exit 11
fi
log "✅ DNS-Eintrag erfolgreich gesetzt."
# --- DNS CHECK ---------------------------------------------------------------
log "🔍 Prüfe DNS-Eintrag mit dig..."
max_retries=5
retry_delay=2
success=false
for i in $(seq 1 "$max_retries"); do
dns_check=$(dig +time=2 +tries=1 +short "$DOMAIN" 2>/tmp/dns_dig_error.txt || true)
if [ -s /tmp/dns_dig_error.txt ]; then
debug "dig error: $(cat /tmp/dns_dig_error.txt)"
fi
if [ -n "$dns_check" ]; then
log "🟢 DNS-Eintrag gefunden: $dns_check"
success=true
break
else
log "🔁 Versuch $i/$max_retries: DNS-Eintrag noch nicht sichtbar"
sleep "$retry_delay"
fi
done
if [ "$success" = false ]; then
log "🔴 Kein DNS-Eintrag gefunden nach $max_retries Versuchen."
exit 12
fi
log "🎉 DNS-Update abgeschlossen."
exit 0