#!/bin/bash # version #VERSION# set -e stty &> /dev/null || \ sleep $[$RANDOM%60] if ! ipCmd="$(which ip 2> /dev/null)" then for p1 in / /usr/ do for p2 in bin/ sbin/ do ipCmd="${p1}${p2}ip" [ -x "${ipCmd}" ] && break done [ -x "${ipCmd}" ] && break done fi i=0 while read -r ipVer updateUrl dnsName do if [[ "${ipVer}" = '#'* ]] || [ -z "${ipVer}" ] then continue fi if [[ "${ipVer}" == *"?" ]] then ipVer="${ipVer%?}" [ -z "$(${ipCmd} -o -${ipVer} addr show scope global)" ] && continue fi if [ "${ipVer}" == '4' ] then dnsVers[${i}]='A' inetVers[${i}]='inet' ipCmdVers[${i}]='-4' elif [ "${ipVer}" == '6' ] then dnsVers[${i}]='AAAA' inetVers[${i}]='inet6' ipCmdVers[${i}]='-6' elif [ "${ipVer}" == '46' ] || [ "${ipVer}" == '64' ] then dnsVers[${i}]='A' inetVers[${i}]='inet' ipCmdVers[${i}]='-4' updateUrls[${i}]="${updateUrl}" dnsNamen[${i}]="${dnsName}" i=$[${i}+1] dnsVers[${i}]='AAAA' inetVers[${i}]='inet6' ipCmdVers[${i}]='-6' else >&2 echo 'Error: First column in "#ETCDIR#/update-ddns.conf" needs to be "4", "6", "46" or "64"!' exit 1 fi updateUrls[${i}]="${updateUrl}" dnsNamen[${i}]="${dnsName}" i=$[${i}+1] done < #ETCDIR#/update-ddns.conf for dnsName in $( \ echo "${dnsNamen[@]}" | \ tr ' ' '\n' | \ sort -u ) do unset updateIps unset updateUrl unset curlForceIpVer needsUpdate=false for ((j=0; j<${i}; j++)) do [ "${dnsName}" != "${dnsNamen[${j}]}" ] && continue updateUrl="${updateUrls[${j}]}" if [[ "${dnsName}" == *'/'* ]] then dnsIps="$( curl ${ipCmdVers[${j}]} "${dnsName}" 2> /dev/null )" ips='1' updateIps="${updateIps}"',auto' if [ -n "${curlForceIpVer}" ] && [ "${curlForceIpVer}" != "${ipCmdVers[${j}]}" ] then >&2 echo 'Error: I cannot determine two different ip versions automatically (because Erich didn'"'"'t need it).' exit 1 fi curlForceIpVer="${ipCmdVers[${j}]}" else dnsIps="$( dig $1 "${dnsName}" ${dnsVers[${j}]} | \ sed '/^;; AUTHORITY SECTION:$/Q' | \ grep '^[^;]' | \ grep "\s${dnsVers[${j}]}\s" | \ awk '{print $5}' | \ sort -u )" ips="$( "${ipCmd}" -o ${ipCmdVers[${j}]} addr show scope global | \ awk '{print $4}' | \ cut -d/ -f1 | \ sort -u )" if [ $(echo "${ips}" | grep -c '.') -eq 0 ] then updateIps="${updateIps}"',auto' if [ -n "${curlForceIpVer}" ] && [ "${curlForceIpVer}" != "${ipCmdVers[${j}]}" ] then >&2 echo 'Error: I cannot determine two different ip versions automatically (because Erich didn'"'"'t need it).' exit 1 fi curlForceIpVer="${ipCmdVers[${j}]}" else updateIps="${updateIps},$(echo -n "${ips}" | tr '\n' ',')" fi fi if [ "${dnsIps}" != "${ips}" ] then needsUpdate=true fi done if ${needsUpdate} then if [[ "${updateUrl}" = *'?'* ]] then updateIps='&address='"${updateIps#,}" else updateIps='?address='"${updateIps#,}" fi curl ${curlForceIpVer} -sS "${updateUrl}${updateIps}" 2> /dev/null fi done