由於這個腳本需要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="[email protected]", # 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。
獲得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"