nat小鸡通过cloudflare自建ddns方便连接,使用zone token 而不是 account api key

今天买了个打折nat,发现公网ip会经常变化。数年前我也买过一个nat,但那个可以通过商家提供的域名访问,这家没有,所以要自建ddns。

简单搜索得知一般通过定时脚本或ddns-go项目实现需求,由于nat小鸡配置极低,为了节省性能,还是用脚本吧。商家提供的脚本文档https://aipeach.gitbook.io/blogbackup/cloudflare-da-jian-ddns-jiao-ben-ban

由于脚本中用的是账号api key,感觉十分难受,因此将其修改成了使用区域token,只给编辑区域dns的权限,舒服很多:

https://github.com/l1ngth6/cloudflare-api-v4-ddns

变更:

  • 使用cloudflare zone token,而不是account api key。
  • 支持添加解析注释。

使用

下载脚本

curl https://raw.githubusercontent.com/l1ngth6/cloudflare-api-v4-ddns/master/cf-v4-ddns.sh > /root/cf-v4-ddns.sh && chmod +x /root/cf-v4-ddns.sh

修改脚本 补充信息

vim cf-v4-ddns.sh
# Create a token with Zone:DNS:Edit permissions for specific zones
# 为特定区域创建具有 Zone:DNS:Edit 权限的令牌
CFTOKEN=

# Zone ID, found in the Cloudflare dashboard overview page
# 区域 ID,可在 Cloudflare 仪表板概览页面右侧找到
CFZONE_ID=

# Hostname or subdomain to update, eg: homeserver or homeserver.example.com
# 通常填写需要使用的完整子域名
CFRECORD_NAME=

# Comment for the DNS record (optional)
# 选填 注释
CFRECORD_COMMENT=""

运行

首次运行脚本,输出内容会显示当前IP,进入cloudflare查看 确保IP已变更为当前IP

./cf-v4-ddns.sh

测试时,当nat ip 未变化时,可以强制刷新解析以查看更改

./cf-v4-ddns.sh -f true

添加定时任务

crontab -e
*/2 * * * * /root/cf-v4-ddns.sh >/dev/null 2>&1

# 如果需要日志,替换上一行代码
*/2 * * * * /root/cf-v4-ddns.sh >> /var/log/cf-ddns.log 2>&1

调试模式
./script.sh -k "your_api_token" -i "your_zone_id" -h "subdomain" -d true

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!无需注册,过短或乱码评论会被屏蔽。
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容