如何为动态 IP|NAT VPS 部署固定域名

cera cera

现在动态 IP 以及 NAT VPS 主机越来越受欢迎,一方面满足了业务的需求,但另一方面也带来一个问题,就是 IP 不固定不好开展业务。比如建个站指向好了,下一刻主机却 IP 变了,此时便要去更新 DNS,更新了过一段时间发现 IP 又变了,又去更新 DNS,如此反复,十分累人。本文将探讨当 IP 发生变动时如何自动更新域名指定 IP。

目前的思路是 VPS 主机自动检测外网 IP 变动,当变了时自动去更新 DNS 记录。在这思路下,方案是可以通过运行脚本检测,并通过 DNS API 触发更新,支持 API 更新的有 CloudFlare、阿里云解析等 DNS 托管。以下以 CloudFlare 为例说明。

脚本说明

直接放上脚本:

#!/bin/bash

# 根据实际修改以下字段值
auth_email="user@example.com"    # CloudFlare 注册邮箱
auth_key="c2547eb745079dac9320b638f5e225cf483cc5cfdda41"   # API Key
zone_name="example.com"   # 要做指向的根域名
record_name="www.example.com"   # 要做指向的记录

# 根据需要修改
ip=$(curl -s http://ipv4.icanhazip.com)   # 获取外网 IP
ip_file="ip.txt"  # IP 自动检测记录
id_file="cloudflare.ids"  # CloudFlare 验证文件
log_file="cloudflare.log"  # CloudFlare API 执行日志

# 日志格式
log() {
    if [ "$1" ]; then
        echo -e "[$(date)] - $1" >> $log_file
    fi
}

# 检测
log "Check Initiated"

if [ -f $ip_file ]; then
    old_ip=$(cat $ip_file)
    if [ $ip == $old_ip ]; then
        echo "IP has not changed."
        exit 0
    fi
fi

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?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

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 "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}")

if [[ $update == *"\"success\":false"* ]]; then
    message="API UPDATE FAILED. DUMPING RESULTS:\n$update"
    log "$message"
    echo -e "$message"
    exit 1 
else
    message="IP changed to: $ip"
    echo "$ip" > $ip_file
    log "$message"
    echo "$message"
fi

直接下载脚本

# wget --no-check-certificate https://zhujiwiki.comhttps://tc-gz-1252597704.cosgz.myqcloud.com/uploads/2018/07/CloudFlareDDNS/cloudflare-update-record.sh > /root/ddns.sh

定期执行

比如上述脚本保存到 /root/ddns.sh,先增加执行权限

# chmod +x /root/ddns.sh   

手动单次执行:

# bash ddns.sh

利用 Crontab 任务定期指定:

# crontab -e

键盘上敲 i 进入编辑,添加如下字段:

*/5 * * * * bash /root/ddns.sh

这段代码的意思是每 5 分钟执行一次检测。

cera cloudiplc tengxunyun

相关推荐

mjjping.com cera cera cloudiplc