#!/bin/bash
#HOSTNAME="$1.v6.rocks"
HOSTNAME="meinvserver.v6.rocks"
TOKEN="xxxgeheimxxx"
# 1. Abfrage DNS
# DNS leer → öffentliche IP abfragen → Eintrag erneuern
# DNS nicht leer → öffentliche IP abfragen und vergleichen
# =: nix
# !=: Eintrag erneuern
# Eintrag erneuert: Check, ob es geklappt hat.
is_valid_ipv4() {
[[ $1 =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] || return 1
local IFS=. n; read -ra n <<< "$1"
for i in "${n[@]}"; { (( i >= 0 && i <= 255 )) || return 1; }
}
is_valid_ipv6() {
local ipv6="$1"
# Leere Eingabe prüfen
if [[ -z "$ipv6" ]]; then
return 1
fi
# IPv6-Adressen können in verschiedenen Formaten vorliegen:
# 1. Vollständige Form: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
# 2. Komprimierte Form: 2001:db8:85a3::8a2e:370:7334
# 3. Mit IPv4-Embedded: ::ffff:192.168.0.1
# Prüfung mit regex für IPv6-Adressen
# Erklärung des regulären Ausdrucks:
# ^(?!.*::.*::) - Verhindert mehr als eine :: Komprimierung
# ([0-9a-fA-F]{1,4}:){1,7} - 1-7 Gruppen von 1-4 Hex-Zeichen mit Doppelpunkt
# | - oder
# :: mit verschiedenen Kombinationen
# (\.((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){4}) - Für IPv4-embedded Notation
local ipv6_regex='^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$'
if [[ "$ipv6" =~ $ipv6_regex ]]; then
return 0 # Gültige IPv6-Adresse
else
return 1 # Ungültige IPv6-Adresse
fi
}
update_dyndns() {
echo "Setze DynV6 auf 0.0.0.0 und ::"
/usr/bin/curl -s "https://dynv6.com/api/update?hostname=$HOSTNAME&token=$TOKEN&ipv4=0.0.0.0&ipv6=::"
echo
sleep 2
echo "Setze DynV6 auf $public_ip4 und $public_ip6"
/usr/bin/curl -s "https://dynv6.com/api/update?hostname=$HOSTNAME&token=$TOKEN&ipv4=$1&ipv6=$2"
echo
}
public_ip4="$(curl -4 https://ifconfig.co)"
public_ip6="$(curl -6 https://ifconfig.co)"
if ! is_valid_ipv4 "$public_ip4" ; then
echo "Fehler: Konnte öffentliche IPv4 nicht ermitteln. Abbruch!"
exit 1
fi
if ! is_valid_ipv6 "$public_ip6" ; then
echo "Fehler: Konnte öffentliche IPv6 nicht ermitteln. Abbruch!"
exit 1
fi
# IP vom DNS
ip_dns4="$(dig -t A $HOSTNAME +short @1.1.1.1 2>/dev/null)"
ip_dns6="$(dig -t AAAA $HOSTNAME +short @1.1.1.1 2>/dev/null)"
if [[ -z "$ip_dns4" || "$ip_dns4" != "$public_ip4" || -z "$ip_dns6" || "$ip_dns6" != "$public_ip6" ]]; then
echo "Update dynv6: $public_ip4 + $public_ip6"
update_dyndns "$public_ip4" "$public_ip6"
else
echo "Nichts zu tun. Public-IP: $ip_dns4 + $ip_dns6"
fi