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='')
相关推荐
早上好啊! 树哥1 小时前
JavaScript Math(算数) 对象的用法详解
开发语言·javascript·ecmascript
Tomorrow'sThinker2 小时前
25年1月更新。Windows 上搭建 Python 开发环境:PyCharm 安装全攻略(文中有安装包不用官网下载)
ide·python·pycharm
noravinsc2 小时前
requests请求带cookie
开发语言·python·pycharm
风_流沙3 小时前
python pandas 对mysql 一些常见操作
python·mysql·pandas
程序猿(雷霆之王)4 小时前
C++——继承
开发语言·c++
自律小仔4 小时前
桌面开发 的变量声明(Variable Declaration)核心知识
开发语言·后端·golang
qq_273900234 小时前
pytorch torch.scatter_reduce函数介绍
人工智能·pytorch·python
ouyang_ouba4 小时前
pygame飞机大战
开发语言·python·pygame
浮生如梦_5 小时前
C#Halcon跨窗口颜色识别
开发语言·图像处理·计算机视觉·c#·视觉检测
叫我阿呆就好了5 小时前
C 实现植物大战僵尸(三)
c语言·开发语言