Add test for existing DNS entry

The actions will only be performed now when the record actually needs
to be changed.
This commit is contained in:
Thomas Kleinendorst 2024-04-07 23:40:58 +02:00
parent ef1dd3f49b
commit f3677cd314
2 changed files with 14 additions and 0 deletions

View file

@ -6,6 +6,7 @@ import logging
import sys
import configparser
import argparse
import dns.resolver
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(levelname)s - %(message)s')
@ -21,6 +22,14 @@ def get_public_IP():
logging.info(f'The public IP address for this machine is: {public_ip}.')
return public_ip
def resolve_name(domain):
logging.info(f'Resolving {domain}...')
result = dns.resolver.resolve(domain, 'A')
resolved_address = result[0].address
logging.info(f'Resolved {domain} to: {resolved_address}.')
return resolved_address
def get_zone_id(domain):
response = requests.get(f'https://api.cloudflare.com/client/v4/zones?name={domain}', headers=cloudflare_request_headers)
@ -72,6 +81,10 @@ fullDomainName = f'{subdomain}.{fixedTopLevelDomain}'
publicIP = get_public_IP()
if resolve_name(fullDomainName) == publicIP:
logging.info(f'Currently resolved name already matches the public ip ({publicIP}), exiting...')
exit(0)
config = configparser.ConfigParser()
config.read(cloudflare_token_path)

View file

@ -1 +1,2 @@
requests==2.31.0
dnspython==2.6.1