DDNS Script Setup
Keeping WireGuard configurations updated can be a pain.. A solution I used was a ddns script to update dns for my cloud server and my homelab.
I didn’t want to expose my home IP to the public so I setup ddns and configured wireguard to use dns in the configuration. This was the better solution over updating wireguard configurations everytime an IP changed.
This is a snippet of the code to read the file and update the correct records:
def read_domain_file():
domains = []
with open("/opt/opnsense-api/domains.txt", "r") as domainsFile:
for line in domainsFile.readlines():
line = line.replace("\n", "")
domains.append(line)
return domains
def domains_parsed(domain):
subdomain = domain.split(".")[:-3]
subdomain = ".".join(subdomain)
tld = domain.split(".")[-3:]
tld = ".".join(tld)[:-1]
return [subdomain, tld]
def read_environment():
with open("/opt/opnsense-api/.env", "r") as envFile:
for line in envFile.readlines():
line = line.replace("\n", "")
if line != "":
key,val = line.split("=")
os.environ[key] = val
def main():
opnsense = opnsense_pull_wan_ip(ipv="ipv4")
opnsense6 = opnsense_pull_wan_ip(ipv="ipv6")
for element in read_domain_file():
print(element)
#print(domains_parsed(element))
subdomain,tld = domains_parsed(element)
#print(subdomain, " + ", tld)
r53atom = route53_list_resource_record_set(tld, subdomain, "A")
if len(r53atom) > 0:
print(r53atom[0]['value'])
if r53atom[0]['value'] != opnsense:
print("Updating Record")
route53_update_record(domain=tld, record=subdomain, update_ip=opnsense, record_type="A")
else:
print("No Updates needed.")
else:
print("The A Record doesn't exist, Creating Record:")
route53_update_record(domain=tld, record=subdomain, update_ip=opnsense, record_type="A")
r53atom6 = route53_list_resource_record_set(tld, subdomain, "AAAA")
if len(r53atom6) > 0:
print(r53atom6[0]['value'])
if r53atom6[0]['value'] != opnsense6:
print("Updating AAAA Record")
route53_update_record(domain=tld, record=subdomain, update_ip=opnsense6, record_type="AAAA")
else:
print("No Updates needed.")
else:
print("The AAAA Record doesn't exist, Creating Record:")
route53_update_record(domain=tld, record=subdomain, update_ip=opnsense6, record_type="AAAA")
if __name__ == "__main__":
read_environment()
#print(opnsense_pull_wan_ip(ipv="ipv4"))
#print(opnsense_pull_wan_ip(ipv="ipv6"))
main()