连上校园网后,发现分配的是公网ip,虽然是动态的,但好歹是个公网的。
于是乎,这不就是服务器吗?装了个宝塔后,真能行。
现在的问题就是,如何动态更新dns解析记录。
于是问了腾讯云客服,学习了相关api,写了如下python脚本:(login_token和domain_id需要自行前往控制台获取)
import requests
import json
from requests.adapters import HTTPAdapters = requests.session()
s.mount('https://', HTTPAdapter(max_retries=5)) # 超时重传次数
login_token = '306877,4ed9987fccf2b2bd4ef3ad9d040'
domain_id = '8331725'def getRecordID():print("获取dns记录ing,可能会卡主")global record_id;url = 'https://dnsapi.cn/Record.List'formdata = {'domain_id': domain_id,'login_token': login_token}try:login = s.post(url=url, data=formdata, timeout=2)except requests.exceptions.RequestException:print("请求超时,请重试")rs = json.loads(login.text) # 转为dictrecord_id = rs['records'][2]['id'];print("获取dns记录完毕")return record_id;def getIP():print("获取iping")url = "http://www.3322.org/dyndns/getip"IPInfo = requests.get(url)IPInfo.encoding = 'UTF-8'ip = IPInfo.textprint("获取ip完毕")return ipdef updateRecord():ip = getIP().strip();getRecordID();print("更改解析ing,可能会卡住")url = 'https://dnsapi.cn/Record.Ddns'formdata = {'domain_id': domain_id,'login_token': login_token,'record_line': '默认','record_line_id': '10=1','value': ip,'record_id': record_id}try:rs = s.post(url=url, data=formdata, timeout=2)except requests.exceptions.RequestException:print("请求超时,请重试!")if (json.loads(rs.text)['status']['code'] == '1'):print(json.loads(rs.text)['status']['message'], '域名解析已更改为' + ip)else:print("操作失败")updateRecord()
获取login_token:ID,Token
获取domain_id:
windows可配合bat脚本实现一键启动
效果:
成功,可以在ip地址变化的时候执行该脚本