logo NodeSeekbeta

Cloudflare域名配置轉移

實現功能:獲取托管在Cloudflare中的舊域名DNS解析記錄,並添加到新域名記錄。
例如舊域名為 a.com,子域名為 b.a.comc.a.com,解析的IP地址分別為1.1.1.12.2.2.2
我要用新的域名b.com 解析a.com域名相同的dns記錄,也就是b.b.comc.b.com,解析的IP地址分別為1.1.1.12.2.2.2,一個個手動添加太麻煩了,於是用以下脚本,就可以一鍵添加記錄。獲取a.com所有的dns解析記錄,然後批量添加到b.com,IP地址也同步。

注意:使用脚本請配置好你的Cloudflare Api 和 Email 其它一般默認就好了,'proxied': False** 代表不開啓雲朵CDN,默認開啓雲朵CDN**

import re
import requests

api_key = 'Your_Key'
email = 'Your_Email'

def get_zone_id(domain):
   headers = {
      'X-Auth-Email': email,
      'X-Auth-Key': api_key,
      'Content-Type': 'application/json'
   }
   url = f'https://api.cloudflare.com/client/v4/zones?name={domain}'
   response = requests.get(url, headers=headers)
   return response.json()['result'][0]['id']

def get_config(domain):
   headers = {
      'X-Auth-Email': email,
      'X-Auth-Key': api_key,
      'Content-Type': 'application/json'
   }
   zone_id = get_zone_id(domain)
   url = f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records'
   response = requests.get(url, headers=headers)
   dns_records = []
   for record in response.json()['result']:
      dns_records.append({
            'Type': record['type'],
            'Name': record['name'],
            'Content': record['content']
      })
   return dns_records

def DNS_ADD(domain, record_type, prefix, record_content):
   headers = {
      'X-Auth-Email': email,
      'X-Auth-Key': api_key,
      'Content-Type': 'application/json'
   }
   zone_id = get_zone_id(domain)
   url = f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records'
   config_domain = prefix + '.' + domain
   data = {
      'type': record_type,
      'name': config_domain,
      'content': record_content,
      'ttl': 120,
      'proxied': True
   }
   response = requests.post(url, headers=headers, json=data)
   if response.status_code == 200:
      print('Successfully:' + config_domain + ' ' + record_type + ' ' + record_content)
   else:
      print('Failed:' + config_domain + ' ' + record_type + ' ' + record_content)
old_domain = input('請輸入要獲取解析記錄的域名:')
new_domain = input('請輸入要添加解析記錄的域名')
config_json = get_config(old_domain)

for config in config_json:
   return_domain = config['Name']
   record_type = config['Type']
   record_content = config['Content']
   try:
      prefix = re.findall('(.*)\.' + old_domain, return_domain)[0]
      DNS_ADD(new_domain, record_type, prefix, record_content)
   except Exception as e:
      print(f"Error occurred: {e}")
  • @onlynnn0 #0
    谢谢代码
    稍微优化了一下:

    import re
    import requests
    
    api_key = 'your-cf-api-key'
    email = 'your-cf-email'
    
    
    def get_zone_id(domain: str) -> str:
        headers = {
            'X-Auth-Email': email,
            'X-Auth-Key': api_key,
            'Content-Type': 'application/json'
        }
        url = f'https://api.cloudflare.com/client/v4/zones?name={domain}'
        response = requests.get(url, headers=headers)
        return response.json()['result'][0]['id']
    
    
    def get_config(domain: str) -> []:
        headers = {
            'X-Auth-Email': email,
            'X-Auth-Key': api_key,
            'Content-Type': 'application/json'
        }
        zone_id = get_zone_id(domain)
        url = f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records'
        response = requests.get(url, headers=headers)
        dns_records = []
        for record in response.json()['result']:
            dns_records.append({
                'Type': record['type'],
                'Name': record['name'],
                'Content': record['content'],
                'Proxied': record['proxied'],
                'Comment': record['comment'],
            })
        return dns_records
    
    
    def add_dns(domain: str, record_type: str, prefix: str, record_content: str, proxied: bool, comment: str):
        headers = {
            'X-Auth-Email': email,
            'X-Auth-Key': api_key,
            'Content-Type': 'application/json'
        }
        zone_id = get_zone_id(domain)
        url = f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records'
        config_domain = prefix + '.' + domain
        data = {
            'type': record_type,
            'name': config_domain,
            'content': record_content,
            'ttl': 1,
            'proxied': proxied,
            'comment': comment,
        }
        response = requests.post(url, headers=headers, json=data)
        if response.status_code == 200:
            print(f'Successfully: {config_domain} {record_type} {record_content}')
        else:
            print(f'Failed: {config_domain} {record_type} {record_content}')
    
    
    def main():
        old_domain = input('請輸入要獲取解析記錄的域名:')
        new_domain = input('請輸入要添加解析記錄的域名')
        config_json = get_config(old_domain)
    
        for config in config_json:
            return_domain = config['Name']
            record_type = config['Type']
            record_content = config['Content']
            proxied = config['Proxied']
            comment = config['Comment']
    
            try:
                prefix = re.findall('(.*)\.' + old_domain, return_domain)[0]
                add_dns(new_domain, record_type, prefix, record_content, proxied, comment)
            except Exception as e:
                print(f"Error occurred: {e}")
    
        print(f"Domain: {old_domain} DNS record has been copyed to {new_domain}")
    
    
    if __name__ == '__main__':
        main()
    
    
    
  • @petermayuyu #1 ok

你好啊,陌生人!

我的朋友,看起来你是新来的,如果想参与到讨论中,点击下面的按钮!

📈用户数目📈

目前论坛共有12543位seeker

🎉欢迎新用户🎉