cloudflare 这个域名服务商它提供了 域名更新的api,所以可以使用脚本来更新域名,如果是动态更新会更好。
把这个如果加入cron计划任务那就更完美了
#!/bin/bash
Green_font="\033[32m" && Red_font="\033[31m" && Font_suffix="\033[0m"
Info="${Green_font}[Info]${Font_suffix}"
Error="${Red_font}[Error]${Font_suffix}"
echo -e "${Green_font}
#=======================================
# Project: auto_update_cloudflare_ddns
# Version: 1.0
# Author: sinoll
# blog: sinoll.com
# document: https://api.cloudflare.com/#zone-properties
#=======================================
${Font_suffix}"
check_root(){
[[ "`id -u`" != "0" ]] && echo -e "${Error} must be root user !" && exit 1
}
directory(){
[[ ! -d /root/ddns_auto_update ]] && echo -e "${Error} This script must be executed under the directory '/root/ddns_auto_update', please check !" && exit 1
cd /root/ddns_auto_update
}
define(){
[[ ! -f config.conf ]] && echo -e "${Error} can not found config.conf file, please check !" && exit 1
email=`cat config.conf | grep "email" | awk -F "=" '{print $NF}'`
zone_id=`cat config.conf | grep "zone_id" | awk -F "=" '{print $NF}'`
api_key=`cat config.conf | grep "api_key" | awk -F "=" '{print $NF}'`
type=`cat config.conf | grep "type" | awk -F "=" '{print $NF}'`
record_id=`cat config.conf | grep "record_id" | awk -F "=" '{print $NF}'`
domain=`cat config.conf | grep "domain" | awk -F "=" '{print $NF}'`
ttl=`cat config.conf | grep "ttl" | awk -F "=" '{print $NF}'`
echo -e "${Info} get the curent ipadress... "
dynamic_ip=`curl getip.sinoll.com`
}
read_oldip(){
[[ ! -f oldip.txt ]] && echo -e "${Error} can not found oldip file, please check !" && exit 1
oldip=`cat oldip.txt | grep "ip" | awk -F "=" '{print $NF}'`
}
updateip(){
if [[ "${oldip}" == "${dynamic_ip}" ]]; then
echo -e "${Info} the ipadress is not changed, so not update_record!" && exit 1
else
echo -e "${Info} update the ddns ipadress now ..."
res=$(update_record)
echo -e "${res}" >result.txt
if [[ "${res}" == *"\"success\":true"* ]]; then
echo -e "${Info} the ddns ip updated!"
echo "ip=${dynamic_ip}" > oldip.txt
exit 1
fi
fi
}
update_record(){
curl -X PUT "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records/${record_id}" \
-H "X-Auth-Email: ${email}" \
-H "X-Auth-Key: ${api_key}" \
-H "Content-Type: application/json" \
--data '{"type":"'${type}'", "name":"'${domain}'", "content":"'${dynamic_ip}'", "ttl":'${ttl}', "proxied":false}'
}
check_root
directory
define
read_oldip
updateipconfig.conf文件大概内容
email=你登录cloudflare的邮箱地址
zone_id=在cloudflare上查找
api_key=在cloudflare上查找
type=A
record_id=初次运行脚本时在生成的result.txt文件中查找
domain=你的域名
ttl=120 最小2分钟
参考了南琴浪的脚本
https://github.com/nanqinlang-script/CloudFlare_DNS_Record