天天看點

更新網站動态IP記錄

#!/bin/sh
   
#################################################
# AnripDdns v3.08.09
# 基于DNSPod使用者API實作的動态域名用戶端
# 作者: 若海[[email protected]]
# 介紹: http://www.anrip.com/ddnspod
# 時間: 2013-08-08 23:25:00
#################################################
   
# 全局變量表
arPass=arMail=""
   
# 獲得外網位址
arIpAdress() {
    local inter="http://members.3322.org/dyndns/getip"
    wget --quiet --no-check-certificate --output-document=- $inter
}
   
# 查詢域名位址
# 參數: 待查詢域名
arNslookup() {
    local dnsvr="114.114.114.114"
    nslookup ${1} $dnsvr | tr -d '\n[:blank:]' | sed 's/.\+1 \([0-9\.]\+\)/\1/'
}
   
# 讀取接口資料
# 參數: 接口類型 待送出資料
arApiPost() {
    local agent="AnripDdns/3.08([email protected])"
    local inter="https://dnsapi.cn/${1:?'Info.Version'}"
    local param="login_email=${arMail}&login_password=${arPass}&format=json&${2}&lang=cn"
    wget --quiet --no-check-certificate --output-document=- --user-agent=$agent --post-data $param $inter
}
   
# 更新記錄資訊
# 參數: 主域名 子域名
arDdnsUpdate() {
    local domainID recordID recordRS recordCD
    # 獲得域名ID
    domainID=$(arApiPost "Domain.Info" "domain=${1}")
    domainID=$(echo $domainID | sed 's/.\+{"id":"\([0-9]\+\)".\+/\1/')
    # 獲得記錄ID
    recordID=$(arApiPost "Record.List" "domain_id=${domainID}&sub_domain=${2}")
    recordID=$(echo $recordID | sed 's/.\+\[{"id":"\([0-9]\+\)".\+/\1/')
    # 更新記錄IP
    recordRS=$(arApiPost "Record.Ddns" "domain_id=${domainID}&record_id=${recordID}&sub_domain=${2}&record_line=預設")
    recordCD=$(echo $recordRS | sed 's/.\+{"code":"\([0-9]\+\)".\+/\1/')
    # 輸出記錄IP
    if [ "$recordCD" == "1" ]; then
        echo $recordRS | sed 's/.\+,"value":"\([0-9\.]\+\)".\+/\1/'
        return 1
    fi
    # 輸出錯誤資訊
    echo $recordRS | sed 's/.\+,"message":"\([^"]\+\)".\+/\1/'
}
   
# 動态檢查更新
# 參數: 主域名 子域名
arDdnsCheck() {
    local postRS
    local hostIP=$(arIpAdress)
    local lastIP=$(arNslookup "${2}.${1}")
    echo "hostIP: ${hostIP}"
    echo "lastIP: ${lastIP}"
    if [ "$lastIP" != "$hostIP" ]; then
        postRS=$(arDdnsUpdate $1 $2)
        echo "postRS: ${postRS}"
        if [ $? -ne 1 ]; then
            return 0
        fi
    fi
    return 1
}
   
###################################################
   
# 設定使用者參數
arMail="登入賬戶"
arPass="密碼"
   
# 檢查更新域名
arDdnsCheck "更新域名" "域名字首"