Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Underknowledge/92ac2124d84342dfe35d5783cd9d5bf0 to your computer and use it in GitHub Desktop.
Save Underknowledge/92ac2124d84342dfe35d5783cd9d5bf0 to your computer and use it in GitHub Desktop.
A bash script to update a Cloudflare DNS A record with the external IP of the source machine
#!/usr/bin/env bash
# based on
# https://gist.github.com/foobarhl/2480f956d26d49b035bf03ea1b01b40f
# get tokens
# https://dash.cloudflare.com/profile/api-tokens
command -v host > /dev/null 2>&1 && _DNS_LOOKUP=host
command -v nslookup > /dev/null 2>&1 && _DNS_LOOKUP=nslookup
command -v jq > /dev/null 2>&1 && _JQ=1 || echo "no jq support, you have to set DNS reccord ID manualy"
_SCRIPT_DIR="$(realpath $(dirname "$0"))"
if [ -f $_SCRIPT_DIR/.env ]; then
. $_SCRIPT_DIR/.env
fi
show_help () {
echo "
-n name of the DNS reccord
-z Zone-id
-t cloudflare authentification key
-c check the API key ( flag )
-i set the DNS reccord ID (script ll try to pull this information by itself)
-v additional verose output
-p proxy the connection through cloudflare (default: false)
"
}
OPTIND=1
while getopts "h?n:z:t:ci:vp" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
n)
_DNS_RECCORD=$OPTARG
_ZONE=$(echo $OPTARG | rev | cut -d'.' -f-2 | rev)
;;
z)
_ZONE_ID=$OPTARG
;;
t)
_TOKEN=$OPTARG
;;
c)
_CHECK=1
;;
i)
_DNS_RECCORD_ID=$OPTARG
;;
v)
_VERBOSE=1
;;
p) _PROXIED=true
;;
k)
_TOKEN=$OPTARG
;;
esac
done
shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift
_PROXIED=${_PROXIED:-false}
_DNS_RECCORD=${_DNS_RECCORD:-}
_ZONE_ID=${_ZONE_ID:-}
_TOKEN=${_TOKEN:-}
_IP=$(curl -s -X GET https://checkip.amazonaws.com)
[[ ! -z "$_VERBOSE" ]] && echo "Current IP is $_IP"
# check if either host or nslookup is set
###################### issues around here . host lookup does not trigger (or does it??..)
# todo!ignore when proxy is true
if [ -z "$_DNS_LOOKUP" ]; then
# Check if the reccord needs an update
if $_DNS_LOOKUP $_DNS_RECCORD 1.1.1.1 | grep "has address" | grep "$_IP"; then
echo "$_DNS_RECCORD is currently set to $_IP; no changes needed"
exit
fi
fi
[[ ! -z "$_VERBOSE" ]] && \
echo "dns = $_DNS_RECCORD
zoneid = $_ZONE_ID
auth key = $_TOKEN
DNS reccord id = $_DNS_RECCORD_ID
Cloudflare Auth key = $_TOKEN
"
if [ ! -z "$_CHECK" ]; then
curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \
-H "Authorization: Bearer $_TOKEN" \
-H "Content-Type:application/json"
fi
[[ ! -z "$_JQ" ]] && _ZONE_ID=${_ZONE_ID:-$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$_ZONE&status=active" \
-H "Authorization: Bearer $_TOKEN" \
-H "Content-Type: application/json" | jq -r '{"result"}[] | .[0] | .id')}
# get the dns record id
# skip when ID is set?..
[[ ! -z "$_JQ" ]] && _DNS_RECCORD_ID=${_DNS_RECCORD_ID:-$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$_ZONE_ID/dns_records?type=A&name=$_DNS_RECCORD" \
-H "Authorization: Bearer $_TOKEN" \
-H "Content-Type: application/json" | jq -r '{"result"}[] | .[0] | .id')}
echo ID $_DNS_RECCORD_ID
## update the record
if [ "$_DNS_RECCORD_ID" == "null" ]; then
echo "The DNS reccord ID could not be determinated, exiting"
exit 1
else
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$_ZONE_ID/dns_records/$_DNS_RECCORD_ID" \
-H "Authorization: Bearer $_TOKEN" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"$_DNS_RECCORD\",\"content\":\"$_IP\",\"ttl\":1,\"proxied\":$_PROXIED}" | jq
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment