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

  • 作者:新网
  • 来源:新网
  • 2018-03-28 01:05:31

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

依赖:

树莓派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 每半个小时执行一次,用来自动检测和更新

免责声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,也不承认相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,请发送邮件至:operations@xinnet.com进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

免费咨询获取折扣