cloudflare-api-v4-ddns/cf-v4-ddns.sh

138 lines
4.6 KiB
Bash
Raw Permalink Normal View History

2018-06-30 21:31:20 +08:00
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
# Automatically update your CloudFlare DNS record to the IP, Dynamic DNS
# Can retrieve cloudflare Domain id and list zone's, because, lazy
# Place at:
2022-09-28 09:17:06 +08:00
# curl https://raw.githubusercontent.com/aipeach/cloudflare-api-v4-ddns/dev/cf-v4-ddns.sh > /usr/local/bin/cf-ddns.sh && chmod +x /usr/local/bin/cf-ddns.sh
2018-06-30 21:31:20 +08:00
# run `crontab -e` and add next line:
2018-07-01 01:50:43 +08:00
# */1 * * * * /usr/local/bin/cf-ddns.sh >/dev/null 2>&1
2018-06-30 21:31:20 +08:00
# 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 \
# -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
2020-04-10 10:12:32 +08:00
# -t A|AAAA # specify ipv4/ipv6, default: ipv4
2018-06-30 21:31:20 +08:00
# Optional flags:
# -f false|true \ # force dns update, disregard local stored ip
# default config
2021-10-21 18:35:04 +08:00
# API key, see https://dash.cloudflare.com/profile/api-tokens,
2018-06-30 21:31:20 +08:00
# incorrect api-key results in E_UNAUTH error
CFKEY=
# Zone name, eg: example.com
2018-06-30 21:31:20 +08:00
CFZONE_NAME=
# Hostname to update, eg: homeserver.example.com
CFRECORD_NAME=
2020-04-10 10:12:32 +08:00
# Record type, A(IPv4)|AAAA(IPv6), default IPv4
CFRECORD_TYPE=A
2018-06-30 21:31:20 +08:00
# Cloudflare TTL for record, between 120 and 86400 seconds
2021-10-21 18:35:04 +08:00
CFTTL=60
2018-06-30 21:31:20 +08:00
# Ignore local file, update ip anyway
FORCE=false
2020-04-10 10:12:32 +08:00
WANIPSITE="http://ipv4.icanhazip.com"
2018-06-30 21:31:20 +08:00
# Site to retrieve WAN ip, other examples are: bot.whatismyipaddress.com, https://api.ipify.org/ ...
2020-04-10 10:12:32 +08:00
if [ "$CFRECORD_TYPE" = "A" ]; then
:
elif [ "$CFRECORD_TYPE" = "AAAA" ]; then
WANIPSITE="http://ipv6.icanhazip.com"
else
echo "$CFRECORD_TYPE specified is invalid, CFRECORD_TYPE can only be A(for IPv4)|AAAA(for IPv6)"
exit 2
fi
2018-06-30 21:31:20 +08:00
# get parameter
2021-10-21 18:35:04 +08:00
while getopts k:h:z:t:f: opts; do
2018-06-30 21:31:20 +08:00
case ${opts} in
k) CFKEY=${OPTARG} ;;
h) CFRECORD_NAME=${OPTARG} ;;
z) CFZONE_NAME=${OPTARG} ;;
2020-04-10 10:12:32 +08:00
t) CFRECORD_TYPE=${OPTARG} ;;
2018-06-30 21:31:20 +08:00
f) FORCE=${OPTARG} ;;
esac
done
# 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 [ "$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`
2018-06-30 21:31:20 +08:00
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
2018-06-30 21:31:20 +08:00
# Get zone_identifier & record_identifier
ID_FILE=$HOME/.cf-id_$CFRECORD_NAME.txt
if [ -f $ID_FILE ] && [ $(wc -l $ID_FILE | cut -d " " -f 1) == 4 ] \
&& [ "$(sed -n '3,1p' "$ID_FILE")" == "$CFZONE_NAME" ] \
&& [ "$(sed -n '4,1p' "$ID_FILE")" == "$CFRECORD_NAME" ]; then
2018-07-01 19:23:38 +08:00
CFZONE_ID=$(sed -n '1,1p' "$ID_FILE")
CFRECORD_ID=$(sed -n '2,1p' "$ID_FILE")
2018-06-30 21:31:20 +08:00
else
echo "Updating zone_identifier & record_identifier"
2023-10-24 14:04:35 +08:00
CFZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$CFZONE_NAME" -H "Authorization: Bearer $CFKEY" -H "Content-Type: application/json" | grep -Eo '"id":"[^"]*'|sed 's/"id":"//' | head -1 )
CFRECORD_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records?name=$CFRECORD_NAME" -H "Authorization: Bearer $CFKEY" -H "Content-Type: application/json" | grep -Eo '"id":"[^"]*'|sed 's/"id":"//' | head -1 )
2018-06-30 21:31:20 +08:00
echo "$CFZONE_ID" > $ID_FILE
echo "$CFRECORD_ID" >> $ID_FILE
echo "$CFZONE_NAME" >> $ID_FILE
echo "$CFRECORD_NAME" >> $ID_FILE
2018-06-30 21:31:20 +08:00
fi
# If WAN is changed, update cloudflare
echo "Updating DNS to $WAN_IP"
RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFRECORD_ID" \
2021-10-21 18:35:04 +08:00
-H "Authorization: Bearer $CFKEY" \
2018-06-30 21:31:20 +08:00
-H "Content-Type: application/json" \
2020-04-10 10:12:32 +08:00
--data "{\"id\":\"$CFZONE_ID\",\"type\":\"$CFRECORD_TYPE\",\"name\":\"$CFRECORD_NAME\",\"content\":\"$WAN_IP\", \"ttl\":$CFTTL}")
2018-06-30 21:31:20 +08:00
2020-05-25 04:09:15 +08:00
if [ "$RESPONSE" != "${RESPONSE%success*}" ] && [ "$(echo $RESPONSE | grep "\"success\":true")" != "" ]; then
2018-06-30 21:31:20 +08:00
echo "Updated succesfuly!"
echo $WAN_IP > $WAN_IP_FILE
2018-06-30 21:31:20 +08:00
exit
else
echo 'Something went wrong :('
echo "Response: $RESPONSE"
exit 1
2018-10-10 13:15:48 +08:00
fi