共计 5251 个字符,预计需要花费 14 分钟才能阅读完成。
几篇参考文章:
1、http://www.anrip.com/post/872
2、http://fengwan.blog.51cto.com/508652/1404534/
源码地址:https://github.com/anrip/ArDNSPod
只是源码只能实现获取本地IP,若是有路由器,仅能获得内网地址。,要向获得外网地址:需要结合两篇文章。
两个文件,放到/usr/bin下面。
第一个文件,ddnspod.sh:
[php]
#!/bin/sh
#################################################
# AnripDdns v5.08
# Dynamic DNS using DNSPod API
# Original by anrip<mail@anrip.com>, http://www.anrip.com/ddnspod
# Edited by ProfFan
#################################################
# OS Detection
case $(uname) in
‘Linux’)
echo "Linux"
arIpAddress() {
local extip
extip=’http://members.3322.org/dyndns/getip’
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
# 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
# arg: domain
arNslookup() {
#wget –quiet –output-document=- $inter$1
echo ""
}
# Get data
# arg: type data
arApiPost() {
local agent="AnripDdns/5.07(ligel@163.com)"
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=- –user-agent=$agent –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=$(arIpAddress)
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)
echo "postRS: ${postRS}"
if [ $? -ne 1 ]; then
return 0
fi
fi
return 1
}
# DDNS
#echo ${#domains[@]}
#for index in ${!domains[@]}; do
# echo "${domains[index]} ${subdomains[index]}"
# arDdnsCheck "${domains[index]}" "${subdomains[index]}"
#done
. $DIR/dns.conf
[/php]
第二个文件,dns.conf:
[php]
# For security reasons, it is recommended that you use token-based auth instead
# arMail="test@gmail.com"
# arPass="123"
# Combine your token ID and token together as follows
arToken="12345,7676f344eaeaea9074c123451234512d"
# Place each domain you want to check as follows
# you can have multiple arDdnsCheck blocks
arDdnsCheck "test.org" "subdomain"
[/php]
增加执行权限
[php]chmod +x /root/ddnspod.sh[/php]
加入任务计划(5分钟执行一次)
[php]echo "*/5 * * * * root /root/ddnspod.sh" >> /etc/crontab[/php]
计划任务执行情况可以查看/var/log/cron
说明:
如果在执行后发现
[php]
[root@localhost ~]# ./ddnspod.sh
./ddns.sh: line 24: nslookup: command not found
hostIP: 114.234.77.222
lastIP:
postRS: 114.234.77.222[/php]
如果你使用的是CentOS则执行
[php]yum -y install bind-utils[/php]
或者是ubuntu的话
[php]sudo apt-get install dnsutils[/php]
当然任何时候都可以用curl命令和crontab来实现动态更新DDNS的ip地址:
1、安装crontab之后为root用户创建文件/var/spool/cron/root
2、创建并配置ddnsupdate.sh,放到/usr/bin/文件下,文件内容(以he.net为例):
Autodetect my IPv4/IPv6 address:
IPV4:curl -4 “http://dyn.example.com:password@dyn.dns.he.net/nic/update?hostname=dyn.example.com”
IPV6:curl -6 “http://dyn.example.com:password@dyn.dns.he.net/nic/update?hostname=dyn.example.com”
3、添加执行权限chomod +x /usr/bin/ddnsupdate.sh
4、编辑root用户的crontab:*/10 * * * * /usr/binddnsupdate.sh,每10分钟执行一次。好了,可以享受你的DDNS了