1. 程式人生 > 實用技巧 >利用CloudFlare自動DDNS

利用CloudFlare自動DDNS

注意要 僅限 DNS

獲取咱的Keyhttps://dash.cloudflare.com/profile

先在控制面板找到咱的目前IP,然後到Cloudflare中新建一個A記錄,如:ddns.yourdomain.com,指向咱的當前IP

下載指令碼

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

這裡需要填寫一個能返回咱的當前IP的網頁地址,下面提供一些

 $ curl ifconfig.me
$ curl icanhazip.com
$ curl ident.me
$ curl ipecho.net/plain
$ curl whatismyip.akamai.com
$ curl tnx.nl/ip
$ curl myip.dnsomatic.com
$ curl ip.appspot.com
$ curl -s checkip.dyndns.org | sed 's/.*IP Address: \([0-9\.]*\).*/\1/g'

修改指令碼

# API key, see https://www.cloudflare.com/a/account/my-account,
# 上一步獲取的CFKEY
CFKEY=
#輸入你需要解析用來DDNS解析的根域名 eg: example.com
CFZONE=
# 暫時空著
CFID=
# 登陸CF的Username, eg: [email protected]
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

第一次執行後,會顯示咱用於DDNS解析的二級域名的CFID,記錄下來

將CFID填入到配置檔案中的CFID處

再次執行

 ./cf-ddns.sh

設定自動執行

 crontab -e
#不知道這樣寫對不對xD
0 * * * * /root/cf-ddns.sh >/dev/null 2>&1