使用 DNSPOD API 实现域名动态解析
0. 简单概述
在家里放一个 NAS 服务器,但是宽带的 IP 地址经常改变,一般路由器自带的花生壳域名解析可以解决,如果路由器没有类似功能或者想使用自己的域名,可以尝试使用 DNSPOD API 来实现域名动态解析。
1. 添加记录 在 dnspod 添加域名后并添加一个记录用来解析宽带的 IP 地址,如添加一个记录h 
2. 获取信息 1) 域名信息 domain_id 接口文档:https://www.dnspod.cn/docs/domains.html#domain-info
def domain_info(domain):
url = "https://dnsapi.cn/Domain.Info"
data = {
"login_email": LOGIN_EMAIL,
"login_password": LOGIN_PASSWORD,
"format": FORMAT,
"domain": domain
}
r = requests.post(url, data=data, timeout=5)
return r.json()["domain"]
def domain_id(domain):
info = domain_info(domain)
return info["id"]
# 获取 domain_id
print domain_id("greak.net")
2) 记录信息 record_id、record_line和value 接口文档:https://www.dnspod.cn/docs/records.html#record-list
def record_info(domain, sub_domain):
url = "https://dnsapi.cn/Record.List"
data = {
"login_email": LOGIN_EMAIL,
"login_password": LOGIN_PASSWORD,
"format": FORMAT,
"domain": domain,
"sub_domain": sub_domain
}
r = requests.post(url, data=data, timeout=5)
return r.json()["records"][0]
def record_data(domain, sub_domain):
info = record_info(domain, sub_domain)
return info["id"], info["line"], info["value"]
# 获取 record_id, record_line, value
rid, line, oldip = record_data("greak.net", "h")
3) 获取最新 IP 地址 接口:http://greak.net/ip
def get_newip():
url = "http://greak.net/ip"
r = requests.get(url, timeout=5)
return r.content.strip()
# 获取 IP 地址
print get_newip()
3. 修改记录 接口文档:https://www.dnspod.cn/docs/records.html#dns
def record_update(domain, sub_domain):
newip = get_newip()
rid, line, oldip = record_data(domain, sub_domain)
if newip == oldip: # 比较新 IP 和现有记录是否一致,如果一致则不需要更新
return "not change"
url = "https://dnsapi.cn/Record.Ddns"
data = {
"login_email": LOGIN_EMAIL,
"login_password": LOGIN_PASSWORD,
"format": FORMAT,
"domain_id": domain_id(domain),
"record_id": rid,
"sub_domain": sub_domain,
"record_line": line,
"value": newip
}
r = requests.post(url, data=data, timeout=5)
return r.json()
# 修改记录
print record_update("greak.net", "h")
4. 定期执行 完整 python 脚本dns.py
#!/usr/bin/env python2
import sys
import os
import json
import time
import requests
LOGIN_EMAIL = "xxxxxx@example.com"
LOGIN_PASSWORD = "xxxxxx"
FORMAT = "json"
def domain_info(domain):
url = "https://dnsapi.cn/Domain.Info"
data = {
"login_email": LOGIN_EMAIL,
"login_password": LOGIN_PASSWORD,
"format": FORMAT,
"domain": domain
}
r = requests.post(url, data=data, timeout=5)
return r.json()["domain"]
def domain_id(domain):
info = domain_info(domain)
return info["id"]
def record_info(domain, sub_domain):
url = "https://dnsapi.cn/Record.List"
data = {
"login_email": LOGIN_EMAIL,
"login_password": LOGIN_PASSWORD,
"format": FORMAT,
"domain": domain,
"sub_domain": sub_domain
}
r = requests.post(url, data=data, timeout=5)
return r.json()["records"][0]
def record_id(domain, sub_domain):
info = record_info(domain, sub_domain)
return info["id"]
def record_line(domain, sub_domain):
info = record_info(domain, sub_domain)
return info["line"]
def record_value(domain, sub_domain):
info = record_info(domain, sub_domain)
return info["value"]
def record_data(domain, sub_domain):
info = record_info(domain, sub_domain)
return info["id"], info["line"], info["value"]
def get_newip():
url = "http://greak.net/ip"
r = requests.get(url, timeout=5)
return r.content.strip()
def record_update(domain, sub_domain):
newip = get_newip()
rid, line, oldip = record_data(domain, sub_domain)
if newip == oldip:
return "not change"
url = "https://dnsapi.cn/Record.Ddns"
data = {
"login_email": LOGIN_EMAIL,
"login_password": LOGIN_PASSWORD,
"format": FORMAT,
"domain_id": domain_id(domain),
"record_id": rid,
"sub_domain": sub_domain,
"record_line": line,
"value": newip
}
r = requests.post(url, data=data, timeout=5)
return r.json()
if __name__ == "__main__":
print time.strftime("%Y/%m/%d %H:%M:%S")
print record_update("greak.net", "h")
添加 cron 计划任务执行
#dns record update
*/1 * * * * root /usr/bin/python /home/debian/dns.py >>/home/debian/dns.log 2>&1
日志输出
2016/05/01 18:17:02
{u'status': {u'message': u'Action completed successful', u'code': u'1', u'created_at': u'2016-05-01 18:17:03'}, u'record': {u'id': 178422498, u'value': u'180.172.158.222', u'name': u'h'}}
# 更新成功
2016/05/01 18:18:02
not change
# 没有变化,无需改变
检查是否生效
$ curl greak.net/ip
180.172.158.222
$ dig h.greak.net | grep "^h.greak.net" | awk '{print $NF}'
180.172.158.222
如果输出的 IP 地址一样就表示生效了,由于存在 dns 缓存, dns 生效需要大概几分钟
注: dnspod 的 api 调用有次数限制,不要频繁调用
原文地址:http://greak.net/2016/05/01/shi-yong-dnspod-apishi-xian-yu-ming-dong-tai-jie-xi/
使用 DNSPOD API 实现域名动态解析的更多相关文章
- 通过阿里云域名动态解析 IP 地址
这两天在家里用树莓派折腾了一个家用服务器,主要用来做 mac 的 Time Machine ,还有就是当做下载机和 nas ,想着平时上班时间家里没人用网络,空着也是空着,就可以利用空闲带宽下个美剧啥 ...
- 域名动态解析到动态IP
一般宽带用户的IP都是动态IP,重连之后IP可能会发生变化. 如果想在其他地方连接家里的设备,或者在家中搭建服务器,就会受到影响. 现在提供一种动态解析域名的方式,只要检测到IP的变化,那么就调用阿里 ...
- openresty域名动态解析
工作中使用openresty,使用第三方服务API通过域名访问.但是,域名通过DNS解析出来之后,在openresty是有 配置解析阶段 很多时候我们会在 Nginx 配置文件里配置上一些域名,比如配 ...
- 利用Dnspod api批量更新添加DNS解析【python脚本】 - 推酷
利用Dnspod api批量更新添加DNS解析[python脚本] - 推酷 undefined
- Java动态解析域名
Java动态解析域名 Java提供InetAddress类,可以对域名-IP进行正向.逆向解析. InetAddress解析的时候一般是调用系统自带的DNS程序. linux 默认的DNS方式是读取/ ...
- 理解AngularJS生命周期:利用ng-repeat动态解析自定义directive
ng-repeat是AngularJS中一个非常重要和有意思的directive,常见的用法之一是将某种自定义directive和ng-repeat一起使用,循环地来渲染开发者所需要的组件.比如现在有 ...
- Protobuf动态解析在Java中的应用 包含例子程序
最近在做ProtoBuf相关的项目,其中用到了动态解析,网上看了下相关资料和博文都比较少,自己来写一个记录一下学习过程. Protocol Buffers是结构化数据格式标准,提供序列化和反序列方 ...
- WordPress搭建教程---购买域名+购买VPS主机+域名DNS解析+网站环境+上传网站程序
WordPress搭建教程 购买域名---NameSilo 购买VPS主机---Vultr 域名DNS解析 网站环境 上传网站程序 参考文章: 1. WordPress搭建教程 https://zhu ...
- 加速你的网络!软路由构建 去AD+国内域名加速解析+抗污染+速度优选 与PSW无缝集成 综合方案
本方案利用OpenWrt搭建4级DNS,实现 去AD + 国内域名加速解析 + 抗污染(域名解析按地区分流)+ 访问速度优选. 方案涉及部分软件配置细节可以参照之前博文:https://www.cnb ...
随机推荐
- iOS开发个人独立博客收集
如今国内技术博客站点有非常多,如CSDN,CNBlog,ITEye等.论坛的话主要是要cocachina. 这里是我收集的iOS开发个人独立博客,文章用搜索引擎比較难搜到,都是牛人: OneV's D ...
- vue2.X 组件通信($emit $on props)
1.index.html 子组件直接修改父组件的数据 组件通讯: vm.$emit(); vm.$on(); 父组件和子组件: 子组件想要拿到父组件数据: 通过 props 之前,子组件可以更改父组 ...
- 地图之CLLocationManager的使用 定位功能使用
1.iOS8曾经使用CLLocationManager 1.导入头文件 <CoreLocation/CoreLocation.h> 2.创建位置管理者 CLLocationManager ...
- 一个JavaScript Function Outliner插件 第三个版本 让你的JavaScript代码也支持折叠
下面我只以英文的vs2008版本作为实例,演示一下打开vs2008 然后一次点击:Tools->Options (工具->选项)会弹出选项设置框在左边的树目录里展开Environment- ...
- JS图片预加载插件
在开发H5项目中有时候会遇到要加载大量图片的情况,利用预加载技术可以提高用户浏览时的体验. 1)概念:懒加载也叫延迟加载:JS图片延迟加载,延迟加载图片或符合某些条件时才加载某些图片.预加载:提前加载 ...
- C# Excel
using System.IO;using System.Text;namespace iLIS.Common{ ///<summary> ///生成Excel文档内容 /// 存入工作流 ...
- 在 Ubuntu16.04上安装并使用Docker
介绍 Docker是一个开放源代码软件项目,让应用程序布署在软件容器下的工作可以自动化进行,借此在Linux操作系统上,提供一个额外的软件抽象层,以及操作系统层虚拟化的自动管理机制[1].Docker ...
- SQL注入基础入门
一般的WEB架构 SQL注入成因: 用户开启浏览器并连接http://www.xxx.com.位于逻辑层的Web服务器从文件系统中加载脚本将其传递给脚本引擎,脚本引擎负责解析并执行脚本. 脚本使用数据 ...
- Nginx与Apache的Rewrite规则的区别
一.Nginx Rewrite规则相关指令 Nginx Rewrite规则相关指令有if.rewrite.set.return.break等,其中rewrite是最关键的指令.一个简单的Nginx R ...
- 1-2:CSS3课程入门之结构选择
E:nth-child(n) 表示E父元素中的第n个字节点 p:nth-child(odd){background:red}/*匹配奇数行*/ p:nth-child(even){background ...