Compare commits
3
Commits
bb80e40ce9
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ffa3cd076 | ||
|
|
e95018240c | ||
|
|
49e7cd13b6 |
No files matched your search
@@ -1,3 +1,50 @@
|
|||||||
# cloudflare-delete-all-dns-records
|
# cloudflare-delete-all-dns-records
|
||||||
|
|
||||||
Delete all the DNS records in a domain in Cloudflare.
|
Delete all the DNS records in a domain in Cloudflare.
|
||||||
|
|
||||||
|
This program will delete all the DNS records in a given domain. Before deletion, it'll also take a backup of the existing DNS records and save it as a .txt file in [BIND](https://www.isc.org/bind/) format.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
- [Python 3](https://www.python.org/)
|
||||||
|
- Pip
|
||||||
|
|
||||||
|
>sudo apt install python3-pip
|
||||||
|
|
||||||
|
- [Python requests library](https://pypi.org/project/requests/)
|
||||||
|
|
||||||
|
>pip install requests
|
||||||
|
|
||||||
|
- CloudFlare API key
|
||||||
|
|
||||||
|
You need a CloudFlare API key with access to *edit zone DNS* access for the domain:
|
||||||
|
|
||||||
|
1. Go to your Cloudflare profile and then [API Tokens](https://dash.cloudflare.com/profile/api-tokens)
|
||||||
|
2. Click on *Create Token*
|
||||||
|
3. You can use the template *Edit Zone DNS*
|
||||||
|
4. Enter the of the domain(s) you want to delete DNS from in *Zone Resources*
|
||||||
|
5. Click *Continue to summary* and the *Create Token*
|
||||||
|
6. You should get an API Token now.
|
||||||
|
|
||||||
|
|
||||||
|
## Running the program
|
||||||
|
|
||||||
|
1. Download and extract the zip file or Clone this repository:
|
||||||
|
|
||||||
|
>git clone https://git.pavakpaul.com/pavak/cloudflare-delete-all-dns-records.git
|
||||||
|
|
||||||
|
2. cd to the directory:
|
||||||
|
|
||||||
|
>cd
|
||||||
|
cloudflare-delete-all-dns-records
|
||||||
|
|
||||||
|
3. Run app.py using python3 and follow the instructions
|
||||||
|
|
||||||
|
>python3 app.py
|
||||||
|
|
||||||
|
### Using a config file to pass the API credentials
|
||||||
|
|
||||||
|
If you want to delete all the DNS records from multiple domains, it is a good idea to use a single token for mutiple domains. In that case, you can use save your credentials in config.json. Simply rename config_sample.json to config.json: `mv config_sample.json config.json` and save your configuration there. To load the config file, simply pass the argument _loadconfig_:
|
||||||
|
|
||||||
|
>python3 app.py loadconfig
|
||||||
|
|
||||||
|
You still can't delete DNS from multiple domains to reduce accidental removals of DNS. To be safe, it's always a good idea to just create a token for a single domain.
|
||||||
-30
@@ -15,11 +15,6 @@ def errorChecker(r):
|
|||||||
assert False
|
assert False
|
||||||
|
|
||||||
def verifyCredentials(credentials):
|
def verifyCredentials(credentials):
|
||||||
"""
|
|
||||||
curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \
|
|
||||||
-H "Authorization: Bearer <API TOKEN HERE>" \
|
|
||||||
-H "Content-Type:application/json"
|
|
||||||
"""
|
|
||||||
headers = getHeaders(credentials)
|
headers = getHeaders(credentials)
|
||||||
r = requests.get('https://api.cloudflare.com/client/v4/user/tokens/verify', headers=headers)
|
r = requests.get('https://api.cloudflare.com/client/v4/user/tokens/verify', headers=headers)
|
||||||
errorChecker(r)
|
errorChecker(r)
|
||||||
@@ -27,13 +22,6 @@ curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \
|
|||||||
|
|
||||||
|
|
||||||
def listAllZones(credentials):
|
def listAllZones(credentials):
|
||||||
"""
|
|
||||||
curl -X GET "https://api.cloudflare.com/client/v4/zones \
|
|
||||||
-H "X-Auth-Email: user@example.com" \
|
|
||||||
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
|
|
||||||
-H "Content-Type: application/json"
|
|
||||||
"""
|
|
||||||
headers = getHeaders(credentials)
|
|
||||||
zones = requests.get(
|
zones = requests.get(
|
||||||
'https://api.cloudflare.com/client/v4/zones',
|
'https://api.cloudflare.com/client/v4/zones',
|
||||||
headers= getHeaders(credentials)
|
headers= getHeaders(credentials)
|
||||||
@@ -43,12 +31,6 @@ curl -X GET "https://api.cloudflare.com/client/v4/zones \
|
|||||||
|
|
||||||
|
|
||||||
def getAllDNS(credentials, id):
|
def getAllDNS(credentials, id):
|
||||||
"""
|
|
||||||
curl -X GET "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records" \
|
|
||||||
-H "X-Auth-Email: user@example.com" \
|
|
||||||
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
|
|
||||||
-H "Content-Type: application/json"
|
|
||||||
"""
|
|
||||||
dns_records = requests.get(
|
dns_records = requests.get(
|
||||||
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)
|
||||||
@@ -57,12 +39,6 @@ curl -X GET "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a83
|
|||||||
return dns_records
|
return dns_records
|
||||||
|
|
||||||
def exportBindDNS(credentials, id):
|
def exportBindDNS(credentials, id):
|
||||||
"""
|
|
||||||
curl -X GET "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records/export" \
|
|
||||||
-H "X-Auth-Email: user@example.com" \
|
|
||||||
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
|
|
||||||
-H "Content-Type: application/json"
|
|
||||||
"""
|
|
||||||
export_response = requests.get(
|
export_response = requests.get(
|
||||||
f'https://api.cloudflare.com/client/v4/zones/{id}/dns_records/export',
|
f'https://api.cloudflare.com/client/v4/zones/{id}/dns_records/export',
|
||||||
headers=getHeaders(credentials)
|
headers=getHeaders(credentials)
|
||||||
@@ -71,12 +47,6 @@ curl -X GET "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a83
|
|||||||
return export_response
|
return export_response
|
||||||
|
|
||||||
def deleteARecord(credentials, zone_id, dns_id):
|
def deleteARecord(credentials, zone_id, dns_id):
|
||||||
"""
|
|
||||||
curl -X DELETE "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records/372e67954025e0ba6aaa6d586b9e0b59" \
|
|
||||||
-H "X-Auth-Email: user@example.com" \
|
|
||||||
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
|
|
||||||
-H "Content-Type: application/json"
|
|
||||||
"""
|
|
||||||
delete_response = requests.delete(
|
delete_response = requests.delete(
|
||||||
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)
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
|
import json
|
||||||
|
import uuid
|
||||||
|
import datetime
|
||||||
from fileinput import filename
|
from fileinput import filename
|
||||||
import json, uuid,datetime
|
|
||||||
from sys import argv
|
from sys import argv
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# import requests
|
|
||||||
from apicalls import *
|
from apicalls import *
|
||||||
|
|
||||||
|
|
||||||
# Get API key and Email address
|
# Get API tocken and Email address
|
||||||
def getCredentials():
|
def getCredentials():
|
||||||
api_key = input("Enter the Cloudflare API key: ")
|
api_key = input("Enter the Cloudflare API token: ")
|
||||||
email_address = input("Enter the Cloudflare Email address: ")
|
email_address = input("Enter the Cloudflare Email address: ")
|
||||||
return {"api_key": api_key, "email": email_address}
|
return {"api_key": api_key, "email": email_address}
|
||||||
|
|
||||||
@@ -20,7 +20,8 @@ if len(argv) > 1 and 'loadconfig' in argv:
|
|||||||
if Path("config.json").exists():
|
if Path("config.json").exists():
|
||||||
configfile = open("config.json")
|
configfile = open("config.json")
|
||||||
config = json.load(configfile)
|
config = json.load(configfile)
|
||||||
credentials = {'api_key': config['API_Key'], 'email': config['Email']}
|
credentials = {
|
||||||
|
'api_key': config['API_Token'], 'email': config['Email']}
|
||||||
else:
|
else:
|
||||||
print('config.json was not found. Please enter the credentials manually')
|
print('config.json was not found. Please enter the credentials manually')
|
||||||
credentials = getCredentials()
|
credentials = getCredentials()
|
||||||
@@ -73,4 +74,4 @@ if domainToDelete in zone_list:
|
|||||||
else:
|
else:
|
||||||
print('Aborted, bye...')
|
print('Aborted, bye...')
|
||||||
else:
|
else:
|
||||||
print('We don\'t have access to the domain name you have entered. Please make sure the API key you have provided has access to the domain name you have entereed.')
|
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.')
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
{
|
{
|
||||||
"API_Key": "your api key goes here",
|
"API_Token": "your api key goes here",
|
||||||
"Email": "your cloudflare email address"
|
"Email": "your cloudflare email address"
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user