今天买了个打折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
暂无评论内容