def update_dns(ip):
url = fhttps://www.duckdns.org/update/{DOMAIN}/{ip}?token={TOKEN}
response = requests.get(url)
if OK in response.text:
print(fDNS更新成功: {ip})
else:
print(DNS更新失败)
if __name__ == __main__:
last_ip =
while True:
current_ip = get_public_ip()
if current_ip != last_ip:
update_dns(current_ip)
last_ip = current_ip
time.sleep(300) 每5分钟检查一次
Shell脚本(以Cloudflare为例)
bash
!/bin/bash
配置信息
ZONE=65.hk
RECORD=home
TOKEN=cloudflare_api_token
IP=$(curl s https://api.ipify.org)
获取记录ID
RECORD_ID=$(curl s X GET https://api.cloudflare.com/client/v4/zones/$(curl s X GET https://api.cloudflare.com/client/v4/zones?name=$ZONE | jq r '.result[0].id')/dns_records?type=A&name=$RECORD.$ZONE | jq r '.result[0].id')
更新记录
curl s X PUT https://api.cloudflare.com/client/v4/zones/$(curl s X GET https://api.cloudflare.com/client/v4/zones?name=$ZONE | jq r '.result[0].id')/dns_records/$RECORD_ID \
H Authorization: Bearer $TOKEN \
H ContentType: application/json \
data {\type\:\A\,\name\:\$RECORD.$ZONE\,\content\:\$IP\,\ttl\:120,\proxied\:false}