DNSPod的API接口實(shí)現(xiàn)DDNS客戶端
將dnspodsh復(fù)制到/usr/bin下設(shè)置成可執(zhí)行屬性,并在/etc/rc.local中加入下面一行即可實(shí)現(xiàn)你自己的免費(fèi)DDNS:/usr/bin/dnspodsh dnspod_name dns
將dnspodsh復(fù)制到/usr/bin下設(shè)置成可執(zhí)行屬性,并在/etc/rc.local中加入下面一行即可實(shí)現(xiàn)你自己的免費(fèi)DDNS:
/usr/bin/dnspodsh dnspod_name dnspod_passwaord &>/dev/null
保存時(shí)注意使用LINUX格式和UTF8編碼
設(shè)置域名的地方有2個(gè)數(shù)組,如果只有一個(gè)域名的話可以這么設(shè)置:
domainList[0]='baidu.com * @'
domainList[1]='baidu.com www'
文件名:dnspodsh
內(nèi)容:
#!/bin/bash
##############################
# dnspodsh v0.3
# 基于dnspod api構(gòu)架的bash ddns客戶端
# 作者:zrong(zengrong.net)
# 詳細(xì)介紹:http://zengrong.net/post/1524.htm
# 創(chuàng)建日期:2012-02-13
# 更新日期:2012-03-11
##############################
login_email=${1:?'必須提供登錄名'}
login_password=${2:?'必須提供密碼'}
format="json"
lang="en"
userAgent="dnspodsh/0.3(zrongzrong@gmail.com)"
commonPost="login_email=$login_email&login_password=$login_password&format=$format&lang=$lang"
apiUrl='https://dnsapi.cn/'
ipUrl='http://members.3322.org/dyndns/getip'
# 要處理的域名數(shù)組,每個(gè)元素代表一個(gè)域名的一組記錄
# 在數(shù)組的一個(gè)元素中,以空格分隔域名和子域名
# 第一個(gè)空格前為主域名,后面用空格分離多個(gè)子域名
# 如果使用泛域名,必須用*轉(zhuǎn)義
domainList[0]='domain1.com * @ www'
domainList[1]='domain2.com subdomain subdomain2'
# 多長時(shí)間比較一次ip地址
delay=300
# logfile
logDir='/var/log'
logFile=$logDir'/dnspodsh.log'
traceFile=$logDir'/dnspodshtrace.log'
# 檢測ip地址是否符合要求
checkip()
{
# ipv4地址
if [[ "$1" =~ ^([0-9]{1,3}.){3}[0-9]{1,3}$ ]];then
return 0
# ipv6地址
elif [[ "$1" =~ ^([?-fA-F]{1,4}:){7}[?-fA-F]{1,4}$|^:((:[?-fA-F]{1,4}){1,6}|:)$|^[?-fA-F]{1,4}:((:[?-fA-F]{1,4}){1,5}|:)$|^([?-fA-F]{1,4}:){2}((:[?-fA-F]{1,4}){1,4}|:)$|^([?-fA-F]{1,4}:){3}((:[?-fA-F]{1,4}){1,3}|:)$|^([?-fA-F]{1,4}:){4}((:[?-fA-F]{1,4}){1,2}|:)$|^([?-fA-F]{1,4}:){5}:([?-fA-F]{1,4})?$|^([?-fA-F]{1,4}:){6}:$ ]];then
return 0
fi
return 1
}
getUrl()
{
#curl -s -A $userAgent -d $commonPost$2 --trace $traceFile $apiUrl$1
curl -s -A $userAgent -d $commonPost$2 $apiUrl$1
}
getVersion()
{
getUrl "Info.Version"
}
getUserDetail()
{
getUrl "User.Detail"
}
writeLog()
{
if [ -w $logDir ];then
local pre=`date`
for arg in $@;do
pre=$pre't'$arg
done
echo -e $pre>>$logFile
fi
echo -e $1
}
getDomainList()
{
getUrl "Domain.List" "&type=all&offset=0&length=10"
}
# 根據(jù)域名id獲取記錄列表
# $1 域名id
getRecordList()
{
getUrl "Record.List" "&domain_id=$1&offset=0&length=20"
}
# 設(shè)置記錄
setRecord()
{
writeLog "set domain $3.$8 to new ip:$7"
local subDomain=$3
# 由于*會(huì)被擴(kuò)展,在最后一步將轉(zhuǎn)義的*替換成*
if [ "$subDomain" = '*' ];then
subDomain='*'
fi
local request="&domain_id=$1&record_id=$2&sub_domain=$sub