Skip to content

Instantly share code, notes, and snippets.

@loveyu
Created October 13, 2014 15:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loveyu/2715a7469927b49a50f3 to your computer and use it in GitHub Desktop.
Save loveyu/2715a7469927b49a50f3 to your computer and use it in GitHub Desktop.
纯 Shell 实现DNSPOD 域名动态更新
#!/bin/bash
#########################################
# author : loveyu #
# url : http://www.loveyu.org #
#########################################
function post()
{
#提交域名更新请求
if [ "" = "$1" ]
then
log "call error"
return 0
fi
#通过CURL 操作,如果手动指定IP添加 value=$1
###########################################
#
# 替换中括号中的内容,包含中括号
# [you email] 你的登录邮箱
# [you password] 你的密码
# [you domain_id] .....
# [you record_id ..... 详见 https://www.dnspod.cn/docs/records.html#dns
# [you sub_doamian] ....
#############################################
result=`curl -s POST https://dnsapi.cn/Record.Ddns -d 'login_email=[you email]&login_password=[you password]&format=json&domain_id=[you domain_id]&record_id=[you record_id]&record_line=默认&sub_domain=[you sub_doamian]'`
log $1
log $result
}
function log()
{
#输出一个记录值
echo -n `date +'[%Y-%m-%d %H:%M:%S]'`
echo " $1"
}
function get_ip(){
#获取外网IP地址
#花生壳的服务
curl -s http://members.3322.org/dyndns/getip
}
function d_lookup() {
if [ "" = "$1" ]
then
log "call error"
echo "nslookup error"
exit
fi
#域名解析地址查询
nslookup $1 | sed -n -e '4,$p' | awk '/Address/{print $2}'
}
ip=$(get_ip) #获取外网IP
#############################################################
#
# 请将 [you domain] 替换为你的域名 如 loveyu.org ,无括号
#
#############################################################
new_ip=$(d_lookup [you domain]) #查询当前域名IP
#如果需要初始化时提交一次,可以直接使用 post $ip,然后把下面的注释掉
if [ "$ip" != "$new_ip" ]
then
post $ip #初始DNS查询不相等时提交数据
fi
while :
do
sleep 30
new_ip=$(get_ip)
if [ "$ip" != "$new_ip" ]
then
#30秒的轮询数据查询
post $new_ip
ip=$new_ip
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment