| Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung |
| home-server [2021-11-11 15:02:27] – [Cloudflare] david | home-server [2023-04-22 15:12:23] (aktuell) – [Cloudflare] manfred |
|---|
| |
| neuste Version auf GitHub: | neuste Version auf GitHub: |
| [[https://github.com/Masterflitzer/cloudflare-dyndns.git]] | [[https://github.com/masterflitzer/cloudflare-ddns.git]] |
| |
| Abhängigkeiten: ''links'' und ''jq'' | Abhängigkeiten: ''links'' und ''jq'' |
| |
| Cron: | Cron: |
| @reboot root cloudflare-dyndns.sh > /var/log/cloudflare-dyndns.log 2>&1 | @reboot root cloudflare-ddns.sh > /var/log/cloudflare-ddns.log 2>&1 |
| @hourly root cloudflare-dyndns.sh > /var/log/cloudflare-dyndns.log 2>&1 | @hourly root cloudflare-ddns.sh > /var/log/cloudflare-ddns.log 2>&1 |
| |
| |
| <code c cloudflare-dyndns.sh> | === Skript === |
| #!/bin/sh | |
| |
| ### Variables | > apt install links curl jq |
| |
| | <code c cloudflare-ddns.ini> |
| | API_TOKEN=1234567893feefc5f0q5000bfo0c38d90bbeb |
| | ZONE_NAME=example.com |
| | </code> |
| | |
| | <code c cloudflare-ddns.sh> |
| | #!/bin/bash |
| | |
| | ##### Variables |
| | |
| | CONFIG_FILE="$(dirname "$0")/$(basename -- "$0" .sh).ini" |
| INTERVAL="5" | INTERVAL="5" |
| COUNTER="10" | COUNTER="10" |
| |
| # credentials: email + password/api-key | # boolean |
| # when using api-key: the key needs the following permissions: com.cloudflare.api.user.read, #dns_records:edit | # if "true" the dns record for the root domain will be updated |
| EMAIL="" | UPDATE_ROOT_DOMAIN="true" |
| API_KEY="" | # multiple record names (subdomains) to be updated |
| # when you want to update "www.example.com", "www" is the RECORD_NAME and "example.com" is the ZONE_NAME | # separated by space, e.g. "www mail smtp" |
| ZONE_NAME="" | |
| # multiple record names separated by space, e.g. "www mail smtp" | |
| RECORD_NAME_V4="" | RECORD_NAME_V4="" |
| RECORD_NAME_V6="" | RECORD_NAME_V6="" |
| PROXIED="false" | PROXIED="false" |
| |
| #------------------------------------------------------------------------------# | # stable base URL for all Version 4 HTTPS endpoints |
| ### Functions | API_ENDPOINT="https://api.cloudflare.com/client/v4" |
| |
| links_IPv4() | # custom API-Token (not global API-Key) |
| { | # permissions needed: #dns_records:edit |
| IPv4="$(links -dump http://checkip.dyndns.org/ | tr -s '[ :]' '\n' | egrep '[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+')" | API_TOKEN="$(cat "$CONFIG_FILE" | grep -E "^API_TOKEN=" | head -1 | cut -d "=" -f2)" |
| #IPv4="1.1.1.1" | |
| | # when you want to update "www.example.com", "www" is the RECORD_NAME and "example.com" is the ZONE_NAME |
| | ZONE_NAME="$(cat "$CONFIG_FILE" | grep -E "^ZONE_NAME=" | head -1 | cut -d "=" -f2)" |
| | |
| | COUNTER_V4="$COUNTER" |
| | COUNTER_V6="$COUNTER" |
| | |
| | |
| | ##### Functions |
| | |
| | links_IPv4() { |
| | IP_V4="$(links -dump http://checkip.dyndns.org/ | tr -s '[ :]' '\n' | egrep '[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+')" |
| | #IPv4="1.1.1.1" |
| } | } |
| |
| links_IPv6() | links_IPv6() { |
| { | IP_V6="$(links -dump http://checkipv6.dyndns.org/ | tr -s ' ' '\n' | egrep '[0-9a-f]+[:]+[0-9a-f]+[:]*')" |
| IPv6="$(links -dump http://checkipv6.dyndns.org/ | tr -s ' ' '\n' | egrep '[0-9a-f]+[:]+[0-9a-f]+[:]*')" | #IPv6="2606:4700:4700::1111" |
| #IPv6="2606:4700:4700::1111" | |
| } | } |
| |
| #------------------------------------------------------------------------------# | |
| ### Get IPs | |
| |
| COUNTER_V4="${COUNTER}" | ##### Script |
| COUNTER_V6="${COUNTER}" | |
| | ### Get IPs |
| | printf "\n#==============================================================================#\n\n" |
| |
| | echo -n "Determining IPv4 address" |
| links_IPv4 | links_IPv4 |
| while [ "x${IPv4}" = "x" -a "${COUNTER_V4}" -gt "0" ] | while test -z "$IP_V4" -a "$COUNTER_V4" -gt "0" |
| do | do |
| echo -n '.' | echo -n '.' |
| COUNTER_V4="$(echo "${COUNTER_V4}"|awk '{print $1-1}')" | COUNTER_V4="$(echo "$COUNTER_V4"|awk '{print $1-1}')" |
| sleep ${INTERVAL} | sleep $INTERVAL |
| links_IPv4 | links_IPv4 |
| done | done |
| | echo |
| |
| | echo -n "Determining IPv6 address" |
| links_IPv6 | links_IPv6 |
| while [ "x${IPv6}" = "x" -a "${COUNTER_V6}" -gt "0" ] | while test -z "$IP_V6" -a "$COUNTER_V6" -gt "0" |
| do | do |
| echo -n '.' | echo -n '.' |
| COUNTER_V6="$(echo "${COUNTER_V6}"|awk '{print $1-1}')" | COUNTER_V6="$(echo "$COUNTER_V6"|awk '{print $1-1}')" |
| sleep ${INTERVAL} | sleep $INTERVAL |
| links_IPv6 | links_IPv6 |
| done | done |
| | echo |
| |
| #==============================================================================# | echo "IPv4: $IP_V4" |
| ### Get ZONE_ID | echo "IPv6: $IP_V6" |
| |
| echo "#==============================================================================#" | printf "\n#==============================================================================#\n\n" |
| |
| ZONE_ID="$(curl -X GET "https://api.cloudflare.com/client/v4/zones" -H "X-Auth-Email: ${EMAIL}" -H "X-Auth-Key: ${API_KEY}" -H "Content-Type: application/json" 2>/dev/null | jq -r '.result[] | .id')" | ### Get Zone ID |
| echo | ZONE_ID="$(curl -s -X GET "${API_ENDPOINT}/zones" -H "Authorization: Bearer ${API_TOKEN}" -H "Content-Type: application/json" 2>/dev/null | jq -r ".result[] | select(.name == \"${ZONE_NAME}\") | .id")" |
| echo "1. ZONE_NAME='${ZONE_NAME}'" | |
| echo "2. ZONE_ID='${ZONE_ID}'" | |
| |
| #------------------------------------------------------------------------------# | ### Get Record ID (IPv4) |
| ### IPv4 | RECORD_ID_V4="$(curl -s -X GET "${API_ENDPOINT}/zones/${ZONE_ID}/dns_records" -H "Authorization: Bearer ${API_TOKEN}" -H "Content-Type: application/json" 2>/dev/null | jq -r ".result[] | select((.name == \"${ZONE_NAME}\") and (.type == \"A\")) | .id")" |
| |
| ### Get RECORD_ID for ZONE_NAME | ### Get Record ID (IPv6) |
| RECORD_ID="$(curl -X GET "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records" -H "X-Auth-Email: ${EMAIL}" -H "X-Auth-Key: ${API_KEY}" -H "Content-Type: application/json" 2>/dev/null | jq -r ".result[] | select((.name == \"${ZONE_NAME}\") and (.type == \"A\")) | .id")" | RECORD_ID_V6="$(curl -s -X GET "${API_ENDPOINT}/zones/${ZONE_ID}/dns_records" -H "Authorization: Bearer ${API_TOKEN}" -H "Content-Type: application/json" 2>/dev/null | jq -r ".result[] | select((.name == \"${ZONE_NAME}\") and (.type == \"AAAA\")) | .id")" |
| echo | |
| echo "3. RECORD_ID='${RECORD_ID}'" | |
| |
| ### Set IPv4 | echo "Record Name = $ZONE_NAME" |
| curl -X PUT "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${RECORD_ID}" -H "X-Auth-Email: ${EMAIL}" -H "X-Auth-Key: ${API_KEY}" -H "Content-Type: application/json" --data "{\"type\":\"A\",\"name\":\"${ZONE_NAME}\",\"content\":\"${IPv4}\",\"ttl\":${TTL},\"proxied\":${PROXIED}}" | echo "ZONE_ID = $ZONE_ID" |
| echo | echo "Record ID (IPv4) = $RECORD_ID_V4" |
| | echo "Record ID (IPv6) = $RECORD_ID_V6" |
| |
| #------------------------------------------------------------------------------# | |
| ### IPv4 - RECORD_NAME | |
| |
| for RECORD_NAME in ${RECORD_NAME_V4} | ### Set IP |
| { | printf "\n#==============================================================================#\n\n" |
| ### Get RECORD_ID for ZONE_NAME | |
| RECORD_ID="$(curl -X GET "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records" -H "X-Auth-Email: ${EMAIL}" -H "X-Auth-Key: ${API_KEY}" -H "Content-Type: application/json" 2>/dev/null | jq -r ".result[] | select((.name == \"${RECORD_NAME}.${ZONE_NAME}\") and (.type == \"A\")) | .id")" | |
| echo | |
| echo "4. RECORD_NAME.ZONE_NAME='${RECORD_NAME}.${ZONE_NAME}'" | |
| echo "5. RECORD_ID='${RECORD_ID}'" | |
| |
| ### Set IPv4 | if test $UPDATE_ROOT_DOMAIN == "true" |
| curl -X PUT "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${RECORD_ID}" -H "X-Auth-Email: ${EMAIL}" -H "X-Auth-Key: ${API_KEY}" -H "Content-Type: application/json" --data "{\"type\":\"A\",\"name\":\"${RECORD_NAME}.${ZONE_NAME}\",\"content\":\"${IPv4}\",\"ttl\":${TTL},\"proxied\":${PROXIED}}" | then |
| echo | echo "IPv4: Updating DNS Record to '$IP_V4'" |
| } | curl -s -X PUT "${API_ENDPOINT}/zones/${ZONE_ID}/dns_records/${RECORD_ID_V4}" -H "Authorization: Bearer ${API_TOKEN}" -H "Content-Type: application/json" --data "{\"type\":\"A\",\"name\":\"${ZONE_NAME}\",\"content\":\"${IP_V4}\",\"ttl\":${TTL},\"proxied\":${PROXIED}}"; echo |
| |
| #------------------------------------------------------------------------------# | printf "\n#------------------------------------------------------------------------------#\n\n" |
| ### IPv6 | |
| |
| ### Get RECORD_ID for ZONE_NAME | echo "IPv6: Updating DNS Record to '$IP_V6'" |
| RECORD_ID="$(curl -X GET "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records" -H "X-Auth-Email: ${EMAIL}" -H "X-Auth-Key: ${API_KEY}" -H "Content-Type: application/json" 2>/dev/null | jq -r ".result[] | select((.name == \"${ZONE_NAME}\") and (.type == \"AAAA\")) | .id")" | curl -s -X PUT "${API_ENDPOINT}/zones/${ZONE_ID}/dns_records/${RECORD_ID_V6}" -H "Authorization: Bearer ${API_TOKEN}" -H "Content-Type: application/json" --data "{\"type\":\"AAAA\",\"name\":\"${ZONE_NAME}\",\"content\":\"${IP_V6}\",\"ttl\":${TTL},\"proxied\":${PROXIED}}"; echo |
| echo | fi |
| echo "6. RECORD_ID='${RECORD_ID}'" | |
| |
| ### Set IPv6 | ### Set IP for Record Names |
| curl -X PUT "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${RECORD_ID}" -H "X-Auth-Email: ${EMAIL}" -H "X-Auth-Key: ${API_KEY}" -H "Content-Type: application/json" --data "{\"type\":\"AAAA\",\"name\":\"${ZONE_NAME}\",\"content\":\"${IPv6}\",\"ttl\":${TTL},\"proxied\":${PROXIED}}" | printf "\n#==============================================================================#\n\n" |
| echo | |
| |
| #------------------------------------------------------------------------------# | if test ! -z "${RECORD_NAME_V4// }"; then printf "IPv4: Subdomains\n\n"; fi |
| ### IPv6 - RECORD_NAME | |
| |
| for RECORD_NAME in ${RECORD_NAME_V6} | COUNTER="0" |
| { | for RECORD_NAME in $RECORD_NAME_V4 |
| ### Get RECORD_ID for ZONE_NAME | do |
| RECORD_ID="$(curl -X GET "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records" -H "X-Auth-Email: ${EMAIL}" -H "X-Auth-Key: ${API_KEY}" -H "Content-Type: application/json" 2>/dev/null | jq -r ".result[] | select((.name == \"${RECORD_NAME}.${ZONE_NAME}\") and (.type == \"AAAA\")) | .id")" | if test $COUNTER -gt 0; then printf "\n#------------------------------------------------------------------------------#\n\n"; fi |
| echo | |
| echo "7. RECORD_NAME.ZONE_NAME='${RECORD_NAME}.${ZONE_NAME}'" | |
| echo "8. RECORD_ID='${RECORD_ID}'" | |
| |
| ### Set IPv6 | RECORD_ID="$(curl -s -X GET "${API_ENDPOINT}/zones/${ZONE_ID}/dns_records" -H "Authorization: Bearer ${API_TOKEN}" -H "Content-Type: application/json" 2>/dev/null | jq -r ".result[] | select((.name == \"${RECORD_NAME}.${ZONE_NAME}\") and (.type == \"A\")) | .id")" |
| curl -X PUT "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${RECORD_ID}" -H "X-Auth-Email: ${EMAIL}" -H "X-Auth-Key: ${API_KEY}" -H "Content-Type: application/json" --data "{\"type\":\"AAAA\",\"name\":\"${RECORD_NAME}.${ZONE_NAME}\",\"content\":\"${IPv6}\",\"ttl\":${TTL},\"proxied\":${PROXIED}}" | |
| echo | |
| } | |
| |
| #==============================================================================# | echo "Record Name = $RECORD_NAME.$ZONE_NAME" |
| | echo "Record ID = $RECORD_ID" |
| | |
| | echo "Updating DNS Record to '$IP_V4'" |
| | curl -s -X PUT "${API_ENDPOINT}/zones/${ZONE_ID}/dns_records/${RECORD_ID}" -H "Authorization: Bearer ${API_TOKEN}" -H "Content-Type: application/json" --data "{\"type\":\"A\",\"name\":\"${RECORD_NAME}.${ZONE_NAME}\",\"content\":\"${IP_V4}\",\"ttl\":${TTL},\"proxied\":${PROXIED}}"; echo |
| | |
| | let "COUNTER += 1" |
| | done |
| | |
| | if test $COUNTER -gt 0; then |
| | printf "#==============================================================================#\n\n" |
| | fi |
| | |
| | if test ! -z "${RECORD_NAME_V6// }"; then printf "IPv6: Subdomains\n\n"; fi |
| | |
| | COUNTER="0" |
| | for RECORD_NAME in $RECORD_NAME_V6 |
| | do |
| | if test $COUNTER -gt 0; then printf "\n#------------------------------------------------------------------------------#\n\n"; fi |
| | |
| | RECORD_ID="$(curl -s -X GET "${API_ENDPOINT}/zones/${ZONE_ID}/dns_records" -H "Authorization: Bearer ${API_TOKEN}" -H "Content-Type: application/json" 2>/dev/null | jq -r ".result[] | select((.name == \"${RECORD_NAME}.${ZONE_NAME}\") and (.type == \"AAAA\")) | .id")" |
| | |
| | echo "Record Name = $RECORD_NAME.$ZONE_NAME" |
| | echo "Record ID = $RECORD_ID" |
| | |
| | echo "Updating DNS Record to '${IP_V6}'" |
| | curl -s -X PUT "${API_ENDPOINT}/zones/${ZONE_ID}/dns_records/${RECORD_ID}" -H "Authorization: Bearer ${API_TOKEN}" -H "Content-Type: application/json" --data "{\"type\":\"AAAA\",\"name\":\"${RECORD_NAME}.${ZONE_NAME}\",\"content\":\"${IP_V6}\",\"ttl\":${TTL},\"proxied\":${PROXIED}}"; echo |
| | |
| | let "COUNTER += 1" |
| | done |
| |
| | if test $COUNTER -gt 0; then |
| | printf "#==============================================================================#\n\n" |
| | fi |
| </code> | </code> |
| |