Zavier's Blog

(2024年重写)基于shell的cloudflare动态ddns脚本

说明:

1.该脚本需在Linux操作系统下运行,并配合crontab定时运行,请确保crond服务处于开启状态,建议1分钟检测一次IP变动,配置如下:

crontab -e
*/1 * * * * cfddns.sh

2.APIKEY和ZONE ID的具体获取方法参照以往发布过的说明:CFDDNS脚本

以下为A记录脚本内容(适用于动态IPv4):

#!/bin/sh
CF_API_KEY=
CF_ZONE_ID=
#Your cloudflare E-mail
EMAIL=
#Your network device, e.g.,eth0 
ROUTER_NETWORK_DEVICE=
#Your DNS record, e.g.,www.domain.com 
DNS_RECORD=
CF_DNS_ID=$(curl -X GET "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records?type=A&name=${DNS_RECORD}" -H "X-Auth-Email:${EMAIL}" -H "X-Auth-Key:${CF_API_KEY}" -H "Content-Type: application/json"|sed 's/,/\n/g'|grep -E "result.*id"|awk -F ":" '{print $3}'|sed 's/"//g')
CURRENT_RESOLV=$( curl -k -X GET "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records/${CF_DNS_ID}" \
-H "X-Auth-Email:${EMAIL}" \
-H "X-Auth-Key:${CF_API_KEY}" \
-H "Content-Type: application/json" |awk -F '"' '{print $26}' )
CURRENT_IP=$( ifconfig $ROUTER_NETWORK_DEVICE | awk -F'[ ]+|:' '/inet /{print $4}' )
if [ $CURRENT_RESOLV == $CURRENT_IP ]; 
then
exit 1
else
curl -k -X PUT "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records/${CF_DNS_ID}" \
-H "X-Auth-Email:${EMAIL}" \
-H "X-Auth-Key:${CF_API_KEY}" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"'$DNS_RECORD'","content":"'$CURRENT_IP'","ttl":1,"proxied":false}'
fi


以下为AAAA记录DDNS脚本(适用于动态ipv6):

#!/bin/sh
CF_API_KEY=
CF_ZONE_ID=
#Your cloudflare E-mail
EMAIL=
#Your network device, e.g.,eth0                     
ROUTER_NETWORK_DEVICE=
#Your DNS record, e.g.,www.domain.com        
DNS_RECORD=
                  
CF_DNS_ID=$(curl -X GET "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records?type=AAAA&name=${DNS_RECORD}" -H "X-Auth-Email:${EMAIL}" -H "X-Auth-Key:${CF_API_KEY}" -H "Content-Type: application/json"|sed 's/,/\n/g'|grep -E "result.*id"|awk -F ":" '{print $3}'|sed 's/"//g')
CURRENT_RESOLV=$( curl -k -X GET "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records/${CF_DNS_ID}" \
-H "X-Auth-Email:${EMAIL}" \
-H "X-Auth-Key:${CF_API_KEY}" \
-H "Content-Type: application/json" |awk -F '"' '{print $26}' )
CURRENT_IP=$( ifconfig $ROUTER_NETWORK_DEVICE |grep "prefixlen 128"|awk '{print $2}' )
if [ $CURRENT_RESOLV == $CURRENT_IP ]; 
then
exit 1
else
curl -k -X PUT "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records/${CF_DNS_ID}" \
-H "X-Auth-Email:${EMAIL}" \
-H "X-Auth-Key:${CF_API_KEY}" \
-H "Content-Type: application/json" \
--data '{"type":"AAAA","name":"'$DNS_RECORD'","content":"'$CURRENT_IP'","ttl":1,"proxied":false}'
fi



发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

搜你所想
畅所欲言
最近发表
文章归档
友情链接

© 2017-2024 Zavier's Blog.|自渡博客