Created
November 28, 2019 09:15
-
-
Save kyriosli/e61331d9f9a38d4ff41c89c0af76c087 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
aliddns_ak="TODO" # 你的阿里云 appkey | |
aliddns_sk="TODO" # 你的阿里云 appsecret | |
aliddns_curl="curl -s whatismyip.akamai.com" # 获取当前公网 IP 的命令,如果不好用可以尝试别的方式 | |
aliddns_dns="223.5.5.5" # 阿里云 DNS 服务器,直接从这个服务器进行查询可以避免 DNS 缓存 | |
aliddns_ttl="600" # DNS 的 TTL 时间,免费版最低是 600s | |
aliddns_domain="kyrios.site" # 你的域名 | |
aliddns_name="@" # DNS 三级域名,@ 表示没有三级域名 | |
aliddns_record_id="18313508395238400" # record id,可以留空。为了提高效率,可以在首次运行后,将 record_id 保存下来 | |
if [[ $aliddns_name = "@" ]]; then | |
aliddns_fullname=$aliddns_domain | |
else | |
aliddns_fullname="$aliddns_name.$aliddns_domain" | |
fi | |
ip=`$aliddns_curl 2>&1` | |
current_ip=`nslookup $aliddns_fullname $aliddns_dns 2>&1 | grep 'Address' | tail -n1 | awk '{print $NF}'` | |
if [ "$ip" = "$current_ip" ] | |
then | |
echo "skipping" | |
exit 0 | |
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 | |
} | |
aliddns_name=`enc $aliddns_name` | |
send_request() { | |
local args="AccessKeyId=$aliddns_ak&Action=$1&Format=json&$2&Version=2015-01-09" | |
local hash=$(printf "%s" "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]+"' | head -n1 | cut -d'"' -f4 | |
} | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment