From 67a5b82d9bc1bc81f2ca1c5c00961d41ea4f090a Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sun, 24 May 2020 13:35:27 +0200 Subject: [PATCH] Fire a single ddns update --- Dockerfile | 1 + src/ddns_update.py | 55 ++++++++++++++++++++++++++++++++++++++------ src/requirements.txt | 2 ++ 3 files changed, 51 insertions(+), 7 deletions(-) mode change 100644 => 100755 src/ddns_update.py create mode 100644 src/requirements.txt diff --git a/Dockerfile b/Dockerfile index 9906021..9b8f8f5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,4 +3,5 @@ RUN pip install --no-cache-dir --upgrade pip WORKDIR /app COPY src/ ./ +RUN pip install --no-cache-dir --requirement requirements.txt CMD [ "python", "ddns_update.py" ] diff --git a/src/ddns_update.py b/src/ddns_update.py old mode 100644 new mode 100755 index 2170905..f5497e0 --- a/src/ddns_update.py +++ b/src/ddns_update.py @@ -1,15 +1,56 @@ #!/usr/bin/python3 -from time import sleep import logging +import requests +from requests.exceptions import RequestException +import click +from os import environ logging.basicConfig(level=logging.DEBUG) log = logging.getLogger("ddns updater") + +def response_successful(response_text): + return response_text.startswith("good") or response_text.startswith("nochg") + + +@click.command() +@click.option( + "--host", + required=True, + default=lambda: environ.get("DDNS_HOST", None), + help="The dns name to update", +) +@click.option( + "--key", + required=True, + default=lambda: environ.get("DDNS_KEY", None), + help="The ddns authorization key", +) +@click.option( + "--ip", + required=True, + default=lambda: environ.get("DDNS_IP", None), + help="The hosts new ip address in ipv4 or ipv6 format", +) +def ddns_update(host, key, ip): + url = f"https://dyndns.strato.com/nic/update?hostname={host}&myip={ip}" + + try: + response = requests.get(url, auth=(host, key), timeout=3) + response.raise_for_status() + + if not response_successful(response.text): + raise RequestException("update failed") + + except RequestException as error: + log.debug(response.text.strip()) + log.error(error) + return + + log.debug(response.text.strip()) + log.info("update successful") + + if __name__ == "__main__": - log.info("starting...") - sequence = 1 - while True: - log.debug(f"update sequence {sequence}") - sequence += 1 - sleep(5) + ddns_update() diff --git a/src/requirements.txt b/src/requirements.txt new file mode 100644 index 0000000..8574798 --- /dev/null +++ b/src/requirements.txt @@ -0,0 +1,2 @@ +Click==7.0 +requests==2.23.0