simple style
This commit is contained in:
1 parent
a24824f78d
commit
1c5ed86fa3
1 file changed
+75
-100
+75
-100
@@ -1,16 +1,26 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
#
|
||||||
|
|
||||||
#################################################
|
#################################################
|
||||||
# AnripDdns v5.08
|
# AnripDdns v5.09
|
||||||
# Dynamic DNS using DNSPod API
|
# Dynamic DNS using DNSPod API
|
||||||
# Original by anrip<mail@anrip.com>, http://www.anrip.com/ddnspod
|
# Original by anrip<mail@anrip.com>, www.anrip.com/post/872
|
||||||
# Edited by ProfFan
|
# Edited by ProfFan
|
||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
# OS Detection
|
# Global Variables
|
||||||
|
|
||||||
|
# Token-based Authentication
|
||||||
|
arToken=""
|
||||||
|
|
||||||
|
# Account-based Authentication
|
||||||
|
arMail=""
|
||||||
|
arPass=""
|
||||||
|
|
||||||
|
# Get WAN IP
|
||||||
|
|
||||||
case $(uname) in
|
case $(uname) in
|
||||||
'Linux')
|
'Linux')
|
||||||
echo "Linux"
|
|
||||||
arIpAddress() {
|
arIpAddress() {
|
||||||
local extip
|
local extip
|
||||||
extip=$(ip -o -4 addr list | grep -Ev '\s(docker|lo)' | awk '{print $4}' | cut -d/ -f1 | grep -Ev '(^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.1[6-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.2[0-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.3[0-1]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^192\.168\.[0-9]{1,3}\.[0-9]{1,3}$)')
|
extip=$(ip -o -4 addr list | grep -Ev '\s(docker|lo)' | awk '{print $4}' | cut -d/ -f1 | grep -Ev '(^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.1[6-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.2[0-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.3[0-1]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^192\.168\.[0-9]{1,3}\.[0-9]{1,3}$)')
|
||||||
@@ -19,112 +29,37 @@ case $(uname) in
|
|||||||
fi
|
fi
|
||||||
echo $extip
|
echo $extip
|
||||||
}
|
}
|
||||||
;;
|
;;
|
||||||
'FreeBSD')
|
|
||||||
echo 'FreeBSD'
|
|
||||||
exit 100
|
|
||||||
;;
|
|
||||||
'WindowsNT')
|
|
||||||
echo "Windows"
|
|
||||||
exit 100
|
|
||||||
;;
|
|
||||||
'Darwin')
|
'Darwin')
|
||||||
echo "Mac"
|
|
||||||
arIpAddress() {
|
arIpAddress() {
|
||||||
ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}'
|
ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}'
|
||||||
}
|
}
|
||||||
;;
|
;;
|
||||||
'SunOS')
|
*)
|
||||||
echo 'Solaris'
|
echo 'Unsupported OS'
|
||||||
exit 100
|
exit 2
|
||||||
;;
|
;;
|
||||||
'AIX')
|
|
||||||
echo 'AIX'
|
|
||||||
exit 100
|
|
||||||
;;
|
|
||||||
*) ;;
|
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Get script dir
|
|
||||||
# See: http://stackoverflow.com/a/29835459/4449544
|
|
||||||
rreadlink() ( # Execute the function in a *subshell* to localize variables and the effect of `cd`.
|
|
||||||
|
|
||||||
target=$1 fname= targetDir= CDPATH=
|
|
||||||
|
|
||||||
# Try to make the execution environment as predictable as possible:
|
|
||||||
# All commands below are invoked via `command`, so we must make sure that `command`
|
|
||||||
# itself is not redefined as an alias or shell function.
|
|
||||||
# (Note that command is too inconsistent across shells, so we don't use it.)
|
|
||||||
# `command` is a *builtin* in bash, dash, ksh, zsh, and some platforms do not even have
|
|
||||||
# an external utility version of it (e.g, Ubuntu).
|
|
||||||
# `command` bypasses aliases and shell functions and also finds builtins
|
|
||||||
# in bash, dash, and ksh. In zsh, option POSIX_BUILTINS must be turned on for that
|
|
||||||
# to happen.
|
|
||||||
{ \unalias command; \unset -f command; } >/dev/null 2>&1
|
|
||||||
[ -n "$ZSH_VERSION" ] && options[POSIX_BUILTINS]=on # make zsh find *builtins* with `command` too.
|
|
||||||
|
|
||||||
while :; do # Resolve potential symlinks until the ultimate target is found.
|
|
||||||
[ -L "$target" ] || [ -e "$target" ] || { command printf '%s\n' "ERROR: '$target' does not exist." >&2; return 1; }
|
|
||||||
command cd "$(command dirname -- "$target")" # Change to target dir; necessary for correct resolution of target path.
|
|
||||||
fname=$(command basename -- "$target") # Extract filename.
|
|
||||||
[ "$fname" = '/' ] && fname='' # !! curiously, `basename /` returns '/'
|
|
||||||
if [ -L "$fname" ]; then
|
|
||||||
# Extract [next] target path, which may be defined
|
|
||||||
# *relative* to the symlink's own directory.
|
|
||||||
# Note: We parse `ls -l` output to find the symlink target
|
|
||||||
# which is the only POSIX-compliant, albeit somewhat fragile, way.
|
|
||||||
target=$(command ls -l "$fname")
|
|
||||||
target=${target#* -> }
|
|
||||||
continue # Resolve [next] symlink target.
|
|
||||||
fi
|
|
||||||
break # Ultimate target reached.
|
|
||||||
done
|
|
||||||
targetDir=$(command pwd -P) # Get canonical dir. path
|
|
||||||
# Output the ultimate target's canonical path.
|
|
||||||
# Note that we manually resolve paths ending in /. and /.. to make sure we have a normalized path.
|
|
||||||
if [ "$fname" = '.' ]; then
|
|
||||||
command printf '%s\n' "${targetDir%/}"
|
|
||||||
elif [ "$fname" = '..' ]; then
|
|
||||||
# Caveat: something like /var/.. will resolve to /private (assuming /var@ -> /private/var), i.e. the '..' is applied
|
|
||||||
# AFTER canonicalization.
|
|
||||||
command printf '%s\n' "$(command dirname -- "${targetDir}")"
|
|
||||||
else
|
|
||||||
command printf '%s\n' "${targetDir%/}/$fname"
|
|
||||||
fi
|
|
||||||
)
|
|
||||||
|
|
||||||
DIR=$(dirname -- "$(rreadlink "$0")")
|
|
||||||
|
|
||||||
# Global Variables:
|
|
||||||
|
|
||||||
# Token-based Authentication
|
|
||||||
arToken=""
|
|
||||||
# Account-based Authentication
|
|
||||||
arMail=""
|
|
||||||
arPass=""
|
|
||||||
|
|
||||||
# Load config
|
|
||||||
|
|
||||||
#. $DIR/dns.conf
|
|
||||||
|
|
||||||
# Get Domain IP
|
# Get Domain IP
|
||||||
# arg: domain
|
# Arg: domain
|
||||||
|
|
||||||
arDdnsInfo() {
|
arDdnsInfo() {
|
||||||
local domainID recordID recordIP
|
local domainID recordID recordIP
|
||||||
# Get domain ID
|
# Get domain ID
|
||||||
domainID=$(arApiPost "Domain.Info" "domain=${1}")
|
domainID=$(arApiPost "Domain.Info" "domain=${1}")
|
||||||
domainID=$(echo $domainID | sed 's/.*{"id":"\([0-9]*\)".*/\1/')
|
domainID=$(echo $domainID | sed 's/.*{"id":"\([0-9]*\)".*/\1/')
|
||||||
|
|
||||||
# Get Record ID
|
# Get Record ID
|
||||||
recordID=$(arApiPost "Record.List" "domain_id=${domainID}&sub_domain=${2}")
|
recordID=$(arApiPost "Record.List" "domain_id=${domainID}&sub_domain=${2}")
|
||||||
recordID=$(echo $recordID | sed 's/.*\[{"id":"\([0-9]*\)".*/\1/')
|
recordID=$(echo $recordID | sed 's/.*\[{"id":"\([0-9]*\)".*/\1/')
|
||||||
|
|
||||||
# Last IP
|
# Last IP
|
||||||
recordIP=$(arApiPost "Record.Info" "domain_id=${domainID}&record_id=${recordID}")
|
recordIP=$(arApiPost "Record.Info" "domain_id=${domainID}&record_id=${recordID}")
|
||||||
recordIP=$(echo $recordIP | sed 's/.*,"value":"\([0-9\.]*\)".*/\1/')
|
recordIP=$(echo $recordIP | sed 's/.*,"value":"\([0-9\.]*\)".*/\1/')
|
||||||
|
|
||||||
# Output IP
|
# Output IP
|
||||||
case "$recordIP" in
|
case "$recordIP" in
|
||||||
[1-9][0-9]*)
|
[1-9][0-9]*)
|
||||||
echo $recordIP
|
echo $recordIP
|
||||||
return 0
|
return 0
|
||||||
@@ -137,7 +72,8 @@ arDdnsInfo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Get data
|
# Get data
|
||||||
# arg: type data
|
# Arg: type data
|
||||||
|
|
||||||
arApiPost() {
|
arApiPost() {
|
||||||
local agent="AnripDdns/5.07(mail@anrip.com)"
|
local agent="AnripDdns/5.07(mail@anrip.com)"
|
||||||
local inter="https://dnsapi.cn/${1:?'Info.Version'}"
|
local inter="https://dnsapi.cn/${1:?'Info.Version'}"
|
||||||
@@ -150,17 +86,18 @@ arApiPost() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Update
|
# Update
|
||||||
# arg: main domain sub domain
|
# Arg: main domain sub domain
|
||||||
|
|
||||||
arDdnsUpdate() {
|
arDdnsUpdate() {
|
||||||
local domainID recordID recordRS recordCD recordIP myIP
|
local domainID recordID recordRS recordCD recordIP myIP
|
||||||
# Get domain ID
|
# Get domain ID
|
||||||
domainID=$(arApiPost "Domain.Info" "domain=${1}")
|
domainID=$(arApiPost "Domain.Info" "domain=${1}")
|
||||||
domainID=$(echo $domainID | sed 's/.*{"id":"\([0-9]*\)".*/\1/')
|
domainID=$(echo $domainID | sed 's/.*{"id":"\([0-9]*\)".*/\1/')
|
||||||
|
|
||||||
# Get Record ID
|
# Get Record ID
|
||||||
recordID=$(arApiPost "Record.List" "domain_id=${domainID}&sub_domain=${2}")
|
recordID=$(arApiPost "Record.List" "domain_id=${domainID}&sub_domain=${2}")
|
||||||
recordID=$(echo $recordID | sed 's/.*\[{"id":"\([0-9]*\)".*/\1/')
|
recordID=$(echo $recordID | sed 's/.*\[{"id":"\([0-9]*\)".*/\1/')
|
||||||
|
|
||||||
# Update IP
|
# Update IP
|
||||||
myIP=$(arIpAddress)
|
myIP=$(arIpAddress)
|
||||||
recordRS=$(arApiPost "Record.Ddns" "domain_id=${domainID}&record_id=${recordID}&sub_domain=${2}&record_type=A&value=${myIP}&record_line=默认")
|
recordRS=$(arApiPost "Record.Ddns" "domain_id=${domainID}&record_id=${recordID}&sub_domain=${2}&record_type=A&value=${myIP}&record_line=默认")
|
||||||
@@ -210,11 +147,49 @@ arDdnsCheck() {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# DDNS
|
# Get script dir
|
||||||
#echo ${#domains[@]}
|
# See: http://stackoverflow.com/a/29835459/4449544
|
||||||
#for index in ${!domains[@]}; do
|
# Execute the function in a *subshell* to localize variables and the effect of `cd`.
|
||||||
# echo "${domains[index]} ${subdomains[index]}"
|
|
||||||
# arDdnsCheck "${domains[index]}" "${subdomains[index]}"
|
rreadlink() (
|
||||||
#done
|
|
||||||
|
target=$1 fname= targetDir= CDPATH=
|
||||||
|
|
||||||
|
{ \unalias command; \unset -f command; } >/dev/null 2>&1
|
||||||
|
[ -n "$ZSH_VERSION" ] && options[POSIX_BUILTINS]=on # make zsh find *builtins* with `command` too.
|
||||||
|
|
||||||
|
while :; do # Resolve potential symlinks until the ultimate target is found.
|
||||||
|
[ -L "$target" ] || [ -e "$target" ] || { command printf '%s\n' "ERROR: '$target' does not exist." >&2; return 1; }
|
||||||
|
command cd "$(command dirname -- "$target")" # Change to target dir; necessary for correct resolution of target path.
|
||||||
|
fname=$(command basename -- "$target") # Extract filename.
|
||||||
|
[ "$fname" = '/' ] && fname='' # !! curiously, `basename /` returns '/'
|
||||||
|
if [ -L "$fname" ]; then
|
||||||
|
# Extract [next] target path, which may be defined
|
||||||
|
# *relative* to the symlink's own directory.
|
||||||
|
# Note: We parse `ls -l` output to find the symlink target
|
||||||
|
# which is the only POSIX-compliant, albeit somewhat fragile, way.
|
||||||
|
target=$(command ls -l "$fname")
|
||||||
|
target=${target#* -> }
|
||||||
|
continue # Resolve [next] symlink target.
|
||||||
|
fi
|
||||||
|
break # Ultimate target reached.
|
||||||
|
done
|
||||||
|
targetDir=$(command pwd -P) # Get canonical dir. path
|
||||||
|
# Output the ultimate target's canonical path.
|
||||||
|
# Note that we manually resolve paths ending in /. and /.. to make sure we have a normalized path.
|
||||||
|
if [ "$fname" = '.' ]; then
|
||||||
|
command printf '%s\n' "${targetDir%/}"
|
||||||
|
elif [ "$fname" = '..' ]; then
|
||||||
|
# Caveat: something like /var/.. will resolve to /private (assuming /var@ -> /private/var), i.e. the '..' is applied
|
||||||
|
# AFTER canonicalization.
|
||||||
|
command printf '%s\n' "$(command dirname -- "${targetDir}")"
|
||||||
|
else
|
||||||
|
command printf '%s\n' "${targetDir%/}/$fname"
|
||||||
|
fi
|
||||||
|
)
|
||||||
|
|
||||||
|
# Load config file
|
||||||
|
|
||||||
|
DIR=$(dirname -- "$(rreadlink "$0")")
|
||||||
|
|
||||||
. $DIR/dns.conf
|
. $DIR/dns.conf
|
||||||
Reference in new issue
Block a user