-
-
Save shellexy/5e3fb3b87c0afb51b3c7c1daefe4109d to your computer and use it in GitHub Desktop.
Dynamic DNS script using Cloudflare API v4
This file contains hidden or 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/bash | |
# can be A or AAAA | |
do_record=AAAA | |
# the interface to get ipv6 address | |
iface=eth0 | |
# the static suffix we should add to | |
v6_suf=53 | |
mail="[email protected]" | |
authkey="012345689abcdef0123456789abcdef" | |
record=aaa.com | |
cachef=/tmp/ddns-$record | |
cfupdate () { | |
_type=$1 | |
_addr=$2 | |
echo "Update $_type record for $record to $_addr" | |
ret=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/$recordid" \ | |
-H "X-Auth-Email: $mail" \ | |
-H "X-Auth-Key: $authkey" \ | |
-H "Content-Type: application/json" \ | |
--data '{"type":"'$_type'","name":"'$record'","content":"'$_addr'"}') | |
if [[ -z $(echo $ret|grep -oP "success\":[^\"]+"|grep true) ]]; then | |
echo $ret | |
else | |
echo "OK." | |
fi | |
} | |
if [[ ! -e $cachef ]]; then | |
touch $cachef | |
fi | |
if [[ $(cat $cachef|grep "id,"|wc -l) -ne 2 ]]; then | |
domain=$(echo $record |grep -oP "[^.]+\.[^.]+$") | |
ret=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$domain" \ | |
-H "X-Auth-Email: $mail" \ | |
-H "X-Auth-Key: $authkey" \ | |
-H "Content-Type: application/json") | |
zoneid=$(echo $ret|grep -oP "\"id\":\"[a-f\d]{32}"|grep -oP "[a-f\d]{32}"|head -n1) | |
ret=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records?name=$record&type=$do_record" \ | |
-H "X-Auth-Email: $mail" \ | |
-H "X-Auth-Key: $authkey" \ | |
-H "Content-Type: application/json") | |
recordid=$(echo $ret|grep -oP "\"id\":\"[a-f\d]{32}"|grep -oP "[a-f\d]{32}"|head -n1) | |
echo "recordid,$do_record,$recordid" >> $cachef | |
echo "zoneid,$zoneid" >> $cachef | |
else | |
recordid=$(cat $cachef|grep "recordid,$do_record"|grep -oP "[a-f\d]{32}") | |
zoneid=$(cat $cachef|grep zoneid|grep -oP "[a-f\d]{32}") | |
fi | |
if [[ -z $recordid || -z $zoneid ]]; then | |
echo "Unable to get recordid or zoneid" | |
rm -f $cachef | |
exit 0 | |
fi | |
if [[ $do_record = "A" ]]; then | |
addr_v4=$(curl ipip.tk/ip -4s) | |
if [[ -z $(cat $cachef|grep $addr_v4) ]]; then | |
sed -i "/v4,/d" $cachef | |
echo "v4,$addr_v4" >> $cachef | |
cfupdate "A" $addr_v4 | |
fi | |
else | |
# check if we have ipv6 address | |
if [[ -z $(ifconfig $iface|grep inet6|grep -P "2[\da-f:]{4,}") ]]; then exit; fi | |
addr_v6_prefix=$(curl ipip.tk/ip -6s|grep -oP "[\da-f]+:[\da-f]+:[\da-f]+:[\da-f]+"|cut -f1|head -n1 ) | |
# check if we really can use ipv6 | |
if [[ -z $addr_v6_prefix ]]; then exit; fi | |
addr_v6="$addr_v6_prefix::$v6_suf" | |
if [[ -z $(ifconfig $iface|grep $addr_v6) ]]; then | |
echo "Adding $addr_v6 to $iface" | |
ip -6 addr add $addr_v6/64 dev $iface | |
fi | |
if [[ -z $(cat $cachef|grep $addr_v6) ]]; then | |
sed -i "/v6,/d" $cachef | |
echo "v6,$addr_v6" >> $cachef | |
cfupdate "AAAA" $addr_v6 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment