优化部分判断逻辑
This commit is contained in:
1 parent
a5549ccb60
commit
3e7ccc78bd
3 files changed
+86
-74
No files matched your search
@@ -14,6 +14,11 @@
|
|||||||
|
|
||||||
# 最近更新
|
# 最近更新
|
||||||
|
|
||||||
|
2021/3/3
|
||||||
|
|
||||||
|
- 优化部分判断逻辑
|
||||||
|
- 优化消息输出
|
||||||
|
|
||||||
2021/2/8
|
2021/2/8
|
||||||
|
|
||||||
- 添加IPv6支持
|
- 添加IPv6支持
|
||||||
|
|||||||
@@ -2,11 +2,12 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
# AnripDdns v6.0.0
|
# AnripDdns v6.1.0
|
||||||
#
|
#
|
||||||
# Dynamic DNS using DNSPod API
|
# Dynamic DNS using DNSPod API
|
||||||
#
|
#
|
||||||
# Author: anrip, https://www.anrip.com/?s=dnspod
|
# Author: Anrip, https://github.com/anrip
|
||||||
|
# https://www.anrip.com/?s=dnspod
|
||||||
# Collaborators: ProfFan, https://github.com/ProfFan
|
# Collaborators: ProfFan, https://github.com/ProfFan
|
||||||
#
|
#
|
||||||
# Usage: please refer to `ddnspod.sh`
|
# Usage: please refer to `ddnspod.sh`
|
||||||
@@ -17,9 +18,9 @@
|
|||||||
|
|
||||||
export arToken=""
|
export arToken=""
|
||||||
|
|
||||||
# Get WAN-IP
|
# Get WAN IPv4
|
||||||
|
|
||||||
arWanIp() {
|
arWanIp4() {
|
||||||
|
|
||||||
local hostIp
|
local hostIp
|
||||||
|
|
||||||
@@ -42,19 +43,24 @@ arWanIp() {
|
|||||||
|
|
||||||
if [ -z "$hostIp" ]; then
|
if [ -z "$hostIp" ]; then
|
||||||
if type wget >/dev/null 2>&1; then
|
if type wget >/dev/null 2>&1; then
|
||||||
hostIp=$(wget --quiet --output-document=- http://members.3322.org/dyndns/getip)
|
hostIp=$(wget -q -O- http://members.3322.org/dyndns/getip)
|
||||||
else
|
else
|
||||||
hostIp=$(curl -s http://members.3322.org/dyndns/getip)
|
hostIp=$(curl -s http://members.3322.org/dyndns/getip)
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ -n "$(echo $hostIp | awk '/^[0-9\.]+$/')" ]; then
|
||||||
|
echo "arWanIp4: No IPv4 address found, please check if you have one"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
echo $hostIp
|
echo $hostIp
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get WAN IPv6
|
# Get WAN IPv6
|
||||||
|
|
||||||
arWan6Ip() {
|
arWanIp6() {
|
||||||
|
|
||||||
local hostIp
|
local hostIp
|
||||||
|
|
||||||
@@ -69,8 +75,8 @@ arWan6Ip() {
|
|||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
if [ -z "$hostIp" ]; then
|
if [ -n "$(echo $hostIp | awk '/^[0-9a-zA-Z:]+$/')" ]; then
|
||||||
echo "arWan6Ip: No IPv6 address found, please check if you have one"
|
echo "arWanIp6: No IPv6 address found, please check if you have one"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -79,49 +85,50 @@ arWan6Ip() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Dnspod Bridge
|
# Dnspod Bridge
|
||||||
# Arg: type data
|
# Args: type data
|
||||||
|
|
||||||
arDdnsApi() {
|
arDdnsApi() {
|
||||||
|
|
||||||
local agent="AnripDdns/6.0.0(mail@anrip.com)"
|
local agent="AnripDdns/6.1.0(mail@anrip.com)"
|
||||||
|
|
||||||
local apiurl="https://dnsapi.cn/${1:?'Info.Version'}"
|
local apiurl="https://dnsapi.cn/${1:?'Info.Version'}"
|
||||||
local params="login_token=$arToken&format=json&$2"
|
local params="login_token=$arToken&format=json&$2"
|
||||||
|
|
||||||
if type wget >/dev/null 2>&1; then
|
if type wget >/dev/null 2>&1; then
|
||||||
wget --quiet --no-check-certificate --output-document=- --user-agent=$agent --post-data $params $apiurl
|
wget -q -O- --no-check-certificate -U $agent --post-data $params $apiurl
|
||||||
else
|
else
|
||||||
curl -s -A $agent -d $params $apiurl
|
curl -s -A $agent -d $params $apiurl
|
||||||
fi
|
fi
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Dnspod Domain ID & Record ID
|
# Fetch Ids of Domain and Record
|
||||||
# Arg: record-type domain subdomain
|
# Args: recordType domain subdomain
|
||||||
|
|
||||||
arDdnsId() {
|
arDdnsIds() {
|
||||||
|
|
||||||
|
local errMsg
|
||||||
|
|
||||||
local domainId
|
local domainId
|
||||||
local recordId
|
local recordId
|
||||||
local errMsg
|
|
||||||
|
|
||||||
# Get domain ID
|
# Get Domain Id
|
||||||
domainId=$(arDdnsApi "Domain.Info" "domain=$2")
|
domainId=$(arDdnsApi "Domain.Info" "domain=$2")
|
||||||
domainId=$(echo $domainId | sed 's/.*"id":"\([0-9]*\)".*/\1/')
|
domainId=$(echo $domainId | sed 's/.*"id":"\([0-9]*\)".*/\1/')
|
||||||
|
|
||||||
if ! [ "$domainId" -gt 0 ] 2>/dev/null ;then
|
if ! [ "$domainId" -gt 0 ] 2>/dev/null ;then
|
||||||
errMsg=$(echo $domainId | sed 's/.*"message":"\([^\"]*\)".*/\1/')
|
errMsg=$(echo $domainId | sed 's/.*"message":"\([^\"]*\)".*/\1/')
|
||||||
echo "arDdnsId: $errMsg"
|
echo "arDdnsIds: $errMsg"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get Record ID
|
# Get Record Id
|
||||||
recordId=$(arDdnsApi "Record.List" "domain_id=$domainId&sub_domain=$3&record_type=$1")
|
recordId=$(arDdnsApi "Record.List" "domain_id=$domainId&sub_domain=$3&record_type=$1")
|
||||||
recordId=$(echo $recordId | sed 's/.*"id":"\([0-9]*\)".*/\1/')
|
recordId=$(echo $recordId | sed 's/.*"id":"\([0-9]*\)".*/\1/')
|
||||||
|
|
||||||
if ! [ "$recordId" -gt 0 ] 2>/dev/null ;then
|
if ! [ "$recordId" -gt 0 ] 2>/dev/null ;then
|
||||||
errMsg=$(echo $recordId | sed 's/.*"message":"\([^\"]*\)".*/\1/')
|
errMsg=$(echo $recordId | sed 's/.*"message":"\([^\"]*\)".*/\1/')
|
||||||
echo "arDdnsId: $errMsg"
|
echo "arDdnsIds: $errMsg"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -129,17 +136,19 @@ arDdnsId() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Fetch Record Ip
|
# Fetch Record Ip
|
||||||
# Arg: domain-id record-id
|
# Args: domainId recordId
|
||||||
|
|
||||||
arDdnsRecordIp() {
|
arDdnsRecordIp() {
|
||||||
|
|
||||||
|
local errMsg
|
||||||
|
|
||||||
local recordIp
|
local recordIp
|
||||||
|
|
||||||
# Last IP
|
# Get Record Ip
|
||||||
recordIp=$(arDdnsApi "Record.Info" "domain_id=$1&record_id=$2")
|
recordIp=$(arDdnsApi "Record.Info" "domain_id=$1&record_id=$2")
|
||||||
recordIp=$(echo $recordIp | sed 's/.*,"value":"\([0-9a-fA-F\.\:]*\)".*/\1/')
|
recordIp=$(echo $recordIp | sed 's/.*,"value":"\([0-9a-fA-F\.\:]*\)".*/\1/')
|
||||||
|
|
||||||
# Output IP
|
# Output Record Ip
|
||||||
case "$recordIp" in
|
case "$recordIp" in
|
||||||
[1-9]*)
|
[1-9]*)
|
||||||
echo $recordIp
|
echo $recordIp
|
||||||
@@ -155,91 +164,87 @@ arDdnsRecordIp() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Update Record Ip
|
# Update Record Ip
|
||||||
# Arg: domain-id record-id sub-domain update-ip
|
# Args: domainId recordId subdomain hostIp recordType
|
||||||
|
|
||||||
arDdnsUpdate() {
|
arDdnsUpdate() {
|
||||||
|
|
||||||
local recordType
|
local errMsg
|
||||||
|
|
||||||
local recordRs
|
local recordRs
|
||||||
local recordIp
|
local recordIp
|
||||||
local recordCd
|
local recordCd
|
||||||
local errMsg
|
|
||||||
|
|
||||||
if [ -n "$(echo "$4" | awk '/^[0-9a-zA-Z:]+$/')" ]; then
|
if [ -z "$5" ]; then
|
||||||
recordType="AAAA"
|
echo "arDdnsUpdate: Args number error"
|
||||||
elif [ -n "$(echo "$4" | awk '/^[0-9\.]+$/')" ]; then
|
|
||||||
recordType="A"
|
|
||||||
else
|
|
||||||
echo "arDdnsRecordUpdate: bad ip address"
|
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Update IP
|
# Update Ip
|
||||||
recordRs=$(arDdnsApi "Record.Modify" "domain_id=$1&record_id=$2&sub_domain=$3&record_type=$recordType&value=$4&record_line=%e9%bb%98%e8%ae%a4")
|
recordRs=$(arDdnsApi "Record.Modify" "domain_id=$1&record_id=$2&sub_domain=$3&record_type=$5&value=$4&record_line=%e9%bb%98%e8%ae%a4")
|
||||||
recordIp=$(echo $recordRs | sed 's/.*,"value":"\([0-9a-fA-F\.\:]*\)".*/\1/')
|
recordIp=$(echo $recordRs | sed 's/.*,"value":"\([0-9a-fA-F\.\:]*\)".*/\1/')
|
||||||
recordCd=$(echo $recordRs | sed 's/.*{"code":"\([0-9]*\)".*/\1/')
|
recordCd=$(echo $recordRs | sed 's/.*{"code":"\([0-9]*\)".*/\1/')
|
||||||
|
|
||||||
# Output IP
|
# Output Result
|
||||||
if [ "$recordIp" = "$4" ] && [ "$recordCd" = "1" ]; then
|
if [ "$recordIp" = "$4" ] && [ "$recordCd" = "1" ]; then
|
||||||
echo $recordIp
|
echo "arDdnsUpdate: success"
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
# Echo error message
|
|
||||||
errMsg=$(echo $recordRs | sed 's/.*,"message":"\([^"]*\)".*/\1/')
|
errMsg=$(echo $recordRs | sed 's/.*,"message":"\([^"]*\)".*/\1/')
|
||||||
echo "arDdnsRecordUpdate: $errMsg"
|
echo "arDdnsUpdate: $errMsg"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# DDNS Check
|
# DDNS Check
|
||||||
# Arg: Main Sub
|
# Args: Main Sub
|
||||||
arDdnsCheck() {
|
arDdnsCheck() {
|
||||||
|
|
||||||
local postRs
|
local recordType
|
||||||
local ddnsIds
|
|
||||||
local lastIP
|
|
||||||
local hostIp
|
local hostIp
|
||||||
|
|
||||||
if [ $# -eq 3 ] && [ $3 -eq 6 ]; then
|
local ddnsIds
|
||||||
hostIp=$(arWan6Ip)
|
local lastIp
|
||||||
if [ $? -ne 0 ]; then
|
local postRs
|
||||||
echo "$hostIp"
|
|
||||||
return 1
|
if [ "$3" = "6" ]; then
|
||||||
fi
|
recordType=AAAA
|
||||||
ddnsIds=$(arDdnsId "AAAA" "$1" "$2")
|
hostIp=$(arWanIp6)
|
||||||
else
|
else
|
||||||
hostIp=$(arWanIp)
|
recordType=A
|
||||||
ddnsIds=$(arDdnsId "A" "$1" "$2")
|
hostIp=$(arWanIp4)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo "$ddnsIds"
|
echo $hostIp
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Updating Domain: $2.$1"
|
ddnsIds=$(arDdnsIds $recordType $1 $2)
|
||||||
echo "Domain IDs: $ddnsIds"
|
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo $ddnsIds
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Checking Domain: $2.$1"
|
||||||
|
lastIp=$(arDdnsRecordIp $ddnsIds)
|
||||||
|
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo $lastIp
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Domain Ids: $ddnsIds"
|
||||||
echo "Host Ip: $hostIp"
|
echo "Host Ip: $hostIp"
|
||||||
|
echo "Last Ip: $lastIp"
|
||||||
lastIP=$(arDdnsRecordIp $(echo $ddnsIds))
|
|
||||||
if [ $? -eq 0 ]; then
|
if [ "$lastIp" == "$hostIp" ]; then
|
||||||
echo "lastIP: $lastIP"
|
echo "Last Ip is the same as host Ip"
|
||||||
if [ "$lastIP" != "$hostIp" ]; then
|
|
||||||
postRs=$(arDdnsUpdate $(echo $ddnsIds) $2 $hostIp)
|
|
||||||
if [ $? -eq 0 ]; then
|
|
||||||
echo "postRs: $postRs"
|
|
||||||
return 0
|
|
||||||
else
|
|
||||||
echo "$postRs"
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
echo "Last IP is the same as current IP!"
|
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "$lastIP"
|
echo "Updating Domain: $2.$1"
|
||||||
return 1
|
arDdnsUpdate $ddnsIds $2 $hostIp $recordType
|
||||||
|
|
||||||
}
|
}
|
||||||
+4
-2
@@ -9,7 +9,9 @@ arToken="12345,7676f344eaeaea9074c123451234512d"
|
|||||||
|
|
||||||
# Place each domain you want to check as follows
|
# Place each domain you want to check as follows
|
||||||
# you can have multiple arDdnsCheck blocks
|
# you can have multiple arDdnsCheck blocks
|
||||||
# add 4 or 6 to tail will specify the IP version used, default 4
|
|
||||||
# IPv6:
|
# IPv6:
|
||||||
# arDdnsCheck "test.org" "subdomain6" 6
|
# arDdnsCheck "test.org" "subdomain" 6
|
||||||
|
|
||||||
|
# IPv4:
|
||||||
arDdnsCheck "test.org" "subdomain"
|
arDdnsCheck "test.org" "subdomain"
|
||||||
Reference in new issue
Block a user