1:批量添加域名
获取Clobal key
保存为add_domain.sh
创建domain.txt 域名存放格式为一行一个根域名
#!/bin/bash
export CF_API_EMAIL=xxx
export CF_API_KEY=xxxx
for domain in $(cat domain.txt); do \
curl -X POST -H "X-Auth-Key: $CF_API_KEY" -H "X-Auth-Email: $CF_API_EMAIL" \
-H "Content-Type: application/json" \
"https://api.cloudflare.com/client/v4/zones" \
--data '{"name":"'$domain'","jump_start":true}'; done
2.批量添加域名解析
保存为add_dns_records.py
创建domains.txt 格式为xxx.com A @ 1.2.3.4 一行一个
import requests
# Cloudflare API 配置
api_email = "xxxx" # 替换为你的 Cloudflare 邮箱
api_key = "xxx" # 替换为你的 Global API Key
api_url = "https://api.cloudflare.com/client/v4/zones"
# 设置请求头
headers = {
"X-Auth-Email": api_email,
"X-Auth-Key": api_key,
"Content-Type": "application/json"
}
# 从文件中读取域名和DNS记录
def read_dns_records(file_path):
records = []
with open(file_path, 'r') as file:
for line in file:
# 读取每一行并拆分为域名、记录类型、主机头、解析值
parts = line.strip().split()
if len(parts) == 4:
domain, record_type, host, value = parts
records.append((domain, record_type, host, value))
else:
print(f"格式错误: {line}")
return records
# 获取 Zone ID(始终获取根域名的 Zone ID)
def get_zone_id(domain):
root_domain = domain.strip() # 去掉域名周围的空格
print(f"查询 Zone ID: {root_domain}") # 打印调试信息
# 发起 API 请求
response = requests.get(f"{api_url}?name={root_domain}", headers=headers)
if response.status_code == 200:
data = response.json()
if data["result"]:
print(f"找到 Zone ID: {root_domain} 的 Zone ID")
return data["result"][0]["id"]
else:
print(f"未找到 {domain} 的 Zone ID: {data}") # 打印详细错误信息
return None
else:
print(f"查询 {domain} 时发生错误: {response.status_code} - {response.text}")
return None
# 检查是否已存在相同的 DNS 记录
def record_exists(zone_id, domain, record_type, host, value):
dns_url = f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records"
response = requests.get(dns_url, headers=headers, params={"name": host, "type": record_type})
if response.status_code == 200:
data = response.json()
for record in data["result"]:
if record["content"] == value:
return True
else:
print(f"查询 {domain} 的 DNS 记录时发生错误: {response.status_code}")
return False
# 添加 DNS 记录
def add_dns_record(zone_id, domain, record_type, host, value):
# 根域名的主机头 @ 可能需要完全域名替代
if host == "@":
host = domain # 使用完全域名替代 @
# 清理主机头,去除首尾空格
host = host.strip()
# 清理值,去除首尾空格
value = value.strip()
# 检查是否已存在相同的 DNS 记录
if record_exists(zone_id, domain, record_type, host, value):
print(f"记录已存在:{domain} {record_type} {host} {value},跳过添加。")
return
dns_url = f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records"
# 构建记录数据
data = {
"type": record_type, # 记录类型:A, CNAME, TXT 等
"name": host, # 主机头
"content": value, # 记录值
"ttl": 1, # TTL(生存时间)
"proxied": False # 不使用 Cloudflare 的代理服务
}
# 如果是 CNAME 记录,确保值的格式是有效的(不以 http:// 或 https:// 开头)
if record_type == "CNAME" and value.startswith("http"):
value = value.split("//")[1]
# 对 TXT 记录,确保值用引号包围(如果没有的话)
if record_type == "TXT" and not value.startswith('"'):
value = f'"{value}"'
# 发送请求添加 DNS 记录
response = requests.post(dns_url, json=data, headers=headers)
if response.status_code == 200:
print(f"成功为 {domain} 添加 {record_type} 记录,主机头 {host} 指向 {value}")
else:
print(f"为 {domain} 添加 {record_type} 记录失败: {response.text}")
# 主函数
def main():
# 读取文件中的域名和 DNS 记录
file_path = "domains.txt" # 替换为你的文件路径 格式为xxx.com A @ 1.2.3.4 一行一个
dns_records = read_dns_records(file_path)
for domain, record_type, host, value in dns_records:
# 获取域名对应的 Zone ID
zone_id = get_zone_id(domain)
if zone_id:
# 添加 DNS 记录
add_dns_record(zone_id, domain, record_type, host, value)
if __name__ == "__main__":
main()
3.批量删除解析
保存为rm_dns_records.py
创建 rm_domains.txt 文档内域名格式为根域名,一行一个
import requests
# Cloudflare API 配置
api_email = "xxx" # 替换为你的 Cloudflare 邮箱
api_key = "xxx" # 替换为你的 Global API Key
api_url = "https://api.cloudflare.com/client/v4/zones"
# 设置请求头
headers = {
"X-Auth-Email": api_email,
"X-Auth-Key": api_key,
"Content-Type": "application/json"
}
# 从文件中读取域名
def read_domains(file_path):
domains = []
with open(file_path, 'r') as file:
for line in file:
domains.append(line.strip()) # 去掉每行的空格或换行符
return domains
# 获取 Zone ID(始终获取根域名的 Zone ID)
def get_zone_id(domain):
root_domain = domain.strip() # 去掉域名周围的空格
print(f"查询 Zone ID: {root_domain}") # 打印调试信息
# 发起 API 请求
response = requests.get(f"{api_url}?name={root_domain}", headers=headers)
if response.status_code == 200:
data = response.json()
if data["result"]:
print(f"找到 Zone ID: {root_domain} 的 Zone ID")
return data["result"][0]["id"]
else:
print(f"未找到 {domain} 的 Zone ID: {data}") # 打印详细错误信息
return None
else:
print(f"查询 {domain} 时发生错误: {response.status_code} - {response.text}")
return None
# 获取 DNS 记录并删除
def delete_all_dns_records(zone_id):
dns_url = f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records"
# 获取该 Zone 下的所有 DNS 记录
response = requests.get(dns_url, headers=headers)
if response.status_code == 200:
data = response.json()
dns_records = data["result"]
for record in dns_records:
record_id = record["id"]
record_name = record["name"]
record_type = record["type"]
print(f"准备删除记录:{record_name} 类型:{record_type}")
# 删除 DNS 记录
delete_url = f"{dns_url}/{record_id}"
delete_response = requests.delete(delete_url, headers=headers)
if delete_response.status_code == 200:
print(f"成功删除 {record_name} 的 {record_type} 记录")
else:
print(f"删除 {record_name} 的 {record_type} 记录失败: {delete_response.text}")
else:
print(f"获取 DNS 记录失败: {response.text}")
# 主函数
def main():
# 读取文件中的域名
file_path = "rm_domains.txt" # 替换为你的文件路径
domains = read_domains(file_path)
for domain in domains:
# 获取域名对应的 Zone ID
zone_id = get_zone_id(domain)
if zone_id:
# 删除所有 DNS 记录
delete_all_dns_records(zone_id)
if __name__ == "__main__":
main()
评论(0)