文章參考來源:利用 Cloudflare API v4 建立 DDNS – 桓桓鄉寇 (huanyichuang.com)
主要補充了 Record ID 直接透過指令過濾出來,連貫運行接下來的 IP 修改
作法同樣是抓出該域名下所有 DNS 紀錄,再針對要修改的域名 ,利用 jq 取得 Record ID
jq 需事先安裝好,用法可以參考這邊:Shell:jq 循环 json 对象, jq 循环 json 数组
GLOBAL_API_KEY=
EMAIL=
ZONE_ID=
DOMAIN=$1
PROXY_ENABLE=$2
result=$(curl -sSX GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \
-H "X-Auth-Email: $EMAIL" \
-H "X-Auth-Key: $GLOBAL_API_KEY" \
-H "Content-Type: application/json" \
| jq --arg DOMAIN $DOMAIN '.result | .[] | select(.name == $DOMAIN) |.id,.content' | sed 's|"||g')
id_ip=(${result// / })
record_id=${id_ip[0]}
online_ip=${id_ip[1]}
actual_ip=$(curl -sS ip.me)
if [ "$online_ip" != "$actual_ip" ]; then
curl -sSX PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$record_id" \
-H "X-Auth-Email: $EMAIL" \
-H "X-Auth-Key: $GLOBAL_API_KEY" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"'"$DOMAIN"'","content":"'"$actual_ip"'","ttl":60,"proxied":'"$PROXY_ENABLE"'}' > /dev/null
echo "$online_ip IP change to $actual_ip"
else
echo "$actual_ip IP no changed"
fi
最後運行要修改的 domain name
sh ddns.sh lab.cwlove.idv.tw false
配合排程就可以設定多個域名更新