阿里云DDNS python客户端

最近在家里搞了个存储(就是朋友送了个主板集成,自己搞了个机箱和硬盘,组了组)。将系统装了Centos 6.8,装了apache、samba等等。因为使用的是万网的域名(现阿里云),所以想要实现动态域名解析(缩写ddns,它不同于我们常用的DNS解析,而是专门用于对付那种没有固定公网 IP 的服务器地址解析的。)。而花生壳太不给力,一会一断,还出问题。于是自己在网上搜了搜,已经有人做了客户端这部分的工作(感谢 rfancn ),用 python 2 实现,所以,它是跨平台的。抄录的传送门。

过程如下:

首先从 github 克隆项目到本地:

[root@bb ~]# python -V
Python 2.6.6
[root@bb ~]# cd /opt/
[root@bb opt]# ls
[root@bb opt]# git clone https://github.com/rfancn/aliyun-ddns-client.git
Initialized empty Git repository in /opt/aliyun-ddns-client/.git/
remote: Counting objects: 29, done.
remote: Total 29 (delta 0), reused 0 (delta 0), pack-reused 29
Unpacking objects: 100% (29/29), done.
[root@bb opt]# cd aliyun-ddns-client/
[root@bb aliyun-ddns-client]# ls
config.py  ddns.conf.example  ddns.py  helper.py  README.md  utils.py  yunresolver.py

然后把配置文件拷贝到指定位置并编辑:

[root@bb aliyun-ddns-client]# cp ddns.conf.example /etc/ddns.conf
[root@bb aliyun-ddns-client]# vi /etc/ddns.conf
[DEFAULT]
# Required: access id obtains from aliyun
access_id=此处输入id
# Required: access key obtains from aliyun
access_key=此处输入密码
# Optional: not used now
interval=600
# Optional: turn on debug mode or not
debug=true

[DomainRecord1]
# Required: domain name, like google.com
domain=wlzs.cn
# Required: subdomain name, like www, blog, bbs
sub_domain=blog
# Required: resolve type, 'A', 'MX'...
type=A
# Optional: record id which get from DNS service provider
id=
# Optional: it can be IP address or alias to another hostname...
value=

这里,我们需要获取阿里云的 API 权限,获取方法则是登录阿里云;

  1. 在后台首页顶栏右边找到“Access Keys”;
  2. 通过短信验证之后点击页面右上边按钮创建一个新的 Access Key;
  3. 页面所显示的Access Key ID和 Access Key Secret 则为所需ID和Key。

获取到 API 权限之后,我们再继续来修改配置:

配置中的 interval 无效不用管, [DomainRecord1] 则是你的域名字段,可以改成对应的名称用来区别不同的子域而已,如果你要做多个子域的动态解析,那就写多个字段即可。

针对这个字段里, domain 为你的域名,主域; sub_domain 为要动态解析的子域,记得提前创建好,解析到任意 IP 即可。

比如说,要动态解析 home.logcg.com,那么就按照上文的例子填写。

type 为解析记录,比如 A 记录(例子中的记录);MX 记录等。

最后 id 和 value 留空,让程序自行完成即可。

编辑完成之后,可以执行 Ubuntu:/opt/aliyun-ddns-client# python ddns.py 来运行,第一次运行会得到错误  [ERROR] You must specify domain record id. 没关系,这是由于我们把 id 字段留空的结果,程序会自行获取完成,再执行一次试试看:

[root@bb aliyun-ddns-client]# python ddns.py 
2016-09-06 21:53:26     [ERROR] You must specify domain record id.
2016-09-06 21:53:26     [ERROR] Failed doing the first time sync for record:DomainRecord1
[root@bb aliyun-ddns-client]# python ddns.py 
2016-09-06 22:24:51 [INFO] No changes,skipped...
You have new mail in /var/spool/mail/root

去后台页面刷新看看,解析地址已经更新了。

自动同步

