群晖添加计划任务,自动修改阿里云域名IP地址,方便外部访问

2021/01 21 20:01

群晖---计划任务--新增---计划的任务---用户自定义脚本

aliddns_domain="XXXXXXXXXXXX.cn"  你的域名

aliddns_ak="XXXXXXXXXXXXX"   你的阿里云ak值

aliddns_sk="XXXXXXXXXXXXXXXXX"  你的阿里云 sk值

在阿里云 api中申请得到

#!/bin/sh

aliddns_name="mail"

aliddns_domain="XXXXXXXXXXXX.cn"

aliddns_ak="XXXXXXXXXXXXX"

aliddns_sk="XXXXXXXXXXXXXXXXX"

aliddns_curl="curl -s whatismyip.akamai.com"

aliddns_dns="8.8.8.8"

aliddns_ttl="600"

ip=`$aliddns_curl 2>&1`

current_ip=`nslookup $aliddns_name.$aliddns_domain $aliddns_dns 2>&1`

if [ "$?" -eq "0" ]

then

current_ip=`echo "$current_ip" | grep 'Address 1' | tail -n1 | awk '{print $NF}'`

if [ "$ip" = "$current_ip" ]

then

exit 0

fi

fi

timestamp=`date -u "+%Y-%m-%dT%H%%3A%M%%3A%SZ"`

urlencode() {

# urlencode <string>

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

}

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")"

}

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"

}

update_record() {

send_request "UpdateDomainRecord" "RR=$aliddns_name&RecordId=$1&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=$aliddns_ttl&Timestamp=$timestamp&Type=A&Value=$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=A&Value=$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

--转载请注明: https://www.zhangmingming.cn/1206.html


发表回复

欢迎回来 (打开)

(必填)