0

使用DNSPOD的API实现动态域名

 2 years ago
source link: https://blogread.cn/it/article/8270?f=hot1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用DNSPOD的API实现动态域名

浏览:468次  出处信息

0. 你得有一个 dnspod 帐号,并且把你的域名(例如 test.com )解析迁移过去(略)

1. 添加一个子域名的 A 记录,例如 ddns.test.com 指向 127.0.0.1

$ export domain=test.com
 $ export subdomain=ddns

2. 生成一个token:参考官方说明 https://support.dnspod.cn/Kb/showarticle/tsid/227/

【务必注意】需要用生成的 ID 和 Token 这两个字段来组合成一个完整的 Token,组合方式为:"ID,Token"(用英文半角逗号分割),比如官方示例中,完整的 Token 为:13490,6b5976c68aba5b14a0558b77c17c3932 。

$ export token=13490,6b5976c68aba5b14a0558b77c17c3932

3. 获取必要信息: 域名和子域名的ID

$ curl -X POST https://dnsapi.cn/Record.List -d "login_token=${token}&format=json&domain=${domain}&sub_domain=${subdomain}"

返回结果为:{"status":{...}, "domain":{"id":640001, "name":"test.com", ...}, "info":{...}, "records":[{"id":"355300007", "name":"ddns", ...}]}

记录下对应域名的id 和子域名的id

$ export domain_id=640001
 $ export subdomain_id=355300007

4. 获取外网ip

$ wanip=`nc ns1.dnspod.net 6666`

5. 更新记录

$ curl https://dnsapi.cn/Record.Ddns -d "login_token=${token}&format=json&domain_id=$domain_id&record_id=$record_id&sub_domain=$sub_domain&record_line=默认&value=$wanip"

= 完 =

(其实没完)其中 1、2、3 做完以后

6. 把 4、5 可以写到一个脚本里

$ vi dnspod.sh

#!/bin/bash

domain_id=640001
record_id=355300007
sub_domain=ddns

wanip=`nc ns1.dnspod.net 6666`
curl https://dnsapi.cn/Record.Ddns -d "login_token=${token}&format=json&domain_id=$domain_id&record_id=$record_id&sub_domain=$sub_domain&record_line=默认&value=$wanip"
7. 设置 crontab

$ crontab -e
*/15 * * * * sh /path/to/dnspod.sh

建议继续学习:

QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK