以下是脚本内容,存放路径随意,这里脚本命名为cf_flashdns.sh,可以使用cron定时执行该脚本。
【按实际情况修改红字的参数即可】
#!/bin/sh
# 將{ }替換為自己的資料
# GET Recore ID
#curl -x get "https://api.cloudflare.com/client/v4/zones/{YOUR_ZONE_ID}/dns_records" \
#-h "x-auth-email:{[email protected]}" \
#-h "x-auth-key:{YOUR_GLOBAL_API_KEY}" \
#-h "content-type: application/json"
NEW_IP=`curl -s http://ipv4.icanhazip.com`
CURRENT_IP=`cat /tmp/current_ip.txt`
if [ "$NEW_IP" = "$CURRENT_IP" ]
then
echo "No Change in IP Adddress"
else
curl -X PUT "https://api.cloudflare.com/client/v4/zones/{YOUR_ZONE_ID}/dns_records/{YOUR_RECORD_ID}" \
-H "X-Auth-Email: {[email protected]}" \
-H "X-Auth-Key: {YOUR_GLOBAL_API_KEY}" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"{YOUR-DDNS.exampel.com}","content":"'$NEW_IP'","ttl":120,"proxied":false}' > /dev/null
echo $NEW_IP > /tmp/current_ip.txt
fi