cloudflare批量添加/删除一个IP段的记录
增加
    import requests
    import time
    
    # Cloudflare配置
    API_KEY = "your_api_key"
    EMAIL = "your_email@example.com"
    ZONE_ID = "your_zone_id"
    HOSTNAME = "nodes.example.com"  # 统一使用这个主机名
    
    headers = {
        "X-Auth-Email": EMAIL,
        "X-Auth-Key": API_KEY,
        "Content-Type": "application/json"
    }
    
    def add_dns_record(ip):
        url = f"https://api.cloudflare.com/client/v4/zones/{ZONE_ID}/dns_records"
        data = {
            "type": "A",
            "name": HOSTNAME,  # 所有记录使用相同主机名
            "content": ip,
            "ttl": 1,
            "proxied": False  # 代理模式下多A记录可能有特殊行为
        }
        
        response = requests.post(url, json=data, headers=headers)
        if response.status_code == 200:
            print(f"成功添加: {HOSTNAME} -> {ip}")
            return True
        else:
            print(f"添加失败[{response.status_code}]: {ip}")
            print(response.json())
            return False
    
    # 批量添加254个IP到同一主机名
    for i in range(1, 255):
        ip = f"172.64.229.{i}"
        
        if not add_dns_record(ip):
            print("遇到错误,终止操作")
            break
        
        # 每5个请求暂停1秒防止速率限制
        if i % 5 == 0:
            time.sleep(1)
    
    print("批量添加操作完成")

删除

    import requests
    import time
    
    # Cloudflare配置
    API_KEY = "your_api_key"
    EMAIL = "your_email@example.com"
    ZONE_ID = "your_zone_id"
    HOSTNAME = "nodes.example.com"  # 目标主机名
    
    headers = {
        "X-Auth-Email": EMAIL,
        "X-Auth-Key": API_KEY,
        "Content-Type": "application/json"
    }
    
    def list_dns_records():
        url = f"https://api.cloudflare.com/client/v4/zones/{ZONE_ID}/dns_records"
        params = {
            "type": "A",
            "name": HOSTNAME,
            "per_page": 100
        }
        response = requests.get(url, headers=headers, params=params)
        if response.status_code == 200:
            return response.json().get("result", [])
        else:
            print("查询记录失败")
            print(response.json())
            return []
    
    def delete_dns_record(record_id, ip):
        url = f"https://api.cloudflare.com/client/v4/zones/{ZONE_ID}/dns_records/{record_id}"
        response = requests.delete(url, headers=headers)
        if response.status_code == 200:
            print(f"删除成功: {HOSTNAME} -> {ip}")
            return True
        else:
            print(f"删除失败: {HOSTNAME} -> {ip}")
            print(response.json())
            return False
    
    # 批量删除
    records = list_dns_records()
    print(f"共找到 {len(records)} 条 A 记录,准备删除...")
    for i, record in enumerate(records, 1):
        record_id = record["id"]
        ip = record["content"]
        delete_dns_record(record_id, ip)
    
        # 防止触发速率限制
        if i % 5 == 0:
            time.sleep(1)
    
    print("批量删除操作完成")

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