cloudns二级免费域名python更新ipv6 dns记录

没找到api,托管到cloudflare也不行。就只能写代码了(只写了更新和添加单条ipv6记录)

需要修改的地方

请求头的cookies填自己的

data里的zone填自己的

python 复制代码
import requests
from lxml import etree 
 
host=''#子域名
cookies='填自己的'
zone='自己域名的'
ipv6 = requests.get('https://6.ipw.cn/').text#获取ipv6地址
print('本机ipv6地址:%s'%ipv6)

get_records_url='https://www.cloudns.net/ajaxPages.php?action=records'
url ='https://www.cloudns.net/ajaxActions.php?action=records'
headers = {
    'cookie':cookies,
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'

}

#获取记录
get_records ={
    "show": "get",
    "zone": zone,
    "type": "all",
    "order-by": "null",
    "page": "1"
}
#修改cname
update_data = {
'show': 'editRecord',
'zone': zone,
'record_id': '',
'settings[host]': '',
'settings[record]': '',
'settings[ttl]': '3600'
}
#添加aaaa
add_ipv6_data = {
    "show": "addRecord",
    "zone": zone,
    "recordType": "AAAA",
    "active": "1",
    "settings[host]": "",
    "settings[record]": "2409:8a62:6421:f881:9d23:59d2:10b:b1d5",
    "settings[ttl]": "3600"
}

response_records_table = requests.post(get_records_url,headers=headers,data=get_records)#获取dns记录表格

# print(response_records.text)

tree = etree.HTML(response_records_table.text)
 
# 测试XPath表达式
record_host_path = '//*[@id="recordsBody"]/tr/td[2]/div/div/text()'#主机
record_type_path = '//*[@id="recordsBody"]/tr/td[3]/text()'#记录类型
record_value_path = '//*[@id="recordsBody"]/tr/td[4]/div/div/text()'#值/指向到
record_id_path = '//*[@id="recordsBody"]/tr/@id'#id
record_host = [host.strip() for host in tree.xpath(record_host_path)]
record_type = [type.strip() for type in tree.xpath(record_type_path)]
record_value= [value.strip() for value in tree.xpath(record_value_path)]
record_id = [id.replace('row-','') for id in tree.xpath(record_id_path)]
# print(record_host)
# print(record_type)
# print(record_value)
# print(record_id)

l= len(record_type)
record_list = {}#存放记录的字典列表
a =0
while a < l:#做成类型对id+host+value的字典列表
    record_list.setdefault(record_type[a],[]).append(record_id[a])
    record_list.setdefault(record_type[a],[]).append(record_host[a])
    record_list.setdefault(record_type[a],[]).append(record_value[a])
    a+=1

# print(record_list)
# print(record_list['AAAA'][0 ])

def update_ipv6(ipv6,record_list,host):  #更新ipv6记录(ipv6你的ipv6地址,host就是子域名留空不加)

    update_data['record_id'] = record_list['AAAA'][0]
    update_data['settings[host]'] = host
    update_data['settings[record]'] = ipv6

    print(update_data)
    response = requests.post(url,headers=headers,data=update_data)
    try:
        if response.json()['status'] == 1:
            print('修改成功')
            print(response.status_code)
        else :
            print(response.text)
            print('失败,状态码错误')
    except:
        print('失败,停止运行')
        exit()

def add_ipv6(ipv6,host): #ipv6你的ipv6地址,host就是子域名留空不加
    add_ipv6_data['recordType']= 'AAAA'
    update_data['settings[host]'] = host
    update_data['settings[record]'] = ipv6
    print(add_ipv6_data)
    response = requests.post(url,headers=headers,data=add_ipv6_data)
    try:
        if response.json()['status'] == 1:
            print('添加成功')
            print(response.status_code)
        else :
            print(response.text)
            print('失败,状态码错误')
    except:
        print('失败,停止运行')
        exit()

if 'AAAA' in record_list:
    print('检测到已有ipv6地址记录')
    update_ipv6(ipv6=ipv6,record_list=record_list,host='hhhhh')
else: 
    print('未检测到ipv6记录')
    add_ipv6(ipv6=ipv6,host='')
相关推荐
用户27784491049932 小时前
借助DeepSeek智能生成测试用例:从提示词到Excel表格的全流程实践
人工智能·python
JavaEdge在掘金4 小时前
ssl.SSLCertVerificationError报错解决方案
python
我不会编程5555 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python
李少兄5 小时前
Unirest:优雅的Java HTTP客户端库
java·开发语言·http
老歌老听老掉牙5 小时前
平面旋转与交线投影夹角计算
python·线性代数·平面·sympy
满怀10155 小时前
Python入门(7):模块
python
无名之逆5 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust
你觉得2055 小时前
哈尔滨工业大学DeepSeek公开课:探索大模型原理、技术与应用从GPT到DeepSeek|附视频与讲义下载方法
大数据·人工智能·python·gpt·学习·机器学习·aigc
似水এ᭄往昔5 小时前
【C语言】文件操作
c语言·开发语言
啊喜拔牙5 小时前
1. hadoop 集群的常用命令
java·大数据·开发语言·python·scala