Skip to content

Instantly share code, notes, and snippets.

@kvieta
Last active March 29, 2023 06:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kvieta/6040b9468a8b12772b5a6bd62f213992 to your computer and use it in GitHub Desktop.
Save kvieta/6040b9468a8b12772b5a6bd62f213992 to your computer and use it in GitHub Desktop.
python3 script to update freedns dynamic dns with logging.
# Put this somewhere on your server, such as ~/scripts/freedns/freedns_update.py
# You can test by running it manually with
# python3 ~/scripts/freedns/freedns_update.py
# and you can check the log at ~/scripts/freedns/log.txt
# To automatically run the script, add this line to your crontab:
# 0 9 * * * python3 ~/scripts/freedns/freedns_update.py
# this will update the ip address every day at 9am.
# Domains that should point to the current IP
freedns_domains = set(["mysubdomain.mydomain.com"])
# FreeDNS API endpoint, accessible from https://freedns.afraid.org/api/ (use the ASCII one)
freedns_check_url = "https://freedns.afraid.org/api/?action=getdyndns&sha=API_KEY_GOES_HERE"
# It is assumed that this address responds with a page containing only the IP address
# alternative: https://wtfismyip.com/text
ip_check_url = "https://ident.me/"
import logging
logging.basicConfig(filename="log.txt",
level=logging.DEBUG,
format="%(asctime)s: %(message)s",
datefmt="%m/%d/%Y %I:%M:%S")
logging.info("")
logging.info("Script executing...")
import urllib.request
current_ip = urllib.request.urlopen(ip_check_url).read().decode("utf-8")
logging.info("Current IP is " + current_ip)
logging.info("Getting FreeDNS records...")
import urllib.request
records_raw = urllib.request.urlopen(freedns_check_url).read().decode("utf-8").split("\n")
logging.info("Found " + str(len(records_raw)) + ".")
for raw_record in records_raw:
domain, current_freedns_ip, freedns_update_url = raw_record.split("|")
if domain not in freedns_domains:
logging.info(domain + " is not on the watch list, skipping.")
else:
logging.info(domain + " (" + current_freedns_ip + ") is on the watch list.")
if current_ip == current_freedns_ip:
logging.info("IPs already match.")
else:
logging.info("IPs don't match, updating...")
urllib.request.urlopen(freedns_update_url)
logging.info("Done.")
logging.info("Script exited gracefully.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment