Skip to content

Instantly share code, notes, and snippets.

@cachenow
Forked from icyleaf/README.md
Created June 6, 2022 10:33
Show Gist options
  • Select an option

  • Save cachenow/fa3b6de7f87a57bf72d8972660e5acd5 to your computer and use it in GitHub Desktop.

Select an option

Save cachenow/fa3b6de7f87a57bf72d8972660e5acd5 to your computer and use it in GitHub Desktop.
Cloudflare DDNS 更新脚本 for Openwrt/Lede

Cloudflare DDNS 更新脚本 for Openwrt/Lede

依赖

  • ddns-script

教程

如果你安装 ddns-script 在添加到供应商看到了 cloudflare 那就不需要本脚本了,如果没有看到那么接着往下看。

先把本脚本 cloudflare_ddns_update.sh 上传只 openwrt 的 /root 目录下面。

再从 Cloudflare 获取 Global API Key 备用(记作为 API_KEY),预先添加一个 A 记录,IP 随意写比如 127.0.0.1, 添加后点击看下面的 API 能够从 APIs 链接获取到 Zone id(记作为 API_ZONE_ID),然后需要通过 curl 获取 dns records 列表接口查看刚才新添加的 A 记录的 DNS Record id(记作为 API_DNS_RECORD_ID)

在 openwrt 的 ddns 添加出供应商选择 Custom,更新脚本填写 /root/cloudflare_ddns_update.sh, 域名填写你的域名,用户名填写你的注册邮箱,密码填写 API_KEY,由于下面额外的参数没有搞懂怎么用, API_ZONE_ID 和 API_DNS_RECORD_ID 获取的值需要填写在脚本对应的变量后面。填写完成后点击保存并应用。

最后点击启用并启动即可。你可以通过系统日志看到类似的日志就说明完成了!

Wed Aug  7 22:21:36 2019 user.notice ddns-scripts[19525]: cloudflare: PID '19525' started at 2019-08-07 22:21
Wed Aug  7 22:21:39 2019 daemon.err uhttpd[5445]:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Wed Aug  7 22:21:39 2019 daemon.err uhttpd[5445]:                                  Dload  Upload   Total   Spent    Left  Speed
Wed Aug  7 22:21:40 2019 daemon.err uhttpd[5445]: 
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   326    0   250  100    76    201     61  0:00:01  0:00:01 --:--:--   317
Wed Aug  7 22:21:41 2019 user.err ddns-scripts[19525]: cloudflare: IP update accepted by DDNS Provider
#!/bin/sh
# 参考资料:
# https://github.com/openwrt/packages/blob/master/net/ddns-scripts/samples/update_sample.sh
# https://github.com/anjianshi/cloudxns-ddns/blob/master/cloudxns.sh
#
# Requirements:
# curl
[ -z "$domain" ] && write_log 14 "Service section not configured correctly! Missing 'domain'"
[ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing 'username'"
[ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing 'password'"
API_URL="https://api.cloudflare.com/client/v4/"
API_EMAIL=$username
API_KEY=$password
API_ZONE_ID="{zone_id}"
API_DNS_RECORD_ID="{dns_record_id"
RESULT=$(curl -X PUT "${API_URL}/zones/${API_ZONE_ID}/dns_records/${API_DNS_RECORD_ID}" \
-H "Content-Type: application/json" \
-H "X-Auth-Email: ${API_EMAIL}" \
-H "X-Auth-Key: ${API_KEY}" \
--data '{"type":"A","name":"'${API_DOMAIN}'","content":"'${__IP}'","ttl":120,"proxied":false}')
write_log 7 "answered:\n$RESULT"
if [ $(echo -n "$RESULT"|grep -o "\"success\":true"|wc -l) = 1 ];then
return 0
else
return 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment