利用CloudFlare API,在NAT VPS中自建DDNS

date
Jul 5, 2019
slug
create-ddns-in-nat-vps-by-cloudflare
status
Published
tags
折腾
summary
DDNS 是通过域名绑定动态 IP 的方式,这里通过 CloudFlare API 将机器变更后的 IP 进行动态替换
type
Post

前言

首先必须要有自己的域名,另外就是使用阿里云解析、DNSPOD云解析、Cloudflare云解析等服务,教程以Cloudflare云解析为例子。

获取CFKEY

在页面下方找到【Global API Key】,点击右侧的View查看Key,并保存下来

设置用来DDNS解析的二级域名

以Nathosts为例,先在控制面板找到目前母鸡的IP,然后到Cloudflare中新建一个A记录,如:ddns.yourdomain.com,指向母鸡当前的IP

下载脚本

wget <https://raw.githubusercontent.com/yulewang/cloudflare-api-v4-ddns/master/cf-v4-ddns.sh>

填写相关信息

# API key, see <https://www.cloudflare.com/a/account/my-account>,
# 上一步获取的CFKEY
CFKEY=

#输入你需要解析用来DDNS解析的根域名 eg: example.com
CFZONE=

# 暂时空着
CFID=

# 登陆CF的Username, eg: user@example.com
CFUSER=

# 填写用来DDNS解析的二级域名,与上面设置的要一致, eg: ddns.yourdomain.com
CFHOST=

# Cloudflare TTL for record, between 120 and 86400 seconds
CFTTL=3600
# Get domain ID from Cloudflare using awk/sed and python json.tool
GETID=true
# Ignore local file, update ip anyway
FORCE=false
# 填写上一步能正确返回母鸡IP的网址, other examples are: bot.whatismyipaddress.com, <https://api.ipify.org/> ...
WANIPSITE="myip.dnsomatic.com"

运行脚本

chmod +x cf-ddns.sh
./cf-ddns.sh

设置定时任务

crontab -e
添加如下代码
*/5 * * * * bash /root/cf-ddns.sh >/dev/null 2>&1
教程综合: 1、https://www.mrkevin.net/code/1444.html 2、https://zhujiwiki.com/5001/

© Krist 2016 - 2024

|