欢迎光临
我们一直在努力

为动态ip部署固定域名的方法

现在动态IP以及NAT VPS主机越来越受欢迎,一方面满足了业务的需求,但另一方面也带来一个问题,就是IP不固定不好开展业务。比如建个站指向好了,下一刻主机却IP变了,此时便要去更新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分钟执行一次检测。

赞(0)
未经允许不得转载:站长电台 » 为动态ip部署固定域名的方法
分享到: 更多 (0)

评论 2

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    I just want to say I am just very new to blogs and really liked your web page. Likely I’m going to bookmark your site . You definitely come with terrific articles and reviews. Kudos for sharing with us your website page.

  2. #2

    Thanks a ton for your efforts to have decided to put these things together on this site. Janet and i also very much liked your suggestions through the articles with certain things. I understand that you have a number of demands on your program therefore the fact that an individual like you took just as much time just like you did to steer people like us through this article is even highly prized.