└── ddns.sh /ddns.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # CF DDNS 3 | 4 | #这里填写区域ID 5 | zone_id=当前域名的区域ID 6 | #API Bearer密钥,在 https://dash.cloudflare.com/profile/api-tokens 创建编辑区域 DNS 7 | bearer=你的Bearer密钥 8 | #需要修改DNS的域名 9 | domain=完整域名 10 | #DDNS IP类型ipv4,ipv6,默认是ipv4 11 | ips=ipv4 12 | 13 | function cloudflaredns(){ 14 | id=$(curl -s "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records" -H "Authorization: Bearer $bearer" | sed -e 's/{/\n/g' -e 's/"proxiable"/\n/g' | grep zone_id | awk -F\" '{print $4" "$16" "$20}' | grep -w $domain | awk '{print $1}') 15 | if [ $(echo $1 | grep : | wc -l) == 0 ] 16 | then 17 | iptype=A 18 | else 19 | iptype=AAAA 20 | fi 21 | if [ -z "$id" ] 22 | then 23 | echo "创建域名 $domain $iptype记录" 24 | curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records" -H "Authorization: Bearer $bearer" -H "Content-Type:application/json" --data '{"type":"'"$iptype"'","name":"'"$domain"'","content":"'"$1"'","ttl":60,"proxied":false}' 25 | else 26 | echo "更新域名 $domain $iptype记录" 27 | curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$id" -H "Authorization: Bearer $bearer" -H "Content-Type:application/json" --data '{"type":"'"$iptype"'","name":"'"$domain"'","content":"'"$1"'","ttl":60,"proxied":false}' 28 | fi 29 | } 30 | 31 | a="$(curl --$ips -s https://www.cloudflare-cn.com/cdn-cgi/trace | grep ip= | cut -f 2- -d'=')" 32 | cloudflaredns $a 33 | while true 34 | do 35 | sleep 5 36 | b="$(curl --$ips -s https://www.cloudflare-cn.com/cdn-cgi/trace | grep ip= | cut -f 2- -d'=')" 37 | if [ "$a" == "$b" ] 38 | then 39 | echo "$(date) 公网IP地址 $b 没有发生变化" 40 | elif [ "$b" != "" ] 41 | then 42 | cloudflaredns $b 43 | while true 44 | do 45 | c="$(curl -s "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records" -H "Authorization: Bearer $bearer" | sed -e 's/{/\n/g' -e 's/"proxiable"/\n/g' | grep zone_id | awk -F\" '{print $4" "$16" "$20" "$24}' | grep -w $domain | awk '{print $4}')" 46 | if [ "$b" == "$c" ] 47 | then 48 | echo "公网IP更新成功" 49 | break 50 | else 51 | echo "重新请求更新公网IP" 52 | cloudflaredns $b 53 | fi 54 | done 55 | #这里支持pushplsh微信推送,注释下方curl前面的#号并填写pushplus密钥启用 56 | #curl --location --request POST 'https://www.pushplus.plus/send' --header 'Content-Type: application/json' --data-raw '{"token":"你的pushplus密钥","title":"公网IP地址已更新","content":"当前公网IP '"$b"'","template":"txt"}' 57 | a="$b" 58 | else 59 | echo "公网IP地址获取失败" 60 | fi 61 | done 62 | --------------------------------------------------------------------------------