更新为区域API

This commit is contained in:
cola 2021-10-21 18:35:04 +08:00 committed by GitHub
parent 1336b077a2
commit e14f6aab39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,7 +16,6 @@ set -o pipefail
# Usage: # Usage:
# cf-ddns.sh -k cloudflare-api-key \ # cf-ddns.sh -k cloudflare-api-key \
# -u user@example.com \
# -h host.example.com \ # fqdn of the record you want to update # -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 # -z example.com \ # will show you all zones if forgot, but you need this
# -t A|AAAA # specify ipv4/ipv6, default: ipv4 # -t A|AAAA # specify ipv4/ipv6, default: ipv4
@ -26,13 +25,10 @@ set -o pipefail
# default config # default config
# API key, see https://www.cloudflare.com/a/account/my-account, # API key, see https://dash.cloudflare.com/profile/api-tokens,
# incorrect api-key results in E_UNAUTH error # incorrect api-key results in E_UNAUTH error
CFKEY= CFKEY=
# Username, eg: user@example.com
CFUSER=
# Zone name, eg: example.com # Zone name, eg: example.com
CFZONE_NAME= CFZONE_NAME=
@ -43,7 +39,7 @@ CFRECORD_NAME=
CFRECORD_TYPE=A CFRECORD_TYPE=A
# Cloudflare TTL for record, between 120 and 86400 seconds # Cloudflare TTL for record, between 120 and 86400 seconds
CFTTL=120 CFTTL=60
# Ignore local file, update ip anyway # Ignore local file, update ip anyway
FORCE=false FORCE=false
@ -61,10 +57,9 @@ else
fi fi
# get parameter # get parameter
while getopts k:u:h:z:t:f: opts; do while getopts k:h:z:t:f: opts; do
case ${opts} in case ${opts} in
k) CFKEY=${OPTARG} ;; k) CFKEY=${OPTARG} ;;
u) CFUSER=${OPTARG} ;;
h) CFRECORD_NAME=${OPTARG} ;; h) CFRECORD_NAME=${OPTARG} ;;
z) CFZONE_NAME=${OPTARG} ;; z) CFZONE_NAME=${OPTARG} ;;
t) CFRECORD_TYPE=${OPTARG} ;; t) CFRECORD_TYPE=${OPTARG} ;;
@ -78,11 +73,6 @@ if [ "$CFKEY" = "" ]; then
echo "and save in ${0} or using the -k flag" echo "and save in ${0} or using the -k flag"
exit 2 exit 2
fi 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 if [ "$CFRECORD_NAME" = "" ]; then
echo "Missing hostname, what host do you want to update?" echo "Missing hostname, what host do you want to update?"
echo "save in ${0} or using the -h flag" echo "save in ${0} or using the -h flag"
@ -120,8 +110,8 @@ if [ -f $ID_FILE ] && [ $(wc -l $ID_FILE | cut -d " " -f 1) == 4 ] \
CFRECORD_ID=$(sed -n '2,1p' "$ID_FILE") CFRECORD_ID=$(sed -n '2,1p' "$ID_FILE")
else else
echo "Updating zone_identifier & record_identifier" echo "Updating zone_identifier & record_identifier"
CFZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$CFZONE_NAME" -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 ) 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 -Po '(?<="id":")[^"]*' | head -1 )
CFRECORD_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records?name=$CFRECORD_NAME" -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" | grep -Po '(?<="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 -Po '(?<="id":")[^"]*' | head -1 )
echo "$CFZONE_ID" > $ID_FILE echo "$CFZONE_ID" > $ID_FILE
echo "$CFRECORD_ID" >> $ID_FILE echo "$CFRECORD_ID" >> $ID_FILE
echo "$CFZONE_NAME" >> $ID_FILE echo "$CFZONE_NAME" >> $ID_FILE
@ -132,8 +122,7 @@ fi
echo "Updating DNS to $WAN_IP" echo "Updating DNS to $WAN_IP"
RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFRECORD_ID" \ RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFRECORD_ID" \
-H "X-Auth-Email: $CFUSER" \ -H "Authorization: Bearer $CFKEY" \
-H "X-Auth-Key: $CFKEY" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
--data "{\"id\":\"$CFZONE_ID\",\"type\":\"$CFRECORD_TYPE\",\"name\":\"$CFRECORD_NAME\",\"content\":\"$WAN_IP\", \"ttl\":$CFTTL}") --data "{\"id\":\"$CFZONE_ID\",\"type\":\"$CFRECORD_TYPE\",\"name\":\"$CFRECORD_NAME\",\"content\":\"$WAN_IP\", \"ttl\":$CFTTL}")