Created
January 26, 2024 20:00
-
-
Save dtxe/1e6ca2efd9940d5f6a5620056b81723c to your computer and use it in GitHub Desktop.
Transfer DNS records from Google Domains YAML format to Porkbun via the Porkbun API
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
import yaml | |
import requests | |
import json | |
# Load config file | |
with open('domain.YAML', 'r') as f: | |
data = list(yaml.safe_load_all(f)) | |
with open('apikeys.yml', 'r') as f: | |
apikeys = yaml.safe_load(f) | |
# for each record in the YAML file, create a new porkbun DNS record | |
for record in data: | |
if record['kind'] != 'dns#resourceRecordSet': | |
continue | |
# Get the record name and value | |
record_domain = '.'.join(record['name'].split('.')[-3:-1]) | |
record_name = '.'.join(record['name'].split('.')[:-3]) | |
for record_value in record['rrdatas']: | |
if record['type'] == 'MX': | |
record_prio = record_value.split(' ')[0] | |
record_value = record_value.split(' ')[1] | |
else: | |
record_prio = None | |
# Create the new record | |
url = 'https://porkbun.com/api/json/v3/dns/create/' + record_domain | |
payload = { | |
'secretapikey': apikeys['apisec'], | |
'apikey': apikeys['apikey'], | |
'name': record_name, | |
'type': record['type'], | |
'content': record_value, | |
'ttl': record['ttl'], | |
} | |
if record_prio: | |
payload['prio'] = record_prio | |
r = requests.post(url, json=payload) | |
print(r.text) | |
# print(json.dumps(payload, indent=4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment