diff --git a/selfhost-update.sh b/selfhost-update.sh new file mode 100644 index 0000000..6a255d2 --- /dev/null +++ b/selfhost-update.sh @@ -0,0 +1,54 @@ +#!/bin/bash +# ============================================================================= +# Selfhost.de DynDNS-Update-Skript für IPv4 + IPv6 +# Speichere es z. B. als /usr/local/bin/selfhost-update.sh +# chmod +x /usr/local/bin/selfhost-update.sh +# ============================================================================= + +# ────── Deine Daten hier eintragen ─────────────────────────────────────────── +USERNAME="356205" # Selfhost.de Login +PASSWORD="yaytVupov9" # Passwort oder langer Token +HOSTNAME="cloud.schoedl.bayern" # z. B. home.selfhost.de +# ───────────────────────────────────────────────────────────────────────────── +# cron job +# */10 * * * * /usr/local/bin/selfhost-update.sh >> /var/log/selfhost-update.log 2>&1 + +UPDATE_URL="https://update.selfhost.de/nic/update" + +# Cache-Dateien (werden automatisch angelegt) +CACHE_IPV4="/var/tmp/last_selfhost_ipv4" +CACHE_IPV6="/var/tmp/last_selfhost_ipv6" + +# Aktuelle öffentliche Adressen ermitteln (sehr zuverlässige Services) +CURRENT_IPV4=$(curl -s --max-time 10 https://ipv4.icanhazip.com | tr -d '\n') +CURRENT_IPV6=$(curl -s --max-time 10 https://ipv6.icanhazip.com | tr -d '\n') + +# Falls IPv6 nicht vorhanden ist → leer lassen (nicht "curl fehlgeschlagen" senden) +[ "$CURRENT_IPV6" = "no IPv6" ] && CURRENT_IPV6="" +[ "$CURRENT_IPV6" = "curl: (7)" ] && CURRENT_IPV6="" # kein IPv6 am Standort + +# Nur senden, wenn sich mindestens eine der IPs geändert hat +SEND=0 +[ ! -f "$CACHE_IPV4" ] || [ "$CURRENT_IPV4" != "$(cat "$CACHE_IPV4")" ] && SEND=1 +[ -n "$CURRENT_IPV6" ] && { [ ! -f "$CACHE_IPV6" ] || [ "$CURRENT_IPV6" != "$(cat "$CACHE_IPV6")" ]; } && SEND=1 + +if [ "$SEND" -eq 1 ]; then + # Parameter zusammensetzen + PARAMS="hostname=$HOSTNAME" + [ -n "$CURRENT_IPV4" ] && PARAMS="$PARAMS&myip=$CURRENT_IPV4" + [ -n "$CURRENT_IPV6" ] && PARAMS="$PARAMS&myipv6=$CURRENT_IPV6" + + RESPONSE=$(curl -s -u "$USERNAME:$PASSWORD" --max-time 20 \ + "$UPDATE_URL?$PARAMS") + + # Cache aktualisieren + echo "$CURRENT_IPV4" > "$CACHE_IPV4" + [ -n "$CURRENT_IPV6" ] && echo "$CURRENT_IPV6" > "$CACHE_IPV6" || rm -f "$CACHE_IPV6" + + echo "$(date '+%Y-%m-%d %H:%M:%S') | Update gesendet | IPv4: $CURRENT_IPV4 | IPv6: $CURRENT_IPV6 | Antwort: $RESPONSE" +else + echo "$(date '+%Y-%m-%d %H:%M:%S') | Keine Änderung → nichts gesendet (IPv4: $CURRENT_IPV4 | IPv6: $CURRENT_IPV6)" +fi + +# (optional) komplettes Log in Datei schreiben +# echo "$(date '+%Y-%m-%d %H:%M:%S') ..." >> /var/log/selfhost-update.log \ No newline at end of file