大家好,又见面了,我是你们的朋友全栈君。
#!/bin/bash
set -e
#================================================================================================================#
# 功能:用于更新阿里云域名IP,实现DDNS功能
#
# 在 http://www.gebi1.com/forum.php?mod=viewthread&tid=287344&page=1&_dsign=8f94f74c 提供的脚本文件基础上修改的。
# ghui, modified 12/2/2019
# 在 N1 debian Buster with Armbian Linux 5.3.0-aml-g12 手动执行/定时任务(crontab)执行测试通过
#================================================================================================================#
#
# 使用方法:
#
# 方法1. 外部参数
# 修改源码,将对应参数 修改为$1,$2,$3,$4,$5,$6
# aliddns.sh <aliddns_ak> <aliddns_sk> <aliddns_subdomain> <aliddns_domain> <aliddns_iptype> <aliddns_ttl>
# 示例(A 代表 IPv4,AAAA 代表 IPv6):
# 执行:aliddns.sh "xxxx" "xxx" "test" "mydomain.site" "A" 600
# 执行:aliddns.sh "xxxx" "xxx" "test" "mydomain.site" "AAAA" 600
#
# 方法2. 内部参数
# 修改源码,将$1,$2,$3,$4,$5,$6 替换为对应参数
#
# 示例:
# aliddns_ak="xxxx"
# aliddns_sk="xxx"
# aliddns_subdomain="test"
# aliddns_domain="mydomain.site"
# aliddns_iptype="A"
# aliddns_ttl=600
# 执行:aliddns.sh
#
#================================================================================================================#
#--------------------------------------------------------------
# 参数
#
# (*)阿里云 AccessKeyId
aliddns_ak=xxx
# (*)阿里云 AccessKeySecret
aliddns_sk=xxx
# (*)域名:test.mydomain.com
aliddns_subdomain=test #'test'
aliddns_domain=xxx.com #'mydomain.com'
# (*)ip地址类型:'A' 或 'AAAA',代表ipv4 和 ipv6
aliddns_iptype=A # 'A' 或 'AAAA',代表ipv4 和 ipv6
# TTL 默认10分钟 = 600秒
aliddns_ttl=600 #"600"
#--------------------------------------------------------------
machine_ip=""
ddns_ip=""
aliddns_record_id=""
if [ "$aliddns_subdomain" = "@" ]
then
aliddns_name=$aliddns_domain
else
aliddns_name=$aliddns_subdomain.$aliddns_domain
fi
now=`date`
echo "**************************************************"
echo "$now"
echo "$aliddns_name"
function getMachine_IPv4() {
echo $(/usr/bin/wget -qO- -t1 -T2 http://ip.3322.net)
}
function getMachine_IPv6() {
ipv6=`ip addr | grep "inet6.*global" | grep -v "deprecated" | awk '{print $2}' | awk -F"/" '{print $1}' | sed -n '1,1p'`
echo $ipv6
}
function getDDNS_IP() {
current_ip=`nslookup -query=$aliddns_iptype $aliddns_name | grep "Address" | grep -v "#53" | awk '{print $2}'`
echo $current_ip
}
function 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
}
function enc() {
echo -n "$1" | urlencode
}
function 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")"
}
function get_recordid() {
grep -Eo '"RecordId":"[0-9]+"' | cut -d':' -f2 | tr -d '"'
}
function query_recordid() {
send_request "DescribeSubDomainRecords" "SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&SubDomain=$aliddns_name&Timestamp=$timestamp&Type=$aliddns_iptype"
}
function update_record() {
send_request "UpdateDomainRecord" "RR=$aliddns_subdomain&RecordId=$1&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=$aliddns_ttl&Timestamp=$timestamp&Type=$aliddns_iptype&Value=$(enc $machine_ip)"
}
function add_record() {
send_request "AddDomainRecord&DomainName=$aliddns_domain" "RR=$aliddns_subdomain&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=$aliddns_ttl&Timestamp=$timestamp&Type=$aliddns_iptype&Value=$(enc $machine_ip)"
}
if [ "$aliddns_iptype" = 'A' ]
then
echo "ddns is IPv4."
machine_ip=`echo "$(getMachine_IPv4)"`
echo "machine_ip = $machine_ip"
aliddns_record_id=$aliddnsipv4_record_id
else
echo "ddns is IPv6."
machine_ip=`echo "$(getMachine_IPv6)"`
echo "machine_ip = $machine_ip"
aliddns_record_id=$aliddnsipv6_record_id
fi
ddns_ip=`echo "$(getDDNS_IP)"`
echo "ddns_ip = $ddns_ip"
if [ "$machine_ip" = "" ]
then
echo "machine_ip is empty!"
exit 0
fi
if [ "$machine_ip" = "$ddns_ip" ]
then
echo "skipping\n"
exit 1
fi
echo "start update..."
timestamp=`date -u "+%Y-%m-%dT%H%%3A%M%%3A%SZ"`
if [ "$aliddns_record_id" = "" ]
then
aliddns_record_id=`query_recordid | get_recordid`
echo "----------------" $aliddns_record_id "\n"
if [ "$aliddns_iptype" = 'A' ]
then
aliddnsipv4_record_id=$aliddns_record_id
else
aliddnsipv6_record_id=$aliddns_record_id
fi
fi
#add support */%2A and @/%40 record
if [ "$aliddns_record_id" = "" ]
then
echo "add record starting"
aliddns_record_id=`add_record | get_recordid`
if [ "$aliddns_record_id" = "" ]
then
echo "aliddns_record_id is empty. \n"
else
if [ "$aliddns_iptype" = 'A' ]
then
aliddnsipv4_record_id=$aliddns_record_id
else
aliddnsipv6_record_id=$aliddns_record_id
fi
echo "added record $aliddns_record_id \n"
fi
else
echo "update record starting"
update_record $aliddns_record_id
echo "updated record $aliddns_record_id \n"
fi
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/139871.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...