0. 簡單概述
在家裏放一個 NAS 服務器,可是寬帶的 IP 地址常常改變,通常路由器自帶的花生殼域名解析能夠解決,若是路由器沒有相似功能或者想使用本身的域名,能夠嘗試使用 DNSPOD API 來實現域名動態解析。html
1. 添加記錄 在 dnspod 添加域名後並添加一個記錄用來解析寬帶的 IP 地址,如添加一個記錄h
python
2. 獲取信息 1) 域名信息 domain_id
接口文檔:https://www.dnspod.cn/docs/domains.html#domain-infojson
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-listapi
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
dom
#!/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 計劃任務執行curl
#dns record update */1 * * * * root /usr/bin/python /home/debian/dns.py >>/home/debian/dns.log 2>&1
日誌輸出post
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 # 沒有變化,無需改變
檢查是否生效url
$ 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/