Repository: wherelse/cloudflare-ddns-script
Branch: master
Commit: 88b12f01d78c
Files: 4
Total size: 8.9 KB
Directory structure:
gitextract_58972c6l/
├── .gitattributes
├── README-EN.md
├── README.md
└── cloudflare-ddns.sh
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitattributes
================================================
# Auto detect text files and perform LF normalization
* text=auto
================================================
FILE: README-EN.md
================================================
# CloudFlare DDNS script/Raspberry pi IPV6 DDNS Solution
### update[ServerChan version](https://github.com/wherelse/cloudFlare-ddns-script/tree/ServerPush)
English | [中文](/README.md)
### Overview
This script is a DDNS script based on the cloudflare API, which supports IPV4 and IPV6. The IP address of the script installation host can be obtained through the network and local methods. Theoretically supports all hosts using the Linux system. It has been tested and available on debian and ubuntu.
### What to do before using a script
1. A liunx device that can be connected to the Internet.
2. Have a domain name.
3. Register a CloudFlare account ( www.cloudflare.com ), add the domain to the account. After the configuration is complete, add an AAAA resolution to the IPV6 address of the service device as required, and set it to perform DNS resolution only.
4. Query the Globe API Key of CloudFlare account and record it for subsequent configuration.
### Instructions
Open a terminal window and execute the following procedure:
```shell
wget https://raw.githubusercontent.com/wherelse/cloudflare-ddns-script/master/cloudflare-ddns.sh
sudo chmod +x /home/username/cloudflare-ddns.sh #Directory changes based on actual users, you should change the username.
```
The personal configuration information in the script needs to be changed, and the directory is consistent with the previous command
```shell
sudo nano /home/username/cloudflare-ddns.sh
#or
sudo vi /home/username/cloudflare-ddns.sh
```
Find the following to make changes
```shell
auth_email="xxxxxxx@xxxx.com" #Your CloudFlare account email
auth_key="*****************" #Yout cloudflare account Globel ID
zone_name="Your main Domain" #Your zone domain name
record_name="Your Full Domain" #Your full record name
ip_index="local" #Domain acquisition method, local or network
#use "internet" or "local",Use local or network to obtain the address
eth_card="eth0"
#The network card bound when using the local acquisition method, the network method can be used without change.
#Get the network card bound by ip in local mode, default is eth0, only local mode is effective
```
Take any domain name as an example, the domain name ipv6.google.com, the zone_name is `google.com` and the record_name is` ipv6.google.com`. After modification, save and exit.
Enter the following at the terminal to run the script:
```shell
bash /home/username/cloudflare-ddns.sh
```
If it says `IP changed to: xxxxx` or` IP has not changed.`, the configuration is successful.
#### Schedule script
In order to achieve dynamic domain name resolution, the script must be kept running to obtain the IP status. Here the system crontab is used for timing.
Enter the `crontab -e` at the terminal,Add the following at the end of the file:
```shell
*/5 * * * * /home/username/cloudflare-ddns.sh >/dev/null 2>&1
```
Save and exit after making changes.Set the script here to execute the `cloudflare-ddns.sh` script every five minutes to achieve dynamic domain name resolution.
### FAQ
When the error log is:
`API UPDATE FAILED. DUMPING RESULTS:`
`{"success":false,"errors":[{"code":7001,"message":"Method PUT not available for that URI."}],"messages":[],"result":null}`
Delete the `cloudflare.ids` file in the script running directory, and then try to run again.
================================================
FILE: README.md
================================================
# CloudFlare DDNS script/Raspberry pi IPV6 DDNS Solution
#### cloudflare ddns 脚本/树莓派IPV6 DDNS解决方案
中文 | [English](/README-EN.md)
## 更新[Server酱支持版本](https://github.com/wherelse/Raspberrypi-IPV6-DDNS-Solution/tree/ServerPush)
### 概述
本脚本是基于cloudflare API的DDNS脚本,支持IPV4和IPV6,可通过网络方式和本地方式获取脚本安装主机的IP地址,理论支持所有使用linux系统的主机,已在debian和ubuntu上测试可用。
### 使用脚本前需要做的
1. 一台可联网的liunx设备
2. 拥有一个域名,免费的或者收费的都可以(中国大陆的域名需要备案)
3. 注册一个CloudFlare账户 ( www.cloudflare.com ), 并将需要使用的域名添加到账户上,完成配置后根据需要添加服务设备的IPV6地址添加一个AAAA解析,并设为仅进行DNS解析
4. 查询CloudFlare账户的Globel API Key并记录下来,用于后续配置
### 使用方法
打开命令窗口,执行以下程序:
```shell
wget https://raw.githubusercontent.com/wherelse/cloudflare-ddns-script/master/cloudflare-ddns.sh
sudo chmod +x /home/username/cloudflare-ddns.sh #目录根据实际用户等进行更改
```
需要对脚本内的个人配置信息进行更改,目录和上一条命令保持一致
```shell
sudo nano /home/username/cloudflare-ddns.sh
#或
sudo vi /home/username/cloudflare-ddns.sh
```
找到如下内容进行更改
```shell
auth_email="xxxxxxx@xxxx.com" #你的CloudFlare注册账户邮箱
auth_key="*****************" #你的cloudflare账户Globel ID
zone_name="Your main Domain" #你的域名
record_name="Your Full Domain" #完整域名
ip_index="local" #域名获取方式,本地或者网络
#use "internet" or "local",使用本地方式还是网络方式获取地址
eth_card="eth0" #使用本地获取方式时绑定的网卡,使用网络方式可不更改
#使用本地方式获取ip绑定的网卡,默认为eth0,仅本地方式有效
```
以任意一个域名为例,ipv6.google.com 这个域名,zone_name为 `google.com` 和record_name则为 `ipv6.google.com` ,修改完成后,保存并退出。
在命令行中输入以下内容运行脚本:
```shell
bash /home/username/cloudflare-ddns.sh
```
如果提示 `IP changed to: xxxxx` 或 `IP has not changed.` 则说明配置成功了
**定时运行脚本**
为了实现动态域名解析,必须让脚本保持运行以获取IP状态,这里使用系统crontab定时
在命令行输入:`crontab -e` 后在文件最后添加以下内容
```shell
*/5 * * * * /home/username/cloudflare-ddns.sh >/dev/null 2>&1
```
更改完成后保存并退出。
在这里将脚本设置为每五分钟执行一次 `cloudflare-ddns.sh` 脚本,就可以实现动态域名解析了。
### 结束
该脚本不仅适用于树莓派,在其他Linux服务器上也适用,使用时都需要根据自己的实际情况更改以上配置时使用的路径
### FAQ
错误日志为以下内容时:
`API UPDATE FAILED. DUMPING RESULTS:`
`{"success":false,"errors":[{"code":7001,"message":"Method PUT not available for that URI."}],"messages":[],"result":null}`
删除脚本运行目录下的`cloudflare.ids`文件,然后再次尝试运行。
================================================
FILE: cloudflare-ddns.sh
================================================
#!/bin/bash
# CHANGE THESE
auth_email="xxxxxxx@xxxx.com" #你的CloudFlare注册账户邮箱,your cloudflare account email address
auth_key="*****************" #你的cloudflare账户Globel ID ,your cloudflare Globel ID
zone_name="Your main Domain" #你的域名,your root domain address
record_name="Your Full Domain" #完整域名,your full domain address
record_type="AAAA" #A or AAAA,ipv4 或 ipv6解析
ip_index="local" #use "internet" or "local",使用本地方式还是网络方式获取地址
eth_card="eth0" #使用本地方式获取ip绑定的网卡,默认为eth0,仅本地方式有效,the default ethernet card is eth0
ip_file="ip.txt" #保存地址信息,save ip information in the ip.txt
id_file="cloudflare.ids"
log_file="cloudflare.log"
if [ $record_type = "AAAA" ];then
if [ $ip_index = "internet" ];then
ip=$(curl -6 ip.sb)
elif [ $ip_index = "local" ];then
if [ "$user" = "root" ];then
ip=$(ifconfig $eth_card | grep 'inet6' | cut -f2 | awk '{ print $2}' | grep -v '^::1$' | grep -v '^fe80' | grep -v '^f[d|c]' | head -1)
else
ip=$(/sbin/ifconfig $eth_card | grep 'inet6' | cut -f2 | awk '{ print $2}' | grep -v '^::1$' | grep -v '^fe80' | grep -v '^f[d|c]' | head -1)
fi
else
echo "Error IP index, please input the right type"
exit 0
fi
elif [ $record_type = "A" ];then
if [ $ip_index = "internet" ];then
ip=$(curl -4 ip.sb)
elif [ $ip_index = "local" ];then
if [ "$user" = "root" ];then
ip=$(ifconfig $eth_card | grep 'inet'| grep -v '127.0.0.1' | grep -v 'inet6'|cut -f2 | awk '{ print $2}')
else
ip=$(/sbin/ifconfig $eth_card | grep 'inet'| grep -v '127.0.0.1' | grep -v 'inet6'|cut -f2 | awk '{ print $2}')
fi
else
echo "Error IP index, please input the right type"
exit 0
fi
else
echo "Error DNS type"
exit 0
fi
# 日志 log file
log() {
if [ "$1" ]; then
echo -e "[$(date)] - $1" >> $log_file
fi
}
# SCRIPT START
log "Check Initiated"
#判断ip是否发生变化,check the ip had been changed?
if [ -f $ip_file ]; then
old_ip=$(cat $ip_file)
if [ $ip == $old_ip ]; then
echo "IP has not changed."
exit 0
fi
fi
#获取域名和授权 get the domain and authentic
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 )
record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?type=${record_type}&name=$record_name" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')
echo "$zone_identifier" > $id_file
echo "$record_identifier" >> $id_file
fi
#更新DNS记录 update the 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 "{\"type\":\"$record_type\",\"name\":\"$record_name\",\"content\":\"$ip\",\"ttl\":1,\"proxied\":false}")
#反馈更新情况 gave the feedback about the update statues
if [[ $update == *"\"success\":true"* ]]; then
message="IP changed to: $ip"
echo "$ip" > $ip_file
log "$message"
echo "$message"
else
message="API UPDATE FAILED. DUMPING RESULTS:\n$update"
log "$message"
echo -e "$message"
exit 1
fi
gitextract_58972c6l/ ├── .gitattributes ├── README-EN.md ├── README.md └── cloudflare-ddns.sh
Condensed preview — 4 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (11K chars).
[
{
"path": ".gitattributes",
"chars": 66,
"preview": "# Auto detect text files and perform LF normalization\n* text=auto\n"
},
{
"path": "README-EN.md",
"chars": 3326,
"preview": "# CloudFlare DDNS script/Raspberry pi IPV6 DDNS Solution \n\n### update[ServerChan version](https://github.com/wherelse/cl"
},
{
"path": "README.md",
"chars": 2025,
"preview": "# CloudFlare DDNS script/Raspberry pi IPV6 DDNS Solution \n\n#### cloudflare ddns 脚本/树莓派IPV6 DDNS解决方案\n\n中文 | [English](/REA"
},
{
"path": "cloudflare-ddns.sh",
"chars": 3714,
"preview": "#!/bin/bash\n\n# CHANGE THESE\nauth_email=\"xxxxxxx@xxxx.com\" #你的CloudFlare注册账户邮箱,your cloudflare account email address\naut"
}
]
About this extraction
This page contains the full source code of the wherelse/cloudflare-ddns-script GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 4 files (8.9 KB), approximately 3.0k tokens. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.