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 hetzner-ddns.py /usr/local/bin
|
||||||
cp etc/hetzner-ddns.conf.example /etc
|
cp etc/hetzner-ddns.conf.example /etc
|
||||||
cp etc/systemd/hetzner-ddns.service /etc/systemd/system/
|
cp etc/systemd/hetzner-ddns.service /etc/systemd/system/
|
||||||
|
systemctl daemon-reload
|
||||||
|
|
||||||
uninstall:
|
uninstall:
|
||||||
rm /usr/local/bin/hetzner-ddns.py
|
rm /usr/local/bin/hetzner-ddns.py
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ After=network-online.target
|
|||||||
Wants=network-online.target
|
Wants=network-online.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=simple
|
||||||
ExecStart=/usr/local/bin/hetzner-ddns.py
|
ExecStart=python3 -u /usr/local/bin/hetzner-ddns.py
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ Options:
|
|||||||
--config=<file> Read options from configuration file
|
--config=<file> Read options from configuration file
|
||||||
--disable-v4 Do not update IPv4 address
|
--disable-v4 Do not update IPv4 address
|
||||||
--disable-v6 Do not update IPv6 address
|
--disable-v6 Do not update IPv6 address
|
||||||
|
--repeat=<s> Update DNS again every S seconds
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from docopt import docopt
|
from docopt import docopt
|
||||||
@@ -30,6 +31,7 @@ import requests
|
|||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
import configparser
|
import configparser
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
args = docopt(__doc__)
|
args = docopt(__doc__)
|
||||||
@@ -75,6 +77,7 @@ def merge_defaults():
|
|||||||
"--retry-attempts": 12,
|
"--retry-attempts": 12,
|
||||||
"--retry-delay": 5,
|
"--retry-delay": 5,
|
||||||
"--hostname": socket.gethostname(),
|
"--hostname": socket.gethostname(),
|
||||||
|
"--repeat": 3600,
|
||||||
}
|
}
|
||||||
|
|
||||||
print("Applying default options:")
|
print("Applying default options:")
|
||||||
@@ -171,8 +174,20 @@ def create_record(record):
|
|||||||
response.raise_for_status()
|
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():
|
def main():
|
||||||
kinds = []
|
kinds = []
|
||||||
|
delay = int(args["--repeat"])
|
||||||
|
|
||||||
if not bool(args["--disable-v4"]):
|
if not bool(args["--disable-v4"]):
|
||||||
kinds += ["A"]
|
kinds += ["A"]
|
||||||
@@ -183,6 +198,7 @@ def main():
|
|||||||
print("Finding DNS zone...")
|
print("Finding DNS zone...")
|
||||||
zone = find_zone(args["--zone"])
|
zone = find_zone(args["--zone"])
|
||||||
|
|
||||||
|
while True:
|
||||||
for kind in kinds:
|
for kind in kinds:
|
||||||
if kind == "A":
|
if kind == "A":
|
||||||
print("Finding public IPv4 address...")
|
print("Finding public IPv4 address...")
|
||||||
@@ -196,8 +212,15 @@ def main():
|
|||||||
print("Finding existing %s record..." % kind)
|
print("Finding existing %s record..." % kind)
|
||||||
rec = find_record(zone=zone, kind=kind, name=args["--hostname"])
|
rec = find_record(zone=zone, kind=kind, name=args["--hostname"])
|
||||||
|
|
||||||
if rec is None:
|
if rec is not None:
|
||||||
print(" not found")
|
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)
|
print("Creating new %s record..." % kind)
|
||||||
create_record(
|
create_record(
|
||||||
{
|
{
|
||||||
@@ -209,13 +232,8 @@ def main():
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
print(" done")
|
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()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user