DNSPOD Linux动态IP dns客户端

大家都知道ipv4 ip地址是稀缺的资源,我们家用的宽带一般都没有固定的IP,每隔一段时间IP地址又变了。如果你想使用家用电脑来架建网站或其它web服务,你得时不时去更新域名绑定的IP(这里说当然是顶级域名了),不然过一会域名就解析不到正确的主机了。在网上找了相关的教程,发现dnspod的dns解析服务可以解决此问题,参照dnspod的API自己写一个LINUX脚本,再配合cron定时任务来实现定时更新动态IP到dnspod解析服务器。脚本在ubuntu,openwrt和centos上测试正常。

该脚本功能比较单一,只能更新一个主域名,如果需要更多的功能请参考另外一个dnspod脚本

1.首先申请一个免费的dnspod帐号,添加一条记录,记录类型选A,记录值随便填个IP。

2.然后下载附件代码解压到~/bin或/usr/bin/下面,命名为dnspod并加上可执行权限

3.在终端输入dnspod就可以进入初始配置,依次输入dnspod注册邮箱,登录密码,子域名(最好选@),和主域名。也可以手动在~/下面创建一个名为.dnspodrc的配置文件,按照下面的格式手动输入以上的信息:

email=dnspod@zhetenger.com
password=securepasswordprotection (为了不让你的密码赤裸裸的出现在文本中,真实密码两边加上了不相关的字母,把password替换成你的密码就好了)
subdomain=@
domain=zhetenger.com

4.使用cron定时任务每二十分钟更新一次IP crontab -e

×/20 × × × × /path/to/dnspod

代码:

#!/bin/sh
#Authored by Benson Huang
#More details at www.zhetenger.com

#for openwrt only. source the path variable set in /etc/profile
is_openwrt=`uname -a | grep -o OpenWrt`

if [ $is_openwrt ] && [ -f /etc/profile ]

then

. /etc/profile

fi

#import dnspod configuration file(DNSPOD login credentials, domain,subdomain,domain_id and record_id).To prevent the passord visible to anyone who stumble across this file, the password is obscured using a string of meaningless letters and numbers.

if [ -f ~/.dnspodrc ]

then
. ~/.dnspodrc

else

read -p "email:(e.g test@gmail.com)" email
read -p "password:" password
read -p "sub_domain name:(e.g @, www, ftp, etc)" subdomain
read -p "main domain: (e.g zhetenger.com)" domain

echo "$email" | sed 's/.*@.*\.com/email=&/' > ~/.dnspodrc
echo "$password" | sed 's/.*/password=secure&protection/' >> ~/.dnspodrc
echo "$subdomain" | sed 's/.*/subdomain=&/' >> ~/.dnspodrc
echo "$domain" | sed 's/.*/domain=&/' >> ~/.dnspodrc

fi

#parse password from ~/.dnspodrc.

password=$(sed -n '/password/{
s/secure//
s/protection//
s/password=//
p
}' ~/.dnspodrc)

pub_ip_addr=$(curl -s http://checkip.dyndns.com | sed -n 's/.*: \([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\).*/\1/p') #get the current public IP address
cert_file="/etc/ssl/cacert.pem" #specify the certificate file path
dns_url="https://dnsapi.cn" #DNSPOD API URL
data="login_email=$email&login_password=$password&format=json&lang=en&" #assign login credentials and other parameters to the variable data

upload_data () {
curl -s -k -A "dnspod" -d $data$2 $dns_url/$1
}

# if domain_id and record_id have already been set in ~/.dnspodrc, the following codes will be skipped

exist_id=`grep -o 'domain_id=[0-9]*' ~/.dnspodrc`

if [ -z "$exist_id" ]

then
domain_id=`upload_data Domain.Info domain=$domain | sed -rn 's/.*("domain":\{.[^}]+\}).*/\1/g;s/.*"id":"([0-9]+)".*/\1/p'`

record_id=$(upload_data Record.List "domain_id=$domain_id" | sed -rn "s/.*(\"records\":\[.[^]]+\]).*/\1/g;s/.*\{\"id\":\"([0-9]+)\",\"name\":\"@\".*\"type\":\"A\".*/\1/p")

cat >> ~/.dnspodrc domain_id=$domain_id
record_id=$record_id
EOF

fi

#update dns record
upload_data Record.Modify "domain_id=$domain_id&record_id=$record_id&record_type=A&record_line=默认&ttl=600&value=$pub_ip_addr"

注意:复制代码时要保证保存的文件能正确显示‘默认’这个中文值,不然会出现record value invalid的错误。

file: 

添加新评论

个人信息
评论内容
提交该页面即表示您接受Mollon的隐私政策