77 lines
1.7 KiB
Bash
Executable File
77 lines
1.7 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Partly adapted from: https://github.com/ddries/d2c.sh/blob/master/d2c.sh
|
|
#
|
|
# requires: wget, ca-certificates, grep, jq
|
|
#rm -f /tmp/cookies.txt
|
|
|
|
COOKIEFILE="/tmp/cookies-cloudflareCtrl.txt"
|
|
|
|
source ~/.cloudflare
|
|
#ZONE_ID=
|
|
#API_KEY=
|
|
|
|
URL="https://api.cloudflare.com/client/v4"
|
|
|
|
# * * * NO MORE CHANGES DOWN HERE * * *
|
|
|
|
COMMANDS="sed grep curl jq"
|
|
for COMMAND in $COMMANDS; do
|
|
if ! command -v $COMMAND &> /dev/null
|
|
then
|
|
echo "$COMMAND could not be found!"
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
# parse parameters
|
|
DOMAIN=$(echo $1 | sed -r 's/^.*\.(.+\..+)$/\1/')
|
|
SUBDOMAIN=$(echo $1 | sed -r 's/^(.+)\..+\..+$/\1/')
|
|
|
|
if [ "$DOMAIN" == "$SUBDOMAIN" ]; then
|
|
SUBDOMAIN=""
|
|
fi
|
|
|
|
echo "DOMAIN=${DOMAIN}"
|
|
echo "SUBDOMAIN=${SUBDOMAIN}"
|
|
|
|
UPDATE_IP=$2
|
|
|
|
#################################################
|
|
|
|
# get records from Cloudflare
|
|
RRAW=$(curl --silent --request GET \
|
|
--url ${URL}/zones/${ZONE_ID}/dns_records \
|
|
--header 'Content-Type: application/json' \
|
|
--header "Authorization: Bearer ${API_KEY}" \
|
|
| jq '.result[] | select(.type == "AAAA" and .name == "'${DOMAIN}'") | [.id, .name, .ttl, .content, .type]'
|
|
)
|
|
|
|
#echo $RRAW
|
|
|
|
id=$(jq -r '.[0]' <<< "${RRAW}")
|
|
content=$(jq -r '.[3]' <<< "${RRAW}")
|
|
|
|
#echo "old ip: ${content}"
|
|
|
|
#if [ "$UPDATE_IP" != "$content" ]; then
|
|
# update DNS
|
|
curl --silent --request PATCH \
|
|
--url "${URL}/zones/${ZONE_ID}/dns_records/${id}" \
|
|
--header 'Content-Type: application/json' \
|
|
--header "Authorization: Bearer ${API_KEY}" \
|
|
--data '{
|
|
"content": "'${UPDATE_IP}'",
|
|
"comment": "Updated by ffx at '$(date -Is)'"
|
|
}'
|
|
|
|
echo "OK: ${name}"
|
|
#else
|
|
# echo "${name} did not change"
|
|
#fi
|
|
|
|
SUCCESS=$?
|
|
echo "Retval: $SUCCESS"
|
|
|
|
exit $SUCCESS
|