ddns-updater/update_client.py
2024-11-10 15:06:21 +00:00

44 lines
1.4 KiB
Python

import os
import socket
from time import sleep
import requests
from dotenv import load_dotenv
load_dotenv()
DDNS_WG_URL = os.environ.get("DDNS_UPDATE_WG")
DDNS_NC_URL = os.environ.get("DDNS_UPDATE_NC")
DDNS_CR_URL = os.environ.get("DDNS_UPDATE_CR")
DDNS_GT_URL = os.environ.get("DDNS_UPDATE_GT")
GT_HOST = os.environ.get("GT_HOST")
MY_IP_URL = "http://ifconfig.me"
domains = [DDNS_NC_URL, DDNS_WG_URL, DDNS_CR_URL, DDNS_GT_URL]
current_ip = requests.get(MY_IP_URL, timeout=15).text
registered_ip = socket.gethostbyname(GT_HOST)
while True:
try:
current_ip = requests.get(MY_IP_URL, timeout=15).text
registered_ip = socket.gethostbyname(GT_HOST)
except Exception as e:
print(f"Error while retrieving current IP! Error:\n\n{e}")
else:
if current_ip != registered_ip:
print(f"IP change detected (was {current_ip} - now {registered_ip})! Updating DNS entries...")
for item in domains:
try:
response = requests.get(item)
print(f"Response from endpoint:\n\n{response.text}")
except Exception as e:
print(f"Error while updating DDNS entry! Error:\n\n{e}")
else:
print("DNS entry updated!")
else:
print(f"No IP change detected (currently {current_ip}).")
finally:
print("Sleeping 10 minutes...")
sleep(1200)