cloudflare上域名的管理

一。服务器安装操作环境,以下两步只需要再服务器上操作一次就可以了
1、sudo yum install -y python-setuptools-0.9.8-7.el7.noarch
2、sudo easy_install pip
3、sudo pip install cloudflare

二。修改ip.txt文件  修改成新的IP
三。修改domain.txt  将要修改的域名添加进去,一行一个域名
四。执行python put_domain.py 文件

批量修改cloudflare解析记录的值

#!/usr/bin/python
#!coding:utf-8
import CloudFlare


cf = CloudFlare.CloudFlare(email='cloudflare账号', token='对应的token  此处需要查出')


new_ip=open('ip.txt','rb').read().strip()

zones=open('domain.txt','rb').readlines()
zones=[zone.strip() for zone in zones if zone.strip()!='']


for zone in zones:
    print "正在处理域名%s"%zone
    zone_id=cf.zones.get(params={"name":zone})[0]["id"]
    record=cf.zones.dns_records.get(zone_id)
    A_records = [r for r in record if r['type']=='A' and r['content']!=new_ip]
    A_record_ids = [r['id'] for r in A_records]

#######记录的IP和要更换的一致不执行
    if len(A_record_ids) ==0:
        continue

#######要添加的记录
    data=[{'name':zone,'type':'A','content':new_ip,'proxied':False},
          {'name':'ag','type':'A','content':new_ip,'proxied':False}]

#####删除A记录
    for A_record_id in A_record_ids:
        cf.zones.dns_records.delete(zone_id,A_record_id)
#####添加修改后的A记录
    try:
        for dd in data:
            cf.zones.dns_records.post(zone_id,data=dd)
    except:
        print "修改$s A记录失败!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"%zone
        print "\r\n"
        continue
        
        

 

 

批量添加域名到cloudflare

##api-key 可在cloudflare属性里看
export CF_API_EMAIL=账号
export CF_API_KEY=*************
##加什么域名放在domains.txt 中
for domain in $(cat domains.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

 

批量增加CNAME记录

#!/usr/bin/python
#!coding:utf-8
import CloudFlare
import re

cf = CloudFlare.CloudFlare(email='对应账号', token='对应token***********')


cname_record=open('cname.txt','rb').read().strip()
 
zones=open('domain.txt','rb').readlines()
zones=[zone.strip() for zone in zones if zone.strip()!='']
 
print zones
for zone in zones:
    #zone = "www."+zone 
    print '正在处理%s'%zone
#    print(cf.zones.get(params={"name":zone}))
    zone_id=cf.zones.get(params={"name":zone})[0]["id"]
    record=cf.zones.dns_records.get(zone_id)
#    A_record = [r for r in record if r['type']=='CNAME' and r['name']==zone]
    A_record = [r for r in record if r['type']=='CNAME' and r['name']=='www.'+zone]
 
 
    if not A_record:
#        data={'name':zone,'type':'CNAME','content':cname_record,'proxied':False}
        data={'name':'www.'+zone,'type':'CNAME','content':cname_record,'proxied':False}
        cf.zones.dns_records.post(zone_id,data=data)
        continue
    A_record_id = A_record[0]['id']
    data={'content':cname_record,'proxied':False}

批量增加A记录

#!/usr/bin/python
#!coding:utf-8
import CloudFlare
import re
 
cf = CloudFlare.CloudFlare(email='账号', token='对应token*******') 
 
cname_record=open('ip.txt','rb').read().strip()
 
zones=open('domain.txt','rb').readlines()
zones=[zone.strip() for zone in zones if zone.strip()!='']
 
print zones
for zone in zones:
    #zone = "www."+zone 
    print '正在处理%s'%zone
#    print(cf.zones.get(params={"name":zone}))
    zone_id=cf.zones.get(params={"name":zone})[0]["id"]
    record=cf.zones.dns_records.get(zone_id)
###   @记录
#    A_record = [r for r in record if r['type']=='A' and r['name']==zone]

###   www记录
    A_record = [r for r in record if r['type']=='A' and r['name']=='www.'+zone]
 
 
    if not A_record:
###   @记录
#        data={'name':zone,'type':'A','content':cname_record,'proxied':True}
###   www记录
        data={'name':'www.'+zone,'type':'A','content':cname_record,'proxied':True}
        cf.zones.dns_records.post(zone_id,data=data)
        continue
    A_record_id = A_record[0]['id']
    data={'content':cname_record,'proxied':True}

 

你可能感兴趣的:(cloudflare上域名的管理)