Compare commits
2 Commits
48cfb3ee87
...
bb80e40ce9
| Author | SHA1 | Date | |
|---|---|---|---|
| bb80e40ce9 | |||
| 254615eda6 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -169,4 +169,5 @@ cython_debug/
|
|||||||
|
|
||||||
# Project setup
|
# Project setup
|
||||||
config.json
|
config.json
|
||||||
response-example*
|
response-example*
|
||||||
|
dns-export*
|
||||||
17
apicalls.py
17
apicalls.py
@ -6,7 +6,11 @@ def getHeaders(credentials):
|
|||||||
return headers
|
return headers
|
||||||
|
|
||||||
def errorChecker(r):
|
def errorChecker(r):
|
||||||
if r.status_code != 200:
|
if r.status_code == 200:
|
||||||
|
return False
|
||||||
|
elif r.status_code == 429:
|
||||||
|
print(f'\n\n\n{"="*80}It looks like you have reached your rate limit to access CloudFlare APIs\nPlease try after 5 minutes.{"="*80}')
|
||||||
|
else:
|
||||||
print(f'Error {r.status_code}.\nDetails:\n{r.json()}')
|
print(f'Error {r.status_code}.\nDetails:\n{r.json()}')
|
||||||
assert False
|
assert False
|
||||||
|
|
||||||
@ -34,6 +38,7 @@ curl -X GET "https://api.cloudflare.com/client/v4/zones \
|
|||||||
'https://api.cloudflare.com/client/v4/zones',
|
'https://api.cloudflare.com/client/v4/zones',
|
||||||
headers= getHeaders(credentials)
|
headers= getHeaders(credentials)
|
||||||
)
|
)
|
||||||
|
errorChecker(zones)
|
||||||
return zones
|
return zones
|
||||||
|
|
||||||
|
|
||||||
@ -48,6 +53,7 @@ curl -X GET "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a83
|
|||||||
f'https://api.cloudflare.com/client/v4/zones/{id}/dns_records',
|
f'https://api.cloudflare.com/client/v4/zones/{id}/dns_records',
|
||||||
headers=getHeaders(credentials)
|
headers=getHeaders(credentials)
|
||||||
)
|
)
|
||||||
|
errorChecker(dns_records)
|
||||||
return dns_records
|
return dns_records
|
||||||
|
|
||||||
def exportBindDNS(credentials, id):
|
def exportBindDNS(credentials, id):
|
||||||
@ -57,7 +63,12 @@ curl -X GET "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a83
|
|||||||
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
|
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
|
||||||
-H "Content-Type: application/json"
|
-H "Content-Type: application/json"
|
||||||
"""
|
"""
|
||||||
|
export_response = requests.get(
|
||||||
|
f'https://api.cloudflare.com/client/v4/zones/{id}/dns_records/export',
|
||||||
|
headers=getHeaders(credentials)
|
||||||
|
)
|
||||||
|
errorChecker(export_response)
|
||||||
|
return export_response
|
||||||
|
|
||||||
def deleteARecord(credentials, zone_id, dns_id):
|
def deleteARecord(credentials, zone_id, dns_id):
|
||||||
"""
|
"""
|
||||||
@ -70,4 +81,6 @@ curl -X DELETE "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31
|
|||||||
f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{dns_id}',
|
f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{dns_id}',
|
||||||
headers=getHeaders(credentials)
|
headers=getHeaders(credentials)
|
||||||
)
|
)
|
||||||
|
errorChecker(delete_response)
|
||||||
return delete_response
|
return delete_response
|
||||||
|
|
||||||
|
|||||||
24
app.py
24
app.py
@ -1,9 +1,8 @@
|
|||||||
import json
|
from fileinput import filename
|
||||||
|
import json, uuid,datetime
|
||||||
from sys import argv
|
from sys import argv
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest import result
|
|
||||||
|
|
||||||
import dns
|
|
||||||
# import requests
|
# import requests
|
||||||
from apicalls import *
|
from apicalls import *
|
||||||
|
|
||||||
@ -46,18 +45,31 @@ if domainToDelete in zone_list:
|
|||||||
zone_owner = zone_list[domainToDelete]['owner']
|
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: ')
|
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':
|
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
|
# get DNS Records
|
||||||
dns_records = getAllDNS(credentials, zone_id).json()["result"]
|
dns_records = getAllDNS(credentials, zone_id).json()["result"]
|
||||||
# Delete DNS Records
|
# Delete DNS Records
|
||||||
|
print('Starting Deletion of DNS records')
|
||||||
for record in dns_records:
|
for record in dns_records:
|
||||||
dns_id = record['id']
|
dns_id = record['id']
|
||||||
dns_type = record['type']
|
dns_type = record['type']
|
||||||
dns_name = record['name']
|
dns_name = record['name']
|
||||||
dns_content = record['content']
|
dns_content = record['content']
|
||||||
print(f'Deleting:\n{dns_type}\t{dns_name}\t{dns_content}...')
|
print(f'Deleting:\n{dns_type}\t{dns_name}\t{dns_content}...')
|
||||||
# deleteARecord(credentials, zone_id, dns_id)
|
delete_response = deleteARecord(credentials, zone_id, dns_id)
|
||||||
print(f'Deleted {dns_id}')
|
if delete_response.json()['result']['id'] == dns_id:
|
||||||
pass
|
print(f'Successfully Deleted {dns_id}')
|
||||||
|
else:
|
||||||
|
print('Something is wrong, abroading...')
|
||||||
|
assert False
|
||||||
else:
|
else:
|
||||||
print('Aborted, bye...')
|
print('Aborted, bye...')
|
||||||
else:
|
else:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user