• 用ssh客户端软件连接路由器,
  • #cd /opt
  • # mkdir aliddns.sh
  • #vi aliddns.sh
  • 将脚本复制粘贴到编辑栏
  • #wq 保存当前文件
  • #chmod 755 aliddns.dh
  • #./aliddns.dh
  • ping一下域名是否解析成功。
  • 最后将脚本写入启动进程就可以了。

以下为脚本文件:

#!/bin/sh

------------以下是要修改的内容-------------------------

aliddns_ak=E7jk1go5wd #ak 修改成自己的阿里云密钥
aliddns_sk=T7TN2nTt4xu9yBu #sk 修改成自己的阿里云密钥
aliddns_name=ubnt #二级域名 ubnt换成自己的二级域名
aliddns_domain=smilefrog.net #域名 smilefrog.net换成自己的域名

----------修改结束-----------------------

aliddns_type=A #记录类型A ipv4,AAAA ipv6
aliddns_curl="curl -s whatismyip.akamai.com" #查询自己域名的脚本 curl -s whatismyip.akamai.com 查ipv4 , curl -s ipv6.whatismyip.akamai.com 查ipv6
aliddns_dns=223.5.5.5 #阿里云的dns 用于查询线上dns解析的 ip
aliddns_record_id=""
aliddns_ttl=600
aliddns_version=1.0

now=date

die () {
echo $1

}

file_name="/tmp/aliddns_$aliddns_name.$aliddns_domain.$aliddns_type.id";

if [ -f "$file_name" ];then
aliddns_record_id=cat $file_name;
else
aliddns_record_id=""
fi

ip=$aliddns_curl 2>&1

current_ip=host $aliddns_name.$aliddns_domain $aliddns_dns |grep 'has address' 2>&1

echo "curl_ip: $ip dns_ip:$current_ip"

if [ "$?" -eq "0" ]
then
current_ip=echo "$current_ip" | tail -n1|awk -F ' ' '{print $4}'
echo "dns_ip:$current_ip"
if [ "$ip" = "$current_ip" ]
then
echo "skipping"
exit 0
fi
fi

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="$outprintf '%%%02X' "'$c"" ;;
esac
done
echo -n $out
}

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)
#echo "http://alidns.aliyuncs.com/?$args&Signature=$(enc "$hash")"
curl -s "http://alidns.aliyuncs.com/?$args&Signature=$(enc "$hash")"
}

get_recordid() {
grep -Eo '"RecordId":"[0-9]+"' | cut -d':' -f2 | tr -d '"'
}

query_recordid() {
send_request "DescribeSubDomainRecords" "SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&SubDomain=$aliddns_name.$aliddns_domain&Timestamp=$timestamp&Type=$aliddns_type"
}

update_record() {
send_request "UpdateDomainRecord" "RR=$aliddns_name&RecordId=$1&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=$aliddns_ttl&Timestamp=$timestamp&Type=$aliddns_type&Value=$(enc "$ip")"
}

add_record() {
send_request "AddDomainRecord&DomainName=$aliddns_domain" "RR=$aliddns_name&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=$aliddns_ttl&Timestamp=$timestamp&Type=$aliddns_type&Value=$(enc "$ip")"
}

if [ "$aliddns_record_id" = "" ]
then
aliddns_record_id=query_recordid | get_recordid
fi
if [ "$aliddns_record_id" = "" ]
then
aliddns_record_id=add_record | get_recordid
echo "added record $aliddns_record_id"
else
update_record $aliddns_record_id
echo "updated record $aliddns_record_id"
fi

save to file

if [ "$aliddns_record_id" = "" ]; then
# failed
echo "aliddns_record_id null"
else
#dbus ram aliddns_record_id=$aliddns_record_id
echo $aliddns_record_id > $file_name
fi

作者 Qi, Asura

发表评论

您的电子邮箱地址不会被公开。