由于这个脚本需要Python为2.7版本.所以先讲一下如何升级python
Centos 5.5升级Python到2.7版本
Centos 5.5默认安装的Python版本是2.4。要升级到2.7或者其他版本需要下载源码包自己编译安装
2.7版本下载地址 http://www.python.org/ftp/python/2.7/Python-2.7.tar.bz2
tar jfvx Python-2.7.tar.bz2
./configure
make all
make install
make clean
make distclean
查看安装的版本信息
/usr/local/bin/python2.7 -V
Python 2.7 (r27:82500, Nov 2 2010, 19:25:22)[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
看来新装的版本生效了,做个软连接应该就OK。
mv /usr/bin/python /usr/bin/python-bak
ln -s /usr/local/bin/python3.0 /usr/bin/python
升级安装好以后yum会无法使用,因为yum使用的是2.4版本的,所以要修改yum的配置文件以便能够识别2.4版本下的python
vim /usr/bin/yum
将文件头部的
#!/usr/bin/python
改成
#!/usr/bin/python2.4
就可以继续使用yum了
DNSPOD API 域名的动态IP的更新脚本
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import httplib, urllib
import socket
import time
params = dict(
login_email="xxx@qq.com", # replace with your email
login_password="xxxx", # replace with your password
format="json",
domain_id=776050, # replace with your domain_od, can get it by API Domain.List
record_id=8073592, # replace with your record_id, can get it by API Record.List
sub_domain="www", # replace with your sub_domain
record_line="默认",
)
current_ip = None
def ddns(ip):
params.update(dict(value=ip))
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"}
conn = httplib.HTTPSConnection("dnsapi.cn")
conn.request("POST", "/Record.Ddns", urllib.urlencode(params), headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
print data
conn.close()
return response.status == 200
def getip():
sock = socket.create_connection(('ns1.dnspod.net', 6666))
ip = sock.recv(16)
sock.close()
return ip
if __name__ == '__main__':
while True:
try:
ip = getip()
print ip
if current_ip != ip:
if ddns(ip):
current_ip = ip
except Exception, e:
print e
pass
time.sleep(300)
|
会在后台一直运行,每隔300秒检查一遍IP,如果修改了就更新IP。
转载于:https://blog.51cto.com/wanwentao/696616
获得domain_id可以用curl
curl -k https://dnsapi.cn/Domain.List -d "login_email=xxx&login_password=xxx"
获得record_id类似
curl -k https://dnsapi.cn/Record.List -d "login_email=xxx&login_password=xxx&domain_id=xxx"