CentOS通过DNSpod实现动态域名

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介:

因为家里使用ADSL动态IP上网,有时在外面需要使用家里资源,原本使用的是TP-LINK自带的花生壳解析,用过之后发现很不稳定。因为自己注册了域名也已使用了DNSPOD解析。

1.新建脚本文件

1
vim ddns.sh

文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/sh
                                                                                                                                                                                                                                                                      
#################################################
# AnripDdns v3.08.09
# 基于DNSPod用户API实现的动态域名客户端
# 作者: 若海[mail@anrip.com]
# 介绍: http://www.anrip.com/ddnspod
# 时间: 2013-08-08 23:25:00
#################################################
                                                                                                                                                                                                                                                                      
# 全局变量表
arPass=arMail= ""
                                                                                                                                                                                                                                                                      
# 获得外网地址
arIpAdress() {
     local  inter= "http://members.3322.org/dyndns/getip"
     wget --quiet --no-check-certificate --output-document=- $inter
}
                                                                                                                                                                                                                                                                      
# 查询域名地址
# 参数: 待查询域名
arNslookup() {
     local  dnsvr= "114.114.114.114"
     nslookup  ${1} $dnsvr |  tr  -d  '\n[:blank:]'  sed  's/.\+1 \([0-9\.]\+\)/\1/'
}
                                                                                                                                                                                                                                                                      
# 读取接口数据
# 参数: 接口类型 待提交数据
arApiPost() {
     local  agent= "AnripDdns/3.08(mail@anrip.com)"
     local  inter= "https://dnsapi.cn/${1:?'Info.Version'}"
     local  param= "login_email=${arMail}&login_password=${arPass}&format=json&${2}"
     wget --quiet --no-check-certificate --output-document=- --user-agent=$agent --post-data $param $inter
}
                                                                                                                                                                                                                                                                      
# 更新记录信息
# 参数: 主域名 子域名
arDdnsUpdate() {
     local  domainID recordID recordRS recordCD
     # 获得域名ID
     domainID=$(arApiPost  "Domain.Info"  "domain=${1}" )
     domainID=$( echo  $domainID |  sed  's/.\+{"id":"\([0-9]\+\)".\+/\1/' )
     # 获得记录ID
     recordID=$(arApiPost  "Record.List"  "domain_id=${domainID}&sub_domain=${2}" )
     recordID=$( echo  $recordID |  sed  's/.\+\[{"id":"\([0-9]\+\)".\+/\1/' )
     # 更新记录IP
     recordRS=$(arApiPost  "Record.Ddns"  "domain_id=${domainID}&record_id=${recordID}&sub_domain=${2}&record_line=默认" )
     recordCD=$( echo  $recordRS |  sed  's/.\+{"code":"\([0-9]\+\)".\+/\1/' )
     # 输出记录IP
     if  "$recordCD"  ==  "1"  ];  then
         echo  $recordRS |  sed  's/.\+,"value":"\([0-9\.]\+\)".\+/\1/'
         return  1
     fi
     # 输出错误信息
     echo  $recordRS |  sed  's/.\+,"message":"\([^"]\+\)".\+/\1/'
}
# 动态检查更新
# 参数: 主域名 子域名
arDdnsCheck() {
     local  postRS
     local  hostIP=$(arIpAdress)
     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
}
                                                                                                                                                                                                                                                                      
###################################################
                                                                                                                                                                                                                                                                      
# 设置用户参数
arMail= "user@anrip.com"
arPass= "anrip.net"
                                                                                                                                                                                                                                                                      
# 检查更新域名
arDdnsCheck  "anrip.com"  "lab"
arDdnsCheck  "anrip.net"  "lab"

ps:以上代码来自http://www.anrip.com/post/872

我们只需要将上面的

# 设置用户参数
arMail = "user@anrip.com"     //DNSPOD用户名
arPass = "anrip.net"                //DNSPOD密码
# 检查更新域名
arDdnsCheck "anrip.com"   "lab"     //需要更新的域名为anrip.com,主机为lab,就是lab.anrip.com
arDdnsCheck "anrip.net"   "lab"      //更新lab.anrip.net

修改成自己的信息即可


3.增加执行权限

1
chmod  +x  /root/ddns .sh


4.加入任务计划(5分钟执行一次)

1
echo  "*/5  * * * * root /root/ddns.sh"  >>  /etc/crontab


计划任务执行情况可以查看/var/log/cron

说明:

如果在执行后发现

1
2
3
4
5
[root@localhost ~] # ./ddns.sh
. /ddns .sh: line 24:  nslookup command  not found
hostIP: 114.234.77.222
lastIP:
postRS: 114.234.77.222

如果你使用的是CentOS则执行

1
yum -y  install  bind-utils

或者是ubuntu的话

1
sudo  apt-get  install  dnsutils



本文转自 rong341233 51CTO博客,原文链接:http://blog.51cto.com/fengwan/1404534
相关文章
|
域名解析 小程序 Linux
CentOS 域名配置解析(购买-备案-配置-访问,二级域名配置)
CentOS 域名配置解析(购买-备案-配置-访问,二级域名配置)
432 0
|
网络协议 Linux 应用服务中间件
2022红帽企业版网络配置--centos7配置DHCP DNS绑定域名 FTP HTTP(apache) nginx samba
2022红帽企业版网络配置--centos7配置DHCP DNS绑定域名 FTP HTTP(apache) nginx samba
220 0
|
域名解析 缓存 网络协议
CentOS-7.2部署DNS域名解析服务器并进行相关配置测试
CentOS-7.2部署DNS域名解析服务器并进行相关配置测试
563 0
CentOS-7.2部署DNS域名解析服务器并进行相关配置测试
|
6月前
|
XML Linux 应用服务中间件
centos7搭建minio并实现分享路径为域名路径
centos7搭建minio并实现分享路径为域名路径
538 0
|
域名解析 小程序 Linux
CentOS搭建内网穿透frp并绑定自定义域名
CentOS搭建内网穿透frp并绑定自定义域名
2469 2
|
域名解析 缓存 网络协议
【Linux网络服务】Centos7搭建DNS域名解析服务
【Linux网络服务】Centos7搭建DNS域名解析服务
|
Linux 数据库
有域名了第一步,阿里云centos宝塔面板安装WordPress个人博客
有域名了第一步,阿里云centos宝塔面板安装WordPress个人博客
有域名了第一步,阿里云centos宝塔面板安装WordPress个人博客
|
安全 NoSQL 应用服务中间件
使用FastDFS在CentOS 7.6.1810中进行域名访问和添加HTTPS安全协议
FastDFS分布式文件存储配置域名和HTTPS协议
882 0
使用FastDFS在CentOS 7.6.1810中进行域名访问和添加HTTPS安全协议