#!/usr/bin/env python
"""Please replace variables written in Capital Letters before using the script"""
"""By https://skylast.com/archives/7293 """
import syslog
import json
import time
import hashlib
import requests
from time import gmtime
import socket
import fcntl
import struct
""" ignore urlib3 warnings """
#requests.packages.urllib3.disable_warnings()
api_url = "https://www.cloudxns.net/api2/record/RECORD_ID"
api_key = "API_KEY"
api_secret = "API_SECRET"
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
wan_ip = get_ip_address('INTERFACE')
request_data = {
"domain_id": DOMAIN_ID,
"host":"home",
"value": wan_ip,
"type": "A",
"ttl": 600,
"line_id": 1
}
request_time = time.strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime())
hmac = hashlib.md5(api_key+api_url+str(json.dumps(request_data))+request_time+api_secret).hexdigest()
headers = {
'API-KEY' : api_key,
'API-REQUEST-DATE' : request_time,
'API-HMAC' : hmac,
'API-FORMAT' : 'json'
}
try:
r = requests.put(url=api_url,headers=headers,data=json.dumps(request_data),verify=False)
except Exception,ex:
syslog.openlog("xnsCloud", syslog.LOG_PID)
syslog.syslog("Error : " + str(ex))
else:
syslog.openlog("xnsCloud", syslog.LOG_PID)
response = r.json()
response_data = response[u'data']
syslog.syslog("Update_Status : " + response[u'message'])
syslog.syslog("Domain_name : " + response_data[u'domain_name'])
syslog.syslog("IPAddress : " + response_data[u'value'])
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.