新网Logo
首页>域名资讯>

【树莓派】python3脚本 自动上报ip到万网域名解析

登录 注册

【树莓派】python3脚本 自动上报ip到万网域名解析

  • 来源:新网
  • 更新日期:2018-03-28

摘要:依赖: 树莓派3b 万网域名(需要申请APIkey) python3 模块: requests aliyun-python-sdk-core-v3 aliyun-python-sdk-alidns 脚本如下: import requests import json from aliyunsdkcore.client import AcsClient from aliyunsdkcore.

依赖:

树莓派3b 万网域名(需要申请APIkey)

python3 模块:

1 (47).jpg

requests aliyun-python-sdk-core-v3 aliyun-python-sdk-alidns

脚本如下:

import requests import json from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkalidns.request.v20150109 import UpdateDomainRecordRequest, DescribeDomainRecordInfoRequest DOMAIN = \'*.com\' RID = \'该域名的rid\' client = AcsClient( "appkey", "appsecret", "cn-hangzhou" ) def ip(): #自己写的获取源 IP服务 r = requests.get(\'http://*.*.com/get/ip\') if r.status_code == 200: return r.text else: return None def update_record(cip, old): update_pi(cip) req = UpdateDomainRecordRequest.UpdateDomainRecordRequest() req.set_RecordId(RID) req.set_RR(old[\'RR\']) req.set_Type(old[\'Type\']) req.set_Value(cip) resp = client.do_action_with_exception(req) print(resp) return True def update_pi(cip): postdata = {\'ip\': ip} #自己写的记录IP变化的web服务 r = requests.post(\'http://*.*.com/pi/ip\', data=postdata) return r.text == \'ok\' def connect_dns(): cip = ip() if not cip: return False req = DescribeDomainRecordInfoRequest.DescribeDomainRecordInfoRequest() req.set_RecordId(RID) resp = client.do_action_with_exception(req).decode(\'utf-8\') old = json.loads(resp) if cip == old[\'Value\']: return True else: return update_record(cip, old) if __name__ == \'__main__\': connect_dns()

然后放置在crontab 每半个小时执行一次,用来自动检测和更新