Last active
May 1, 2019 00:25
-
-
Save dishuostec/fe561dad1289d82fe777cc640d45313b to your computer and use it in GitHub Desktop.
dnspod ddns
This file contains hidden or 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 | |
################################################# | |
# dnspod ddns v1.0 | |
# Dynamic DNS using DNSPod API | |
# Fork from https://github.com/anrip/ArDNSPod | |
# Modified by dishuostec | |
################################################# | |
# OS Detection | |
case $(uname) in | |
'Linux') | |
echo "Linux" | |
arIpAddress() { | |
local extip | |
local inter="http://ipinfo.io/ip" | |
extip=$(wget --quiet --no-check-certificate --output-document=- $inter) | |
echo $extip | |
} | |
;; | |
'FreeBSD') | |
echo 'FreeBSD' | |
exit 100 | |
;; | |
'WindowsNT') | |
echo "Windows" | |
exit 100 | |
;; | |
'Darwin') | |
echo "Mac" | |
arIpAddress() { | |
ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | |
} | |
;; | |
'SunOS') | |
echo 'Solaris' | |
exit 100 | |
;; | |
'AIX') | |
echo 'AIX' | |
exit 100 | |
;; | |
*) ;; | |
esac | |
# Global Variables: | |
# Token-based Authentication | |
arToken="" | |
# Account-based Authentication | |
arMail="" | |
arPass="" | |
# Get Domain IP | |
# arg: domain | |
arNslookup() { | |
local server="114.114.114.114" | |
local ip | |
ip=$(nslookup ${1} $server | tail -n 1 | sed -n 's/.*\(\(\(^\| \)[0-9]\{1,3\}\.\)\{1\}\([0-9]\{1,3\}\.\)\{2\}[0-9]\{1,3\}\) .*/\1/gp') | |
echo $ip | |
} | |
# Get data | |
# arg: type data | |
arApiPost() { | |
local inter="https://dnsapi.cn/${1:?'Info.Version'}" | |
if [ "x${arToken}" = "x" ]; then # undefine token | |
local param="login_email=${arMail}&login_password=${arPass}&format=json&${2}" | |
else | |
local param="login_token=${arToken}&format=json&${2}" | |
fi | |
wget --quiet --no-check-certificate --output-document=- --post-data $param $inter | |
} | |
# Update | |
# arg: main domain sub domain | |
arDdnsUpdate() { | |
local domainID recordID recordRS recordCD myIP | |
# Get domain ID | |
domainID=$(arApiPost "Domain.Info" "domain=${1}") | |
domainID=$(echo $domainID | sed 's/.*{"id":"\([0-9]*\)".*/\1/') | |
# Get Record ID | |
recordID=$(arApiPost "Record.List" "domain_id=${domainID}&sub_domain=${2}") | |
recordID=$(echo $recordID | sed 's/.*\[{"id":"\([0-9]*\)".*/\1/') | |
# Update IP | |
myIP=$3 | |
recordRS=$(arApiPost "Record.Ddns" "domain_id=${domainID}&record_id=${recordID}&sub_domain=${2}&record_type=A&value=${myIP}&record_line=默认") | |
recordCD=$(echo $recordRS | sed 's/.*{"code":"\([0-9]*\)".*/\1/') | |
# Output IP | |
if [ "$recordCD" = "1" ]; then | |
echo $recordRS | sed 's/.*,"value":"\([0-9\.]*\)".*/\1/' | |
return 1 | |
fi | |
# Echo error message | |
echo $recordRS | sed 's/.*,"message":"\([^"]*\)".*/\1/' | |
} | |
# DDNS Check | |
# Arg: Main Sub | |
arDdnsCheck() { | |
local postRS | |
local hostIP=$(arIpAddress) | |
local lastIP=$(arNslookup "${2}.${1}") | |
echo "hostIP: ${hostIP}" | |
echo "lastIP: ${lastIP}" | |
if [ "$lastIP" != "$hostIP" ]; then | |
postRS=$(arDdnsUpdate $1 $2 $hostIP) | |
echo "postRS: ${postRS}" | |
if [ $? -ne 1 ]; then | |
return 0 | |
fi | |
fi | |
return 1 | |
} | |
# DDNS | |
# For security reasons, it is recommended that you use token-based auth instead | |
# arMail="[email protected]" | |
# arPass="123" | |
# Combine your token ID and token together as follows | |
arToken="00000,00000000000000000000000000000000" | |
# Place each domain you want to check as follows | |
# you can have multiple arDdnsCheck blocks | |
# arDdnsCheck "foo.com" "sub1" | |
# arDdnsCheck "foo.com" "sub2" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment