#/bin/bash set -o errexit set -o nounset set -o pipefail
date "+%n%F %T"
# Automatically update your CloudFlare DNS record to the IP, Dynamic DNS # Can retrieve cloudflare Domain id and list zone's, because, lazy
# Place at: # From https://github.com/yulewang/cloudflare-api-v4-ddns # curl https://raw.githubusercontent.com/yulewang/cloudflare-api-v4-ddns/master/cf-v4-ddns.sh > /usr/local/bin/cf-ddns.sh && chmod +x /usr/local/bin/cf-ddns.sh # run `crontab -e` and add next line: # */1 * * * * /usr/local/bin/cf-ddns.sh >/dev/null 2>&1 # or you need log: # */1 * * * * /usr/local/bin/cf-ddns.sh >> /var/log/cf-ddns.log 2>&1
# Usage: # cf-ddns.sh -k cloudflare-api-key \ # -u user@example.com \ # -h host.example.com \ # fqdn of the record you want to update # -z example.com \ # will show you all zones if forgot, but you need this # -t A|AAAA # specify ipv4/ipv6, default: ipv4
# Optional flags: # -f false|true \ # force dns update, disregard local stored ip
# default config
# API key, see https://www.cloudflare.com/a/account/my-account, # incorrect api-key results in E_UNAUTH error CFKEY=
# Username, eg: user@example.com CFUSER=
# Zone name, eg: example.com CFZONE_NAME=
# Hostname to update, eg: homeserver.example.com CFRECORD_NAME=next.litao.cloud
# Record type, A(IPv4)|AAAA(IPv6), default IPv4 CFRECORD_TYPE=AAAA
# Cloudflare TTL for record, between 120 and 86400 seconds CFTTL=120
# Ignore local file, update ip anyway FORCE=false
#WANIPSITE="http://ipv6.icanhazip.com" WANIPSITE=`ifconfig | grep global | awk {'print $2'}`
# Site to retrieve WAN ip, other examples are: bot.whatismyipaddress.com, https://api.ipify.org/ ... if [ "$CFRECORD_TYPE" = "AAAA" ]; then : elif [ "$CFRECORD_TYPE" = "A" ]; then WANIPSITE="http://ipv4.icanhazip.com" else echo"$CFRECORD_TYPE specified is invalid, CFRECORD_TYPE can only be A(for IPv4)|AAAA(for IPv6)" exit 2 fi
# If required settings are missing just exit if [ "$CFKEY" = "" ]; then echo"Missing api-key, get at: https://www.cloudflare.com/a/account/my-account" echo"and save in ${0} or using the -k flag" exit 2 fi if [ "$CFUSER" = "" ]; then echo"Missing username, probably your email-address" echo"and save in ${0} or using the -u flag" exit 2 fi if [ "$CFRECORD_NAME" = "" ]; then echo"Missing hostname, what host do you want to update?" echo"save in ${0} or using the -h flag" exit 2 fi
# If the hostname is not a FQDN if [ "$CFRECORD_NAME" != "$CFZONE_NAME" ] && ! [ -z "${CFRECORD_NAME##*$CFZONE_NAME}" ]; then CFRECORD_NAME="$CFRECORD_NAME.$CFZONE_NAME" echo" => Hostname is not a FQDN, assuming $CFRECORD_NAME" fi
# Get current and old WAN ip WAN_IP=`curl -s ${WANIPSITE}` WAN_IP_FILE=$HOME/.cf-wan_ip_$CFRECORD_NAME.txt if [ -f $WAN_IP_FILE ]; then OLD_WAN_IP=`cat $WAN_IP_FILE` else echo"No file, need IP" OLD_WAN_IP="" fi
# If WAN IP is unchanged an not -f flag, exit here if [ "$WAN_IP" = "$OLD_WAN_IP" ] && [ "$FORCE" = false ]; then echo"WAN IP Unchanged, to update anyway use flag -f true" exit 0 fi