Last active
May 16, 2021 21:08
-
-
Save bearlike/50cf1bedf473ec7ce8b90549a2914238 to your computer and use it in GitHub Desktop.
Adds DNS A records pointing to a mentioned server using Cloudflare API v4. Edit places necessary.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Adds DNS A records pointing to a mentioned server using Cloudflare API v4. Edit places necessary. | |
Note: | |
For better codebase privacy/security, refer configuration file for | |
authentication in python-cloudflare docs. This is for internal usage. | |
""" | |
import CloudFlare # pip3 install cloudflare | |
import sys | |
def print_help(): | |
help_string = """ | |
Adds DNS A records pointing to this server. | |
USAGE | |
======= | |
python3 create-dns-record.py [ZONE_NAME] [SUBDOMAIN] | |
EXAMPLE (creates subdomain.example.com) | |
======================================= | |
python3 create-dns-record.py example.com subdomain | |
""" | |
print(help_string) | |
def return_zone_info(cf, zone_name): | |
try: | |
zone_info = cf.zones.get(params={'name': zone_name})[0] | |
except IndexError: | |
print("Zone does not exist in this token.") | |
exit(-1) | |
return zone_info | |
def add_record(zone_name, subdomain): | |
cf = CloudFlare.CloudFlare( | |
token='01234567890123456789') | |
zone_info = return_zone_info(cf, zone_name) | |
zone_id = zone_info['id'] | |
dns_record = {'name': subdomain, 'type': 'A', 'content': 'XXX.XXX.XXX.XXX'} | |
try: | |
r = cf.zones.dns_records.post(zone_id, data=dns_record) | |
except CloudFlare.exceptions.CloudFlareAPIError: | |
print("Record already Exist.") | |
exit(-1) | |
if r['name'] == subdomain+'.'+zone_name: | |
print(r['name'], "added successfully.") | |
exit(0) | |
if __name__ == '__main__': | |
try: | |
zone_name = sys.argv[1] | |
subdomain = sys.argv[2] | |
except IndexError: | |
print_help() | |
add_record(zone_name, subdomain) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Deletes DNS A record(s) using Cloudflare API v4. Edit places necessary. | |
Program will ask for manual confirmation to prevent accidental deletion. | |
Note: | |
For better codebase privacy/security, refer configuration file for | |
authentication in python-cloudflare docs. This is for internal usage. | |
""" | |
import CloudFlare # pip3 install cloudflare | |
import sys | |
def print_help(): | |
help_string = """ | |
Deletes DNS A records using Cloudflare API v4. | |
USAGE | |
======= | |
python3 delete_dns_records.py [Zone Name] [Sub doman] | |
EXAMPLE (deletes sub.example.com) | |
======================================= | |
python3 delete_dns_records.py example.com sub | |
""" | |
print(help_string) | |
def return_zone_info(cf, zone_name): | |
try: | |
zone_info = cf.zones.get(params={'name': zone_name})[0] | |
except IndexError: | |
print("Zone does not exist in this token.") | |
exit(-1) | |
return zone_info | |
def delete_record(zone_name, dns_name): | |
cf = CloudFlare.CloudFlare( | |
token='12345678901234567890') | |
zone_info = return_zone_info(cf, zone_name) | |
zone_id = zone_info['id'] | |
dns_records = cf.zones.dns_records.get( | |
zone_id, params={'name': dns_name + '.' + zone_name}) | |
if len(dns_records) < 1: | |
print("Records do not exist") | |
exit(-1) | |
print(len(dns_records), "DNS (A) Records will be deleted. Contine? (Type 'YES' to proceed): ", end="") | |
confirm_text = str(input()) | |
if not(confirm_text == "YES" or confirm_text == "yes"): | |
print("User Aborted...") | |
exit(0) | |
for dns_record in dns_records: | |
dns_record_id = dns_record['id'] | |
r = cf.zones.dns_records.delete(zone_id, dns_record_id) | |
print("Record(s) successfully deleted.") | |
exit(0) | |
if __name__ == '__main__': | |
try: | |
zone_name = sys.argv[1] | |
dns_name = sys.argv[2] | |
except IndexError: | |
print_help() | |
delete_record(zone_name, dns_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment