MinaMiGo
永遠の夏
Koizumi's Blog

Cloudflare DDNS 脚本(支持IPv4和IPv6)

一、前言

这个脚本忘了从哪里搞来的了,今天要用的时候发现并用不了???然后测试了一下发现api调用有一处错误,就修改了一下。然后又丢上来水一篇文章。

二、正文

1.参数讲解

# CloudFlare 注册邮箱 
auth_email="" 
# CloudFlare Global API Key ,在cloudflare登录后,查看个人资料,在APi令牌页面查看
auth_key="" 
# 根域名,比如你要ddns www.google.com,那么根域名就填google.com
zone_name="" 
# DDNS的域名,比如你想ddns www.google.com,那么就填www.google.com
record_name="" 
# IP类型,填ipv4或ipv6
type="ipv6"

2.代码本体

最后更新于2022-01-20。

那么就是代码本体了。把下面的内容保存为一个脚本,然后在服务器上使用crontab定时运行就可以了,一个小时一次就可以,api调用貌似是有限制的1小时5次。

#!/bin/bash
##############  用户配置(修改这里)  ###############
# CloudFlare 注册邮箱
auth_email="" 
# CloudFlare Global API Key
auth_key=""  
# 根域名
zone_name="" 
# DDNS的域名
record_name=""
# IP类型,填ipv4或ipv6
type="ipv6"
#############  脚本配置(不懂不要修改)  ############
# 变动前的公网 IP 保存位置
ip_file="./ip.txt"
# 域名识别信息保存位置
id_file="./cloudflare.ids"
# 监测日志保存位置
log_file="./cloudflare.log"

################  下面的不懂不要改  ###############
##################  功能定义  ####################
record_type=""
ip=""
zone_identifier=""
record_identifier=""
update=""
#日志
log() {
    if [ "$1" ]; then
        echo -e "[$(date)] - $1" >> $log_file
    fi
}
#获取本机IP
get_ip() {
    if [ $type == "ipv4" ]; then
	    record_type="A"
	    ip=$(curl -s http://v4.ipv6-test.com/api/myip.php)
    elif [ $type == "ipv6" ]; then
	    record_type="AAAA"
	    ip=$(curl -s http://v6.ipv6-test.com/api/myip.php)
    else
	    echo "Type wrong"
	    log "Type wrong"
        exit 0
    fi
}
#判断IP是否变化,不变化则结束程序
check_ip_change() {
    if [ -f $ip_file ]; then
        old_ip=$(cat $ip_file)
        if [ "$ip" == "$old_ip" ]; then
            echo "IP has not changed."
            log "IP has not changed."
            exit 0
        fi
    fi
}
#获取域名zone_id和子域名记录ID
get_id() {
    if [ -f $id_file ] && [ $(wc -l $id_file | cut -d " " -f 1) == 2 ]; then
        zone_identifier=$(head -1 $id_file)
        record_identifier=$(tail -1 $id_file)
    else
        zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
        rec_response_json=`curl -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json"`
        record_identifier=`echo $rec_response_json | grep -Po '(?<="id":")[^"]*'`
        echo "$zone_identifier" > $id_file
        echo "$record_identifier" >> $id_file
    fi
}
#更新 DNS 记录
update_dns() {
    update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"$record_type\",\"name\":\"$record_name\",\"content\":\"$ip\"}")
}
###################  脚本主体  ###################
log "Script start."
#获取IP
get_ip
#判断是否成功获取到IP
if [ "$ip" == "" ]; then
    echo "Can not get IP address.Please check your network connection."
    log "Can not get IP address.Please check your network connection."
    exit 0
fi
#检查IP是否变化
check_ip_change
#获取域名zone_id和子域名记录ID
get_id
#判断是否成功获取到ID
if [ "$zone_identifier" == "" ]; then
    echo "Can not get zone_id."
    log "Can not get zone_id."
    exit 0
elif [ "$record_identifier" == "" ]; then
    echo "Can not get record_id."
    log "Can not get record_id."
    exit 0
fi
#更新 DNS 记录
update_dns
#判断是否成功
if [[ $update == *"\"success\":false"* ]]; then
    log "API UPDATE FAILED. DUMPING RESULTS:\n$update"
    echo -e "API UPDATE FAILED. DUMPING RESULTS:\n$update"
    exit 0
else
    echo "$ip" > $ip_file
    log "$record_name IP changed to: $ip"
    echo "$record_name IP changed to: $ip"
fi

 

本篇文章链接:https://blog.minamigo.moe/archives/135
转载请注明出处!日常分类下的文章禁止转载。
没有标签
首页      编程      linux      Cloudflare DDNS 脚本(支持IPv4和IPv6)

MinaMiGo

文章作者

发表回复

textsms
account_circle
email

  • kezhh

    安装了warp DDNS的不是本机IP 请问UP怎么解决啊

    3 年前 回复
  • czs

    运行后提示:Can not get IP address.Please check your network connection.,是不是脚本哪里还有问题呢,谢谢

    4 年前 回复
    • MinaMiGo博主

      @czs: 自行检查网络到检测服务器地址的连接,也有可能是curl的问题。

      4 年前 回复

Koizumi's Blog

Cloudflare DDNS 脚本(支持IPv4和IPv6)
一、前言 这个脚本忘了从哪里搞来的了,今天要用的时候发现并用不了???然后测试了一下发现api调用有一处错误,就修改了一下。然后又丢上来水一篇文章。 二、正文 1.参数讲解 # Clo…
扫描二维码继续阅读
2020-06-02