-
-
Save Leopere/f33509802a75d1a835bbe94e496ead4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
## Inspired by | |
## http://epistrephe.in/2016-09-21/cloudflare-dynamic-dns/ | |
## 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Inspired by | |
## http://epistrephe.in/2016-09-21/cloudflare-dynamic-dns/ | |
# X-Auth-Email | |
EMAIL_ADDR='[email protected]' | |
# X-Auth-Key | |
## Obtained at the bottom of https://www.cloudflare.com/my-account | |
AUTH_KEY="REPLACEME" | |
# External IP Address | |
EXTERNAL_IP=`curl ifconfig.co` | |
# DomainName | |
DOMAIN_NAME="REPLACEME" | |
# ZoneID | |
ZONE_ID="REPLACEME" | |
# Record ID | |
RECID="REPLACEME" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment