Skip to content

Instantly share code, notes, and snippets.

@machsix
Last active November 9, 2019 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save machsix/590a26b4f6cbfda52e614cb67d06ba6f to your computer and use it in GitHub Desktop.
Save machsix/590a26b4f6cbfda52e614cb67d06ba6f to your computer and use it in GitHub Desktop.
#!/bin/bash
# CHANGE THESE
API=1aeb5xxxxxxxxxx9dd194c56d07caf311
EMAIL="example@gmail.com"
DOMAIN="example.net"
LOG_DEV="/var/log/ddns.json"
get_zone_id () {
local zone_id=`curl -fs -X GET "https://api.cloudflare.com/client/v4/zones" \
-H "X-Auth-Email: ${EMAIL}" \
-H "X-Auth-Key: ${API}" \
-H "Content-Type: application/json" | jq -r ".result[] | select(.name==\"${DOMAIN}\") | .id"`
echo $zone_id
}
ZONE_ID=$(get_zone_id)
get_record_id () {
local record=$1
local record_id=`curl -s X GET "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records?type=A&name=${record}" \
-H "Content-Type:application/json" \
-H "X-Auth-Key:${API}" \
-H "X-Auth-Email:${EMAIL}" | jq -r ".result[0].id"`
echo $record_id
}
get_ip () {
echo `curl -fs http://ipv4.icanhazip.com`
}
set_ip () {
local ip=$1
local record=$2
local proxied=$3
local record_id=`get_record_id $record`
local log=`curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${record_id}" \
-H "X-Auth-Email: $EMAIL" \
-H "X-Auth-Key: $API" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"${record}\",\"content\":\"$ip\",\"ttl\":1,\"proxied\":${proxied}}"`
echo ${log} | jq "." >> ${LOG_DEV}
local status=`echo ${log} | jq -r ".success"`
if [[ $status == "true" ]]; then
echo "0"
else
echo "1"
fi
}
domains=("a.example.net" "b.example.net" "c.example.net")
proxy=("true" "false" "false")
n=`echo ${#domains[@]}-1 | bc -l`
IP=$(get_ip)
cat /dev/null > ${LOG_DEV}
for i in `seq 0 $n`; do
d=${domains[$i]}
p=${proxy[$i]}
echo "Set $d => $IP Proxy $p"
STATUS=$(set_ip "$IP" "$d" "$p")
echo "Status ${STATUS}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment