公司没有自己的固定公网IP地址,之前采用花生壳DDNS解析进行域名访问。但是花生壳DDNS需要花钱,免费版的使用效果并不是很好,导致DDNS解析老是失败,而且域名不是很好记。后来再阿里云上购买了域名,因为没有固定公网IP,所以每次IP变化就得更改解析记录,非常不现实。后来在阿里云上找到了解决方法,思路就是使用阿里云上的API,使用脚本运行在内网服务器上,每当检测到自己公网IP变化,就调用阿里云API,修改自己域名解析到最新的公网地址上。这样就可以实现动态域名解析

1. 阿里云配置

登陆进入阿里云控制台,配置域名解析
使用脚本更新阿里云DNS记录_第1张图片

使用脚本更新阿里云DNS记录_第2张图片

需要调用API,或OSS存储,则需要申请阿里云Acesskeys
使用脚本更新阿里云DNS记录_第3张图片
按照提示,创建子用户
使用脚本更新阿里云DNS记录_第4张图片

按步骤创建好子用户,我们需要授权给子用户。
使用脚本更新阿里云DNS记录_第5张图片

按照以下方式进行授权
使用脚本更新阿里云DNS记录_第6张图片
然后进入子账户,创建Accesskey,创建好后,记得保存AK信息,不然进入控股台后无法查看AccessKeysecret
使用脚本更新阿里云DNS记录_第7张图片

2内网服务器配置

2.1 AliDDNS脚本

这个脚本是在阿里云社区里面找到的,整体不用太多修改,直接拿来用即可,非常完善

#!/bin/bash

# 更新日志
# Update 20180914 :
# [Fix] 再次修复: CPU 100%占用问题
#       这次找到了AliDDNS卡100%死循环的原因,并且加了一个小的检测机制,理论上应该不会再出现
#       CPU卡100%的问题了(如果有问题请私信脚本作者,谢谢!)

BuildTime="20180914"

# 专家模式开关
# 注意: 只有当你了解整个AliDDNS工作流程,并且有一定的动手能力,希望对AliDDNS脚本的更多参数进行
#       深度定制时,你可以打开这个开关,会提供更多可以设置的选项,但如果你不懂、超级小白,请不要
#       打开这个开关!因打开专家模式后配置失误发生的问题,作者不负任何责任!
#       如需打开专家模式,请将脚本文件中的 Switch_AliDDNS_ExpertMode 变量值设置为1,即可打开
#       专家模式,如需关闭,请将此值设置为0!
Switch_AliDDNS_ExpertMode="0"
echo " "
echo `date`
# ===================================================================================
#
# 下面的代码均为程序的核心代码,请不要触动任何地方的代码,直接运行脚本即可使用!
#
# ===================================================================================

# Shell环境初始化
# 字体颜色定义
Font_Black="\033[30m"  
Font_Red="\033[31m" 
Font_Green="\033[32m"  
Font_Yellow="\033[33m"  
Font_Blue="\033[34m"  
Font_Purple="\033[35m"  
Font_SkyBlue="\033[36m"  
Font_White="\033[37m" 
Font_Suffix="\033[0m"
# 消息提示定义
Msg_Info="${Font_Blue}[Info] ${Font_Suffix}"
Msg_Warning="${Font_Yellow}[Warning] ${Font_Suffix}"
Msg_Error="${Font_Red}[Error] ${Font_Suffix}"
Msg_Success="${Font_Green}[Success] ${Font_Suffix}"
Msg_Fail="${Font_Red}[Failed] ${Font_Suffix}"
# Shell变量开关初始化
Switch_env_is_root="0"
Switch_env_curl_exist="0"
Switch_env_openssl_exist="0"
Switch_env_nslookup_exist="0"
Switch_env_sudo_exist="0"
Switch_env_system_release="none"
# AliDDNS组件-变量初始化
AliDDNS_DomainName=""
AliDDNS_SubDomainName=""
AliDDNS_TTL=""
AliDDNS_AK=""
AliDDNS_SK=""
AliDDNS_LocalIP=""
AliDDNS_DomainServerIP=""
# ServerChan组件-变量初始化
Switch_ServerChan_Enable="0"
ServerChan_SCKEY=""
ServerChan_Text=""
ServerChan_Content=""

# Shell脚本信息显示
#echo -e "${Font_Green}
#=========================================================
# AliDDNS 工具 (阿里云云解析修改工具)
# 
# Build:    ${BuildTime}
# 支持平台:  CentOS/Debian/Ubuntu
# 作者:     iLemonrain (原作者: kyriosli/koolshare-aliddns)
# Blog:     https://blog.ilemonrain.com
# E-mail:   [email protected]
#========================================================

#${Font_suffix}"

# 检查Root权限,并配置开关
function_Check_Root(){
    if [ "`id -u`" != "0" ]; then
        Switch_env_is_root="0"
    else
        Switch_env_is_root="1"
    fi
}

function_Check_Enviroment(){
    if [ -f "/usr/bin/curl" ]; then
        Switch_env_curl_exist="1"
    else
        Switch_env_curl_exist="0"
    fi
    if [ -f "/usr/bin/openssl" ]; then
        Switch_env_openssl_exist="1"
    else
        Switch_env_openssl_exist="0"
    fi
    if [ -f "/usr/bin/nslookup" ]; then
        Switch_env_nslookup_exist="1"
    else
        Switch_env_nslookup_exist="0"
    fi
    if [ -f "/usr/bin/sudo" ]; then
        Switch_env_sudo_exist="1"
    else
        Switch_env_sudo_exist="0"
    fi
    if [ -f "/etc/redhat-release" ]; then
        Switch_env_system_release="centos"
    elif [ -f "/etc/lsb-release" ]; then
        Switch_env_system_release="ubuntu"
    elif [ -f "/etc/debian_version" ]; then
        Switch_env_system_release="debian"
    else
        Switch_env_system_release="unknown"
    fi
}

function_Install_Enviroment(){
    if [ "${Switch_env_curl_exist}" = "0" ] || [ "${Switch_env_openssl_exist}" = "0" ] || [ "${Switch_env_nslookup_exist}" = "0" ]; then
        echo -e "${Msg_Warning}未检查到必需组件或者组件不完整,正在尝试安装……"
        if [ "${Switch_env_is_root}" = "1" ]; then
            if [ "${Switch_env_system_release}" = "centos" ]; then
                echo -e "${Msg_Info}检测到系统分支:CentOS"
                echo -e "${Msg_Info}正在安装必需组件……"
                yum install curl bind-utils openssl -y
            elif [ "${Switch_env_system_release}" = "ubuntu" ]; then
                echo -e "${Msg_Info}检测到系统分支:Ubuntu"
                echo -e "${Msg_Info}正在安装必需组件……"
                apt-get install curl dnsutils openssl -y
            elif [ "${Switch_env_system_release}" = "debian" ]; then
                echo -e "${Msg_Info}检测到系统分支:Debian"
                echo -e "${Msg_Info}正在安装必需组件……"
                apt-get install curl dnsutils openssl -y
            else
                echo -e "${Msg_Warning}系统分支未知,取消环境安装,建议手动安装环境!"
            fi
            if [ -f "/usr/bin/curl" ]; then
                Switch_env_curl_exist="1"
            else
                Switch_env_curl_exist="0"
                echo -e "${Msg_Error}curl组件安装失败!可能会影响到程序运行!建议手动安装!"
            fi
            if [ -f "/usr/bin/openssl" ]; then
                Switch_env_openssl_exist="1"
            else
                Switch_env_openssl_exist="0"
                echo -e "${Msg_Error}openssl组件安装失败!可能会影响到程序运行!建议手动安装!"
            fi
            if [ -f "/usr/bin/nslookup" ]; then
                Switch_env_nslookup_exist="1"
            else
                Switch_env_nslookup_exist="0"
                echo -e "${Msg_Error}nslookup组件安装失败!可能会影响到程序运行!建议手动安装!"
            fi
        elif [ -f "/usr/bin/sudo" ]; then
            echo -e "${Msg_Warning}检测到当前脚本并非以root权限启动,正在尝试通过sudo命令安装……"
            if [ "${Switch_env_system_release}" = "centos" ]; then
                echo -e "${Msg_Info}检测到系统分支:CentOS"
                echo -e "${Msg_Info}正在安装必需组件 (使用sudo)……"
                sudo yum install curl bind-utils -y
            elif [ "${Switch_env_system_release}" = "ubuntu" ]; then
                echo -e "${Msg_Info}检测到系统分支:Ubuntu"
                echo -e "${Msg_Info}正在安装必需组件 (使用sudo)……"
                sudo apt-get install curl dnsutils -y
            elif [ "${Switch_env_system_release}" = "debian" ]; then
                echo -e "${Msg_Info}检测到系统分支:Debian"
                echo -e "${Msg_Info}正在安装必需组件 (使用sudo)……"
                sudo apt-get install curl dnsutils -y
            else
                echo -e "${Msg_Warning}系统分支未知,取消环境安装,建议手动安装环境!"
            fi
        else
            echo -e "${Msg_Error}系统缺少必需环境,并且无法自动安装,建议手动安装!"
        fi
    fi
}

# 判断是否有已存在的配置文件 (是否已经配置过环境)
function_AliDDNS_CheckConfig(){
    if [ -f "/etc/OneKeyAliDDNS/config.cfg" ]; then
        echo -e "${Msg_Info}检测到存在的配置,自动读取现有配置\n       如果你不需要,请通过菜单中的清理环境选项进行清除"
        # 读取配置文件
        AliDDNS_DomainName=`sed '/^AliDDNS_DomainName=/!d;s/.*=//' /etc/OneKeyAliDDNS/config.cfg | sed 's/\"//g'`
        AliDDNS_SubDomainName=`sed '/^AliDDNS_SubDomainName=/!d;s/.*=//' /etc/OneKeyAliDDNS/config.cfg | sed 's/\"//g'`
        AliDDNS_TTL=`sed '/^AliDDNS_TTL=/!d;s/.*=//' /etc/OneKeyAliDDNS/config.cfg | sed 's/\"//g'`
        AliDDNS_AK=`sed '/^AliDDNS_AK=/!d;s/.*=//' /etc/OneKeyAliDDNS/config.cfg | sed 's/\"//g'`
        AliDDNS_SK=`sed '/^AliDDNS_SK=/!d;s/.*=//' /etc/OneKeyAliDDNS/config.cfg | sed 's/\"//g'`
        AliDDNS_LocalIP=`sed '/^AliDDNS_LocalIP=/!d;s/.*=//' /etc/OneKeyAliDDNS/config.cfg | sed 's/\"//g'`
        AliDDNS_DomainServerIP=`sed '/^AliDDNS_DomainServerIP=/!d;s/.*=//' /etc/OneKeyAliDDNS/config.cfg | sed 's/\"//g'`
        if [ "${AliDDNS_DomainName}" = "" ] || [ "${AliDDNS_SubDomainName}" = 0 ] || [ "${AliDDNS_TTL}" = "" ] \
                || [ "${AliDDNS_AK}" = "" ] || [ "${AliDDNS_SK}" = "" ] || [ "${AliDDNS_LocalIP}" = "" ] \
                || [ "${AliDDNS_DomainServerIP}" = "" ]; then
            echo -e "${Msg_Error}配置文件有误,请检查配置文件,或者建议清理环境后重新配置 !"
            exit 1
        fi
        Switch_AliDDNS_Config_Exist="1"
    else
        Switch_AliDDNS_Config_Exist="0"
    fi
}

function_AliDDNS_SetConfig(){
    # AliDDNS_DomainName
    if [ "${AliDDNS_DomainName}" = "" ]; then
        echo -e "\n${Msg_Info}请输入一级域名 (比如 example.com)"
        read -p "(此项必须填写,查看帮助请输入“h”):" AliDDNS_DomainName
        [ "${AliDDNS_DomainName}" = "h" ] && function_document_AliDDNS_DomainName && echo -e "${Msg_Info}请输入一级域名 (比如 example.com)" && read -p "(此项必须填写,查看提示请输入 "h"):" AliDDNS_DomainName
        while [ -z "${AliDDNS_DomainName}" ]
        do
            echo -e "${Msg_Error}此项不可为空,请重新填写"
            echo -e "${Msg_Info}请输入一级域名 (比如 example.com)"
            read -p "(此项必须填写,查看帮助请输入“h”):" AliDDNS_DomainName
        done
    fi
    # AliDDNS_SubDomainName
    if [ "${AliDDNS_SubDomainName}" = "" ]; then
        echo -e "\n${Msg_Info}请输入二级域名 (比如 ddns)"
        read -p "(此项必须填写,查看帮助请输入“h”):" AliDDNS_SubDomainName
        [ "${AliDDNS_SubDomainName}" = "h" ] && function_document_AliDDNS_SubDomainName && echo -e "${Msg_Info}请输入二级域名 (比如 ddns)" && read -p "(此项必须填写,查看帮助请输入“h”):" AliDDNS_SubDomainName
        while [ -z "${AliDDNS_SubDomainName}" ]
        do
            echo -e "${Msg_Error}此项不可为空,请重新填写"
            echo -e "${Msg_Info}请输入二级域名 (比如 ddns)"
            read -p "(此项必须填写,查看帮助请输入“h”):" AliDDNS_SubDomainName
        done
    fi
    # AliDDNS_TTL
    if [ "${AliDDNS_TTL}" = "" ]; then
        echo -e "\n${Msg_Info}请输入记录的TTL(Time-To-Live)值:"
        read -p "(默认为600,查看帮助请输入“h”):" AliDDNS_TTL
        [ "${AliDDNS_TTL}" = "h" ] && function_document_AliDDNS_TTL && echo -e "${Msg_Info}请输入记录的TTL(Time-To-Live)值:" && read -p "(默认为600):" AliDDNS_TTL
        [ -z "${AliDDNS_TTL}" ] && echo -e "${Msg_Info}检测到输入空值,设置AliDDNS_TTL值为:“600”" && AliDDNS_TTL="600"
    fi
    # AliDDNS_AK
    if [ "${AliDDNS_AK}" = "" ]; then
        echo -e "\n${Msg_Info}请输入阿里云AccessKey ID"
        read -p "(此项必须填写,查看帮助请输入“h”):" AliDDNS_AK
        [ "${AliDDNS_AK}" = "h" ] && function_document_AliDDNS_AK && echo -e "${Msg_Info}请输入阿里云AccessKey ID" && read -p "(此项必须填写,查看帮助请输入“h”):" AliDDNS_AK
        while [ -z "${AliDDNS_AK}" ]
        do
            echo -e "${Msg_Error}此项不可为空,请重新填写"
            echo -e "${Msg_Info}请输入阿里云AccessKey ID"
            read -p "(此项必须填写,查看帮助请输入“h”):" AliDDNS_AK
        done
    fi
    # AliDDNS_SK
    if [ "${AliDDNS_SK}" = "" ]; then
        echo -e "\n${Msg_Info}请输入阿里云Access Key Secret"
        read -p "(此项必须填写,查看帮助请输入“h”):" AliDDNS_SK
        [ "${AliDDNS_SK}" = "h" ] && function_document_AliDDNS_SK && echo -e "${Msg_Info}请输入阿里云Access Key Secret" && read -p "(默认为600):" AliDDNS_SK
        while [ -z "${AliDDNS_SK}" ]
        do
            echo -e "${Msg_Error}此项不可为空,请重新填写"
            echo -e "${Msg_Info}请输入阿里云Access Key Secret"
            read -p "(此项必须填写,查看帮助请输入“h”):" AliDDNS_SK
        done
    fi
    # AliDDNS_LocalIP
    if [ "${Switch_AliDDNS_ExpertMode}" = "1" ]; then
        if [ "${AliDDNS_LocalIP}" = "" ]; then
            echo -e "\n${Msg_Info}请输入获取本机IP使用的命令"
            read -p "(查看帮助请输入“h”):" AliDDNS_LocalIP
            [ "${AliDDNS_LocalIP}" = "h" ] && function_document_AliDDNS_LocalIP && echo -e "${Msg_Info}请输入获取本机IP使用的命令" && read -p "(查看帮助请输入“h”):" AliDDNS_LocalIP
            [ -z "${AliDDNS_LocalIP}" ] && echo -e "${Msg_Info}检测到输入空值,设置执行命令为:“curl -s whatismyip.akamai.com”" && AliDDNS_LocalIP="curl -s whatismyip.akamai.com"
        fi
    else
        AliDDNS_LocalIP="curl -s whatismyip.akamai.com"
    fi
    # AliDDNS_DomainServerIP
    if [ "${Switch_AliDDNS_ExpertMode}" = "1" ]; then
        if [ "${AliDDNS_DomainServerIP}" = "" ]; then
            echo -e "\n${Msg_Info}请输入解析使用的DNS服务器"
            read -p "(此项必须填写,查看帮助请输入“h”):" AliDDNS_DomainServerIP
            [ "${AliDDNS_DomainServerIP}" = "h" ] && function_document_AliDDNS_DomainServerIP && echo -e "${Msg_Info}请输入解析使用的DNS服务器" && read -p "(此项必须填写,查看帮助请输入“h”):" AliDDNS_DomainServerIP
            [ -z "${AliDDNS_DomainServerIP}" ] && echo -e "${Msg_Info}检测到输入空值,设置默认DNS服务器为:“223.5.5.5”" && AliDDNS_DomainServerIP="223.5.5.5"
        fi
    else
        AliDDNS_DomainServerIP="223.5.5.5"
    fi
}

function_AliDDNS_WriteConfig(){
    # 写入配置文件
    echo -e "\n${Msg_Info}正在写入配置文件……"
    if [ "${Switch_env_is_root}" = "1" ]; then 
        Config_configdir="/etc/OneKeyAliDDNS/"
    else
        Config_configdir="~/OneKeyAliDDNS/"
    fi
    mkdir -p ${Config_configdir}
    rm -f ${Config_configdir}config.cfg
    cat>${Config_configdir}config.cfg<&1`
    if [ "${AliDDNS_LocalIP}" = "" ]; then
        echo -e "${Msg_Error}未能获取本机IP!"
        echo -e "${Msg_Fail}程序运行出现致命错误,正在退出……"
        exit 1
    else
        echo -e "${Msg_Info}本机IP:${AliDDNS_LocalIP}"
    fi
}

# 新版获取域名IP的方法,使用腾讯云的HttpDNS
#
#function_AliDDNS_DomainIP(){
#    echo -e "${Msg_Info}正在获取 $AliDDNS_SubDomainName.$AliDDNS_DomainName 的IP……"
#    AliDDNS_DomainIP=`curl -s http://119.29.29.29/d?dn=$AliDDNS_SubDomainName.$AliDDNS_DomainName`
#    if [ "$?" -eq "0" ]; then
#        # 如果执行成功,分离出结果中的IP地址
#        if [ "${AliDDNS_DomainIP}" = "" ]; then
#            echo -e "${Msg_Info}解析结果:$AliDDNS_SubDomainName.$AliDDNS_DomainName -> (结果为空)"
#            echo -e "${Msg_Warning}$AliDDNS_SubDomainName.$AliDDNS_DomainName 未检测到任何有效的解析记录,可能是DNS记录不存在或尚未生效"
#            echo -e "${Msg_Info}程序可能会报告InvalidParameter异常错误,如出现此错误,请前往阿里云云解析面板手动添加一条任意记录值的A解析记录即可!"
#        else
#            echo -e "${Msg_Info}解析结果:$AliDDNS_SubDomainName.$AliDDNS_DomainName -> $AliDDNS_DomainIP"
#        fi
#        # 进行判断,如果本次获取的新IP和旧IP相同,结束程序运行
#        if [ "$AliDDNS_LocalIP" = "$AliDDNS_DomainIP" ]
#        then
#            echo -e "${Msg_Info}当前IP ($AliDDNS_LocalIP) 与 $AliDDNS_SubDomainName.$AliDDNS_DomainName ($AliDDNS_DomainIP) 的IP相同"
#            echo -e "${Msg_Success}未发生任何变动,无需进行改动,正在退出……"
#            exit 0
#        fi 
#    fi
#}    

# 旧版获取域名IP的方法,如果新版方法发生异常,请删掉新版代码,取消旧版的注释,保存即可!

# 获取DDNS域名当前解析记录IP
function_AliDDNS_DomainIP(){
    echo -e "${Msg_Info}正在获取 $AliDDNS_SubDomainName.$AliDDNS_DomainName 的IP……"
    AliDDNS_DomainIP=`nslookup $AliDDNS_SubDomainName.$AliDDNS_DomainName $AliDDNS_DomainServerIP 2>&1`
    if [ "$?" -eq "0" ]; then
        # 如果执行成功,分离出结果中的IP地址
        AliDDNS_DomainIP=`echo "$AliDDNS_DomainIP" | grep 'Address:' | tail -n1 | awk '{print $NF}'`
        if [ "${AliDDNS_DomainIP}" = "" ]; then
            echo -e "${Msg_Info}解析结果:$AliDDNS_SubDomainName.$AliDDNS_DomainName -> (结果为空)"
            echo -e "${Msg_Info}$AliDDNS_SubDomainName.$AliDDNS_DomainName 未检测到任何有效的解析记录,可能是DNS记录不存在或尚未生效"
        else
            echo -e "${Msg_Info}解析结果:$AliDDNS_SubDomainName.$AliDDNS_DomainName -> $AliDDNS_DomainIP"
        fi
    # 进行判断,如果本次获取的新IP和旧IP相同,结束程序运行
        if [ "$AliDDNS_LocalIP" = "$AliDDNS_DomainIP" ]
        then
            echo -e "${Msg_Info}当前IP ($AliDDNS_LocalIP) 与 $AliDDNS_SubDomainName.$AliDDNS_DomainName ($AliDDNS_DomainIP) 的IP相同"
            echo -e "${Msg_Success}未发生任何变动,无需进行改动,正在退出……"
            exit 0
        fi 
    fi
}

function_AliDDNS_GetTimestamp(){
    echo -e "${Msg_Info}正在生成时间戳……"
    timestamp=`date -u "+%Y-%m-%dT%H%%3A%M%%3A%SZ"`
}

urlencode() {
    # urlencode 
    out=""
    while read -n1 c
    do
        case $c in
            [a-zA-Z0-9._-]) out="$out$c" ;;
            *) out="$out`printf '%%%02X' "'$c"`" ;;
        esac
    done
    echo -n $out
}
# URL加密命令
enc() {
    echo -n "$1" | urlencode
}
# 发送请求函数
send_request() {
    local args="AccessKeyId=$AliDDNS_AK&Action=$1&Format=json&$2&Version=2015-01-09"
    local hash=$(echo -n "GET&%2F&$(enc "$args")" | openssl dgst -sha1 -hmac "$AliDDNS_SK&" -binary | openssl base64)
    curl -s "http://alidns.aliyuncs.com/?$args&Signature=$(enc "$hash")"
}

# 获取记录值 (RecordID)
get_recordid() {
    grep -Eo '"RecordId":"[0-9]+"' | cut -d':' -f2 | tr -d '"'
}

# 请求记录值 (RecordID)
query_recordid() {
    send_request "DescribeSubDomainRecords" "SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&SubDomain=$AliDDNS_SubDomainName.$AliDDNS_DomainName&Timestamp=$timestamp"
}
# 更新记录值 (RecordID)
update_record() {
    send_request "UpdateDomainRecord" "RR=$AliDDNS_SubDomainName&RecordId=$1&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=$AliDDNS_TTL&Timestamp=$timestamp&Type=A&Value=$AliDDNS_LocalIP"
}
# (Update 20180703 Bugfix : 屏蔽add_record,避免发生记录重复冲突问题,感谢 mylyne 反馈)
# 添加记录值 (RecordID)
# add_record() {
#    send_request "AddDomainRecord" "RR=$AliDDNS_SubDomainName&DomainName=$AliDDNS_DomainName&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=$AliDDNS_TTL&Timestamp=$timestamp&Type=A&Value=$AliDDNS_LocalIP"
# }

# RecordID更新
function_AliDDNS_UpdateRecord(){
    echo -e "${Msg_Info}正在更新记录……"
    if [ "${AliDDNS_RecordID}" = "" ]; then
        echo -e "${Msg_Info}正在获取RecordID……"
        AliDDNS_RecordID=`query_recordid | get_recordid`
        if [ "${AliDDNS_RecordID}" = "" ]; then
            echo -e "${Msg_Warning}未能获取到RecordID,可能没有检测到有效的解析记录 (RecordID:$AliDDNS_RecordID)"
        else
            echo -e "${Msg_Info}RecordID -> $AliDDNS_RecordID"
            echo -e "${Msg_Info}正在更新解析记录:$AliDDNS_SubDomainName.$AliDDNS_DomainName -> $AliDDNS_LocalIP ……"
            update_record $AliDDNS_RecordID
            echo -e "\n${Msg_Info}已经更新RecordID:$AliDDNS_RecordID"
        fi
    fi
    if [ "${AliDDNS_RecordID}" = "" ]; then
        # 输出失败结果 (因为没有获取到RecordID)
        echo -e "${Msg_Fail}DDNS记录更新失败!"
        exit 1
    else
        # 输出成功结果
        echo -e "${Msg_Success}DDNS记录更新成功,新的IP为:$AliDDNS_LocalIP"
        # ServerChan推送组件
        function_ServerChan_SuccessMsgPush
        exit 0
    fi
}

function_AliDDNS_CleanEnviroment(){
    rm -f /etc/OneKeyAliDDNS/config.cfg
    rm -f ~/OneKeyAliDDNS/config.cfg
    rm -f /etc/OneKeyAliDDNS/config-ServerChan.cfg
    rm -f ~/OneKeyAliDDNS/config-ServerChan.cfg
    Switch_env_is_root="0"
    AliDDNS_DomainName=""
    AliDDNS_SubDomainName=""
    AliDDNS_TTL=""
    AliDDNS_AK=""
    AliDDNS_SK=""
    AliDDNS_LocalIP=""
    AliDDNS_DomainServerIP=""
    Switch_ServerChan_Enable="0"
    ServerChan_SCKEY=""
    ServerChan_Text=""
    ServerChan_Content=""
}

function_AliDDNS_ShowVersion(){
    echo -e "
# AliDDNS 工具 (阿里云云解析修改工具)
# 
# Build:    ${BuildTime}
# 支持平台:  CentOS/Debian/Ubuntu
# 作者:     iLemonrain (原作者: kyriosli/koolshare-aliddns)
# Blog:     https://blog.ilemonrain.com
# E-mail:   [email protected]
"
exit 0
}

function_ServerChan_Configure(){
    if [ "${ServerChan_SCKEY}" = "" ]; then
        echo -e "\n${Msg_Info}请输入ServerChan SCKEY:"
        read -p "(此项必须填写):" ServerChan_SCKEY
        [ "${ServerChan_SCKEY}" = "h" ] && function_document_ServerChan_SCKEY && echo -e "${Msg_Info}请输入ServerChan SCKEY:" && read -p "(此项必须填写,查看提示请输入 "h"):" ServerChan_SCKEY
        while [ -z "${ServerChan_SCKEY}" ]
        do
            echo -e "${Msg_Error}此项不可为空,请重新填写"
            echo -e "${Msg_Info}请输入ServerChan SCKEY:"
            read -p "(此项必须填写):" ServerChan_SCKEY
        done
    fi
        if [ "${ServerChan_ServerFriendlyName}" = "" ]; then
        echo -e "\n${Msg_Info}请输入服务器名称:请使用中文/英文,不要使用除了英文下划线以外任何符号"
        read -p "(此项必须填写,便于识别):" ServerChan_ServerFriendlyName
        [ "${ServerChan_ServerFriendlyName}" = "h" ] && function_document_ServerChan_ServerFriendlyName && echo -e "${Msg_Info}请输入服务器名称:请使用中文/英文,不要使用除了英文下划线以外任何符号" && read -p "(此项必须填写,便于识别):" ServerChan_ServerFriendlyName
        while [ -z "${ServerChan_ServerFriendlyName}" ]
        do
            echo -e "${Msg_Error}此项不可为空,请重新填写"
            echo -e "${Msg_Info}请输入服务器名称:请使用中文/英文,不要使用除了英文下划线以外任何符号"
            read -p "(此项必须填写,便于识别):" ServerChan_ServerFriendlyName
        done
    fi
}

function_ServerChan_CheckConfig(){
    if [ -f "/etc/OneKeyAliDDNS/config-ServerChan.cfg" ]; then
        Switch_ServerChan_ConfigExist="1"
    else
        Switch_ServerChan_ConfigExist="0"
    fi
}

function_ServerChan_ReadConfig(){
    if [ -f "/etc/OneKeyAliDDNS/config-ServerChan.cfg" ]; then
        # 读取配置文件
        Switch_ServerChan_Enable=`sed '/^Switch_ServerChan_Enable=/!d;s/.*=//' /etc/OneKeyAliDDNS/config-ServerChan.cfg | sed 's/\"//g'`
        ServerChan_ServerFriendlyName=`sed '/^ServerChan_ServerFriendlyName=/!d;s/.*=//' /etc/OneKeyAliDDNS/config-ServerChan.cfg | sed 's/\"//g'`
        ServerChan_SCKEY=`sed '/^ServerChan_SCKEY=/!d;s/.*=//' /etc/OneKeyAliDDNS/config-ServerChan.cfg | sed 's/\"//g'`
        # 开关变量设1
        Switch_ServerChan_ConfigExist="1"
    else
        Switch_ServerChan_ConfigExist="0"
    fi
}

function_ServerChan_WriteConfig(){
    # 写入配置文件
    echo -e "\n${Msg_Info}正在写入配置文件……"
    if [ "${Switch_env_is_root}" = "1" ]; then 
        Config_configdir="/etc/OneKeyAliDDNS/"
    else
        Config_configdir="~/OneKeyAliDDNS/"
    fi
    mkdir -p ${Config_configdir}
    rm -f ${Config_configdir}config-ServerChan.cfg
    cat>${Config_configdir}config-ServerChan.cfg<

将此脚本保存为aliddns.sh 放在内网服务器上即可

2.2 服务器配置

内网服务器是centos7,需要安装一些软件包配置

yum install -y wget curl cronie

将脚本保存名为aliddns.sh,路径为/usr/sbin/;然后添加可执行权限
chmod +x /usr/sbin/aliddns.sh

然后运行脚本即可
使用脚本更新阿里云DNS记录_第8张图片
输入1回车,然后输入一级域名,如cctv.com,即在阿里云上申请的域名
使用脚本更新阿里云DNS记录
然后按照提示逐步输入
使用脚本更新阿里云DNS记录_第9张图片
其他配置完成后,脚本会自动检测环境。

2.3计划任务

脚本配置完毕后,增加计划任务,定时执行

*/5 * * * * /usr/sbin/AliDDNS-v2.0.sh run >/dev/null 2>&1 &

然后开启计划任务即可