Update DNS repeatedly
This commit is contained in:
1
Makefile
1
Makefile
@@ -2,6 +2,7 @@ install:
|
||||
cp hetzner-ddns.py /usr/local/bin
|
||||
cp etc/hetzner-ddns.conf.example /etc
|
||||
cp etc/systemd/hetzner-ddns.service /etc/systemd/system/
|
||||
systemctl daemon-reload
|
||||
|
||||
uninstall:
|
||||
rm /usr/local/bin/hetzner-ddns.py
|
||||
|
||||
@@ -4,8 +4,8 @@ After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/local/bin/hetzner-ddns.py
|
||||
Type=simple
|
||||
ExecStart=python3 -u /usr/local/bin/hetzner-ddns.py
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
||||
@@ -20,6 +20,7 @@ Options:
|
||||
--config=<file> Read options from configuration file
|
||||
--disable-v4 Do not update IPv4 address
|
||||
--disable-v6 Do not update IPv6 address
|
||||
--repeat=<s> Update DNS again every S seconds
|
||||
"""
|
||||
|
||||
from docopt import docopt
|
||||
@@ -30,6 +31,7 @@ import requests
|
||||
import socket
|
||||
import sys
|
||||
import configparser
|
||||
import time
|
||||
|
||||
|
||||
args = docopt(__doc__)
|
||||
@@ -75,6 +77,7 @@ def merge_defaults():
|
||||
"--retry-attempts": 12,
|
||||
"--retry-delay": 5,
|
||||
"--hostname": socket.gethostname(),
|
||||
"--repeat": 3600,
|
||||
}
|
||||
|
||||
print("Applying default options:")
|
||||
@@ -171,8 +174,20 @@ def create_record(record):
|
||||
response.raise_for_status()
|
||||
|
||||
|
||||
def delete_record(rid):
|
||||
response = requests.delete(
|
||||
url=f"https://dns.hetzner.com/api/v1/records/{rid}",
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"Auth-API-Token": args["--token"],
|
||||
}
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
|
||||
def main():
|
||||
kinds = []
|
||||
delay = int(args["--repeat"])
|
||||
|
||||
if not bool(args["--disable-v4"]):
|
||||
kinds += ["A"]
|
||||
@@ -183,6 +198,7 @@ def main():
|
||||
print("Finding DNS zone...")
|
||||
zone = find_zone(args["--zone"])
|
||||
|
||||
while True:
|
||||
for kind in kinds:
|
||||
if kind == "A":
|
||||
print("Finding public IPv4 address...")
|
||||
@@ -196,8 +212,15 @@ def main():
|
||||
print("Finding existing %s record..." % kind)
|
||||
rec = find_record(zone=zone, kind=kind, name=args["--hostname"])
|
||||
|
||||
if rec is None:
|
||||
print(" not found")
|
||||
if rec is not None:
|
||||
if rec["value"] == addr:
|
||||
print("Existing record is up-to-date")
|
||||
continue
|
||||
|
||||
print("Deleting existing %s record..." % kind)
|
||||
delete_record(rec["id"])
|
||||
print(" done")
|
||||
|
||||
print("Creating new %s record..." % kind)
|
||||
create_record(
|
||||
{
|
||||
@@ -209,13 +232,8 @@ def main():
|
||||
}
|
||||
)
|
||||
print(" done")
|
||||
else:
|
||||
print(" found")
|
||||
print("Updating existing %s record..." % kind)
|
||||
rec["value"] = addr
|
||||
rec["ttl"] = args["--ttl"]
|
||||
update_record(rec)
|
||||
print(" done")
|
||||
|
||||
print(f"Sleeping for {delay} seconds...")
|
||||
time.sleep(delay)
|
||||
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user