梅林路由器-阿里云ddns-ipv6脚本(含socat转发)
#!/bin/sh # Author: tyasky ak="eeeeeqqerewrwerewe" sk="wwwwwwqqewrewrer2432r3qr43qq" host="cx" domain="rvwcom.cn" runnum=10 # 最多尝试更新次数 rungap=60 # 尝试间隔秒数 type=AAAA # 解析记录类型 downvalue="" # 解析值,留空则动态获取 # 根据type选择获取ip的函数 if [ "$type" = "AAAA" ];then get_downvalue() { ip -6 addr|grep '64 scope global'|head -1|awk -F/ '{print $1}'|awk '{print $NF}' #ifconfig -a|grep inet6|grep 64|grep -v fe | awk '{print $3}' |cut -d "/" -f1 } else get_downvalue() { ip -4 addr|grep global\ ppp|head -1|awk -F/ '{print $1}'|awk '{print $2}' } fi # 第二个参数指定额外不编码的字符 urlencode() { local string="${1}" local strlen=${#string} local encoded="" local pos c o for pos in $(awk "BEGIN { for ( i=0; i<$strlen; i++ ) { print i; } }") do c=${string:$pos:1} case $c in [-_.~a-zA-Z0-9$2] ) o="${c}" ;; * ) o=`printf '%%%02X' "'$c"` esac encoded="$encoded$o" done echo "${encoded}" } send_request() { timestamp=`date -u +"%Y-%m-%dT%H:%M:%SZ"` nonce=`openssl rand -base64 8 | md5sum | cut -c1-8` args="AccessKeyId=$ak&Format=json&SignatureMethod=HMAC-SHA1&SignatureNonce=$nonce&SignatureVersion=1.0&Timestamp=$timestamp&Version=2015-01-09&$1" args=`echo $args | sed 's/\&/\n/g' | LC_ALL=c sort | xargs | sed 's/ /\&/g'` CanonicalizedQueryString=$(urlencode $args "=&") StringToSign="GET&%2F&$(urlencode $CanonicalizedQueryString)" Signature=$(urlencode $(echo -n "$StringToSign" | openssl dgst -sha1 -hmac "$sk&" -binary | openssl base64)) echo $(curl -k -s "https://alidns.aliyuncs.com/?$args&Signature=$Signature") echo $args } getValueFromJson() { local json="$1" local key="$2" echo $json | sed 's/":/:/g;s/"//g;s/,/\n/g' | grep $key | awk -F: '{ print $2 }' } DescribeSubDomainRecords() { send_request "Action=DescribeSubDomainRecords&SubDomain=$host.$domain" } UpdateDomainRecord() { local recordid=$(getValueFromJson `DescribeSubDomainRecords` "RecordId") send_request "Action=UpdateDomainRecord&RR=$host&RecordId=$recordid&Type=$type&Value=$downvalue" } AddDomainRecord() { send_request "Action=AddDomainRecord&DomainName=$domain&RR=$host&Type=$type&Value=$downvalue" } DeleteSubDomainRecords() { send_request "Action=DeleteSubDomainRecords&DomainName=$domain&RR=$host" } start_update(){ while [ $runnum -gt 0 ] do runnum=$(expr $runnum - 1) datetime=$(date +%Y-%m-%d\ %T) echo 当前时间:$datetime rslt=`DescribeSubDomainRecords | grep TotalCount` if [ -z "$rslt" ];then echo "未获取到阿里云查询结果" sleep $rungap continue fi upvalue=$(getValueFromJson "$rslt" "Value") echo 域名指向:$upvalue downvalue=${downvalue:=`get_downvalue`} if [ -z "$downvalue" ]; then echo "未获取到本机地址" sleep $rungap continue fi echo 本机地址:$downvalue if [ "$upvalue" = "$downvalue" ]; then echo "处理结果:已正确解析,无需更新。" elif [ -n "$upvalue" ]; then echo "更新解析记录..." UpdateDomainRecord else echo "添加解析记录..." AddDomainRecord fi break done } runsocat() { echo "添加转发记录:[$downvalue]:$1 >> $2" start-stop-daemon --start --background --name S$1 --exec /usr/sbin/socat TCP6-LISTEN:$1,ipv6-v6only=1,reuseaddr,fork TCP4:$2 } start_update /sbin/ddns_custom_updated 1 #路由器web 18443 start-stop-daemon --start --background --name S18443 --exec /usr/sbin/socat TCP6-LISTEN:18443,ipv6-v6only=1,reuseaddr,fork TCP4:192.168.132.1:18443 #exsi 服务器 18543 start-stop-daemon --start --background --name S18543 --exec /usr/sbin/socat TCP6-LISTEN:18543,reuseaddr,fork TCP4:192.168.132.99:443 #阿里云webdav 18680 start-stop-daemon --start --background --name S18680 --exec /usr/sbin/socat TCP6-LISTEN:18680,ipv6-v6only=1,reuseaddr,fork TCP4:192.168.132.1:18680 #路由器webdav 18780 start-stop-daemon --start --background --name hacdias_webdav --exec /koolshare/hacdias/webdav -- -c /koolshare/hacdias/webdav.yaml