那么,现在我们来利用 crontab来实现域名的动态解析

[root@bb aliyun-ddns-client]# crontab -e
*/1 * * * * cd /opt/aliyun-ddns-client && /usr/bin/python ddns.py && /bin/date >>/opt/a.txt

这句表示每隔一分钟,进入程序目录(我们之前克隆客户端的目录),然后执行程序进行一次对比更新,并且将更新时间进行记录。

至此我们的 blog.wlzs.cn 域名就成了动态解析:v:

 

遇到的问题:

1.ImportError: No module named requests

[root@bb aliyun-ddns-client]# python ddns.py 
Traceback (most recent call last):
  File "ddns.py", line 21, in 
    from helper import DDNSHelper
  File "/opt/aliyun-ddns-client/helper.py", line 20, in 
    from utils import DDNSUtils
  File "/opt/aliyun-ddns-client/utils.py", line 20, in 
    import requests
ImportError: No module named requests

解决方法:
安装pip,通过pip进行安装缺少的模块。

[root@bb ~]# wget -q http://peak.telecommunity.com/dist/ez_setup.py
[root@bb ~]# python ez_setup.py 
Downloading http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg
Processing setuptools-0.6c11-py2.6.egg
Copying setuptools-0.6c11-py2.6.egg to /usr/lib/python2.6/site-packages
Adding setuptools 0.6c11 to easy-install.pth file
Installing easy_install script to /usr/bin
Installing easy_install-2.6 script to /usr/bin

Installed /usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg
Processing dependencies for setuptools==0.6c11
Finished processing dependencies for setuptools==0.6c11
[root@bb ~]# wget --no-check-certificate https://github.com/pypa/pip/archive/1.5.5.tar.gz
--2016-09-06 21:50:46--  https://github.com/pypa/pip/archive/1.5.5.tar.gz
正在解析主机 github.com... 192.30.253.112
正在连接 github.com|192.30.253.112|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Found
位置:https://codeload.github.com/pypa/pip/tar.gz/1.5.5 [跟随至新的 URL]
--2016-09-06 21:50:48--  https://codeload.github.com/pypa/pip/tar.gz/1.5.5
正在解析主机 codeload.github.com... 192.30.253.121
正在连接 codeload.github.com|192.30.253.121|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:未指定 [application/x-gzip]
正在保存至: “1.5.5.tar.gz”

    [                          <=>                                                                 ] 7,864,509   1.36M/s   in 6.8s    

2016-09-06 21:50:56 (1.10 MB/s) - “1.5.5.tar.gz” 已保存 [7864509]

[root@bb ~]# tar -xvzf 1.5.5.tar.gz 
[root@bb ~]# cd pip-1.5.5/
[root@bb pip-1.5.5]# python setup.py install
[root@bb pip-1.5.5]# pip install requests
Downloading/unpacking requests
  Downloading requests-2.11.1-py2.py3-none-any.whl (514kB): 514kB downloaded
Installing collected packages: requests
Successfully installed requests
Cleaning up...

2.先安装ez_setup.py,然后再安装pip,否则会提示以下问题。

[root@bb pip-1.5.5]# python setup.py install
Traceback (most recent call last):
  File "setup.py", line 6, in 
    from setuptools import setup, find_packages
ImportError: No module named setuptools

3.Http Status Code:400

[root@bb aliyun-ddns-client]# python ddns.py    
Http Status Code:400
{"Code":"InvalidParameter","HostId":"dns.aliyuncs.com","Message":"The specified value of parameter \"Value[the same as old]\" is not valid","RequestId":"98173A34-CFA5-4E01-A06C-27019CE00641"}
2016-09-06 21:55:25     [ERROR] Failed doing the first time sync for record:DomainRecord1

解决方法:

此问题好像是因为程序更新的地址与已经解析的地址有冲突,只要阿里云里已经解析的地址和将要更新的地址不相同就可以。

二〇一六年九月六日 22:27:37

THE END