Skip to content

Instantly share code, notes, and snippets.

/-

Created December 13, 2016 02:36
Show Gist options
  • Save anonymous/c04327a5b2540f05d1181ef01054fc47 to your computer and use it in GitHub Desktop.
Save anonymous/c04327a5b2540f05d1181ef01054fc47 to your computer and use it in GitHub Desktop.
#!/bin/sh
## Load config
if [ -L $0 ]
then
source `readlink -e $0 | sed "s:[^/]*$:config:"`
else
source `echo $0 | sed "s:[^/]*$:config:"`
fi
## Added Functions
case $1 in
getdns )
echo "getting all DNS records"
curl -X GET "https://api.cloudflare.com/client/v4/zones?name=$DOMAIN_NAME" \
-H "X-Auth-Email: $EMAIL_ADDR" \
-H "X-Auth-Key: $AUTH_KEY" \
-H "Content-Type: application/json"|jq
## CloudFlare legacy API
# curl https://www.cloudflare.com/api_json.html \
# -d 'a=rec_load_all' \
# -d "tkn=$AUTH_KEY" \
# -d "email=$EMAIL_ADDR" \
# -d "z=$DOMAIN_NAME"|jq
;;
zonelookup )
curl -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?name=dyn.$DOMAIN_NAME" \
-H "X-Auth-Email: $EMAIL_ADDR" \
-H "X-Auth-Key: $AUTH_KEY" \
-H "Content-Type: application/json"|jq
;;
update )
## Checking if an update is necessery.
[ ! -f ./currentip ] && touch ./currentip
## Original Suggestion for resolving external IP
## NEWIP=`dig +short myip.opendns.com @resolver1.opendns.com`
echo "getting external IP"
NEWIP=`curl https://ifconfig.co`
echo External IP appears to be $NEWIP
CURRENTIP=`cat ./currentip`
echo Reading stored IP
if [ "$NEWIP" = "$CURRENTIP" ]
then
## This section should be replaced with something smarter.
echo "IP address unchanged"
else
echo "Updating DNS records for dyn.$DOMAIN_NAME to point at $NEWIP"
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECID" \
-H "X-Auth-Email: $EMAIL_ADDR" \
-H "X-Auth-Key: $AUTH_KEY" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"dyn.$DOMAIN_NAME\",\"content\":\"$NEWIP\"}"
echo $NEWIP > /var/tmp/currentip.txt
## CloudFlare legacy API
# curl https://www.cloudflare.com/api_json.html \
# -d 'a=rec_edit' \
# -d "tkn=$AUTH_KEY" \
# -d email="$EMAIL_ADDR" \
# -d "z=$DOMAIN_NAME" \
# -d "id=RECID" \
# -d 'type=A' \
# -d "name=dyn.$DOMAIN_NAME" \
# -d 'ttl=1' \
# -d "content=$NEWIP"
echo $NEWIP > ./currentip
fi
;;
* )
echo "usage: ./cloudflare.sh getdns|update"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment