文章目录
分享文章
最近做了一波采集站,需要将大量域名上传至Cloudflare,因此使用Python写了一个脚本,只需要简单配置即可批量上传网站至Cloudflare,最后生成文本信息方便日后管理。
# -*- coding:utf-8 -*-
import requests
import json
import csv
import random
import datetime
'''
1.每次手动输入一个域名进行解析,多个的话,会进行循环添加
2.DNS默认添加www和主域名
3.修改SSL模式为flexible
4.每次从cf.csv里随机获取一个账户进行添加
'''
####################
vps = '159.88.186.888' # 修改为需要解析的ip
#####################
while True:
email = '[email protected]' # CF账号
api = 'a5b904f85d8a582cbdcb6ce7de23164d87ebc' # API
domain = input('输入你要添加的域名:')
names = ['www', domain] # 默认添加www和主域名
headers = {'X-Auth-Email': email,
'X-Auth-Key': api,
'Content-Type': 'application/json'}
proxies = {'http': 'http://127.0.0.1:1080', 'https': 'https://127.0.0.1:1080'}
r = requests.get('https://api.cloudflare.com/client/v4/accounts?page=1&per_page=20&direction=desc', headers=headers)
print(r.json())
account_id = r.json()['result'][0]['id']
data1 = {'name': domain, 'account': {'id': account_id, 'name': email}, 'jump_start': False}
# '''
# 添加域名
url1 = 'https://api.cloudflare.com/client/v4/zones'
r = requests.post(url1, headers=headers, data=json.dumps(data1))
r_result = r.json()
print(r_result)
print(r_result['success'])
domain_id = r_result['result']['id']
print(domain_id)
ns1 = r_result['result']['name_servers'][0]
ns2 = r_result['result']['name_servers'][1]
# 添加DNS
for name in names:
data2 = {'type': 'A',
'name': name,
'content': vps,
'ttl': 1,
'priority': 0,
'proxied': True}
url2 = 'https://api.cloudflare.com/client/v4/zones/' + domain_id + '/dns_records'
r = requests.post(url2, data=json.dumps(data2), headers=headers)
# print(r.json()['result'])
print('恭喜! %s 添加成功.' % domain)
# '''
# domain_id = '529cc89cbfea5f7f98bd4d23d80970d7'
# 修改SSL模式
url3 = 'https://api.cloudflare.com/client/v4/zones/' + domain_id + '/settings/ssl'
r = requests.patch(url3, data=json.dumps({'value': 'flexible'}), headers=headers)
# print(r.json()['result']['value'])
print('email: %s' % email)
print('nameserver1: %s' % ns1)
print('nameserver2: %s' % ns2)
print('--------------------------------------')
d = datetime.datetime.now()
with open('解析信息.txt', 'a') as f:
f.write('域名:' + domain + '\n' + 'vps:' + vps + '\n' + 'email:' + email + '\n' + 'ns1:' + ns1 + '\n' + 'ns2' + ns2 + '\n'+'添加日期:'+ str(d) +'\n' + '============================' + '\n')