From fcb18989b94489f5d2388710b2b887ba6106ba4b Mon Sep 17 00:00:00 2001 From: Gitouche Date: Mon, 26 Sep 2022 22:52:12 +0200 Subject: [PATCH] clean ipv6 support --- src/example.config.py | 10 +++++-- src/gandi-live-dns.py | 61 ++++++++++++++++++++++--------------------- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/example.config.py b/src/example.config.py index fb03a6b..e73bb28 100644 --- a/src/example.config.py +++ b/src/example.config.py @@ -26,10 +26,16 @@ domain = 'mydomain.tld' #enter all subdomains to be updated, subdomains must already exist to be updated #your domain and subdomains to be updated, subdomains must already exist to be updated dnsentries = { - "mydomain.tld": ["subdomain1", "subdomain2"], - "myotherdomain.tld": ["subdomain3"], + "mydomain.tld": { + "ipv4": ["subdomain1", "subdomain2"], + "ipv6": ["subdomain3v6"], + }, + "myotherdomain.tld": { + "ipv4": ["subdomain4"], + }, } + #300 seconds = 5 minutes ttl = '300' diff --git a/src/gandi-live-dns.py b/src/gandi-live-dns.py index c63c888..327891f 100755 --- a/src/gandi-live-dns.py +++ b/src/gandi-live-dns.py @@ -65,11 +65,10 @@ def get_dnsip(uuid, is_ipv6=False, verbose=False): ''' if is_ipv6: record_type = '/AAAA' - subdomain = config.subdomains6[0] else: record_type = '/A' - subdomain = config.subdomains[0] + subdomain = config.subdomains[0] url = config.api_endpoint+ '/zones/' + uuid + '/records/' + subdomain + record_type headers = {'X-Api-Key':config.api_secret} u = requests.get(url, headers=headers) @@ -125,41 +124,43 @@ def main(force_update, verbosity, repeat): if repeat and verbose: print(f'repeat turned on, will repeat every {repeat} seconds') - for key, value in config.dnsentries.items(): - config.domain = key - config.subdomains = value + for domain, content in config.dnsentries.items(): + config.domain = domain + for key, value in content.items(): + afi = key + config.subdomains = value - #get zone ID from Account - uuid = get_uuid() + #get zone ID from Account + uuid = get_uuid() - #compare dynIP and DNS IP - dynIP = get_dynip(config.ifconfig, verbose) + #compare dynIP and DNS IP + dynIP = get_dynip(config.ifconfig[afi], verbose) + + if check_is_ipv6(dynIP, verbose): + is_ipv6 = True + print('Detected ipv6') + else: + print('Detected ipv4') + is_ipv6 = False + + dnsIP = get_dnsip(uuid, is_ipv6, verbose) - if check_is_ipv6(dynIP, verbose): - subdomains = config.subdomains6 - is_ipv6 = True - print('Detected ipv6') - else: - print('Detected ipv4') - is_ipv6 = False subdomains = config.subdomains - dnsIP = get_dnsip(uuid, is_ipv6, verbose) - - if force_update: - print ('Going to update/create the DNS Records for the subdomains') - for sub in subdomains: - update_records(uuid, dynIP, sub, is_ipv6, verbose) - else: - if verbose: - print(f'dynIP: {dynIP}') - print(f'dnsIP: {dnsIP}') - if dynIP == dnsIP: - print ('IP Address Match - no further action') - else: - print (f'IP Address Mismatch - going to update the DNS Records for the subdomains with new IP {dynIP}') + if force_update: + print ('Going to update/create the DNS Records for the subdomains') for sub in subdomains: update_records(uuid, dynIP, sub, is_ipv6, verbose) + else: + if verbose: + print(f'dynIP: {dynIP}') + print(f'dnsIP: {dnsIP}') + if dynIP == dnsIP: + print ('IP Address Match - no further action') + else: + print (f'IP Address Mismatch - going to update the DNS Records for the subdomains with new IP {dynIP}') + for sub in subdomains: + update_records(uuid, dynIP, sub, is_ipv6, verbose) if repeat: if verbosity: print(f'Repeating in {repeat} seconds')