78 lines
2.9 KiB
Python
78 lines
2.9 KiB
Python
import json
|
|
import uuid
|
|
import datetime
|
|
from fileinput import filename
|
|
from sys import argv
|
|
from pathlib import Path
|
|
from apicalls import *
|
|
|
|
|
|
# Get API tocken and Email address
|
|
def getCredentials():
|
|
api_key = input("Enter the Cloudflare API token: ")
|
|
email_address = input("Enter the Cloudflare Email address: ")
|
|
return {"api_key": api_key, "email": email_address}
|
|
|
|
|
|
# Get credentials
|
|
|
|
if len(argv) > 1 and 'loadconfig' in argv:
|
|
if Path("config.json").exists():
|
|
configfile = open("config.json")
|
|
config = json.load(configfile)
|
|
credentials = {
|
|
'api_key': config['API_Token'], 'email': config['Email']}
|
|
else:
|
|
print('config.json was not found. Please enter the credentials manually')
|
|
credentials = getCredentials()
|
|
else:
|
|
credentials = getCredentials()
|
|
|
|
|
|
zones = listAllZones(credentials).json()["result"]
|
|
|
|
|
|
print('Here are the domains in your account')
|
|
zone_index = 0
|
|
zone_list = {}
|
|
for zone in zones:
|
|
print(f'{zone_index + 1}: {zone["name"]}')
|
|
zone_list[zone['name']] = {'id': zone['id'], 'name': zone['name'], 'owner': zone['owner']['email'], 'account_of': zone['account']['name']}
|
|
|
|
domainToDelete = input('Please enter the domain name to delete the DNS records from:\n')
|
|
|
|
if domainToDelete in zone_list:
|
|
zone_id = zone_list[domainToDelete]['id']
|
|
zone_owner = zone_list[domainToDelete]['owner']
|
|
confirmation = input(f'Are you sure to delete all the DNS records from the domain {domainToDelete}, owned by {zone_owner}?\nEnter YES to continue: ')
|
|
if confirmation == 'YES':
|
|
# Get Bind DNS
|
|
bind_DNS = exportBindDNS(credentials, zone_id).text
|
|
# Write to a file
|
|
file_name = f'dns-export-{str(uuid.uuid4())[0:8]}-{domainToDelete}-{datetime.datetime.now().strftime("%d-%b-%Y-%H-%M-%S")}.txt'
|
|
print(f'Backing up DNS records to {file_name}')
|
|
bind_export_file = open(file_name, 'w')
|
|
bind_export_file.write(bind_DNS)
|
|
bind_export_file.close()
|
|
print('Successfully backed up DNS records in BIND format.')
|
|
# get DNS Records
|
|
dns_records = getAllDNS(credentials, zone_id).json()["result"]
|
|
# Delete DNS Records
|
|
print('Starting Deletion of DNS records')
|
|
for record in dns_records:
|
|
dns_id = record['id']
|
|
dns_type = record['type']
|
|
dns_name = record['name']
|
|
dns_content = record['content']
|
|
print(f'Deleting:\n{dns_type}\t{dns_name}\t{dns_content}...')
|
|
delete_response = deleteARecord(credentials, zone_id, dns_id)
|
|
if delete_response.json()['result']['id'] == dns_id:
|
|
print(f'Successfully Deleted {dns_id}')
|
|
else:
|
|
print('Something is wrong, abroading...')
|
|
assert False
|
|
else:
|
|
print('Aborted, bye...')
|
|
else:
|
|
print('We don\'t have access to the domain name you have entered. Please make sure the API token you have provided has access to the domain name you have entereed.')
|