Last active
March 30, 2024 23:19
-
-
Save AfroThundr3007730/2f24d51247c17b091746cdee87227e29 to your computer and use it in GitHub Desktop.
Dynamic DNS update wrapper for nsupdate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/systemd/system/ddclient.service | |
[Unit] | |
Description=Dynamic DNS update wrapper for nsupdate | |
[Service] | |
Type=simple | |
Restart=no | |
ExecStart=/usr/local/sbin/my-ddclient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/systemd/system/ddclient.timer | |
[Unit] | |
Description=Dynamic DNS update wrapper for nsupdate | |
Wants=network.target | |
[Timer] | |
OnCalendar=*:0/5 | |
Persistent=1 | |
[Install] | |
WantedBy=timers.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# /usr/local/sbin/my-ddclient | |
# The actual ddclient keeps breaking things so we replaced it with a shell script. | |
# SPDX-License-Identifier: GPL-3.0-or-later | |
echo "Updating IPv4/IPv6 host records for $HOSTNAME." | |
valid_fqdn='^[-a-zA-Z0-9]{1,63}\.([-a-zA-Z0-9]{1,63}\.?)+\.?$' | |
valid_length='^.{4,254}$' | |
valid_ipv4='^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$' | |
valid_ipv6='^([0-9a-fA-F]{1,4}::?){1,7}[0-9a-fA-F]{1,4}$' | |
if [[ ! $HOSTNAME =~ $valid_fqdn || ! $HOSTNAME =~ $valid_length ]]; then | |
echo "Invalid FQDN for host: $HOSTNAME" | |
exit 1 | |
fi | |
nameserver=$(dig +short soa "${HOSTNAME#*.}" | awk '{print $1}') | |
current_ipv4=$(curl api.ipify.org 2>/dev/null) # Alternate: ipv4.icanhazip.com | |
current_ipv6=$(curl api6.ipify.org 2>/dev/null) # Alternate: ipv6.icanhazip.com | |
published_ipv4=$(dig +short a "$HOSTNAME" @"$nameserver") | |
published_ipv6=$(dig +short aaaa "$HOSTNAME" @"$nameserver") | |
if [[ ! $current_ipv4 =~ $valid_ipv4 ]]; then | |
echo "No valid IPv4 address found. $current_ipv4" | |
elif [[ $current_ipv4 == "$published_ipv4" ]]; then | |
echo "IPv4 address matches published record. $current_ipv4" | |
else | |
update_cmd+="update delete $HOSTNAME 3600 in a\n" | |
update_cmd+="update add $HOSTNAME 3600 in a $current_ipv4\n" | |
fi | |
if [[ ! $current_ipv6 =~ $valid_ipv6 ]]; then | |
echo "No valid IPv6 address found. $current_ipv6" | |
elif [[ $current_ipv6 == "$published_ipv6" ]]; then | |
echo "IPv6 address matches published record. $current_ipv6" | |
else | |
update_cmd+="update delete $HOSTNAME 3600 in aaaa\n" | |
update_cmd+="update add $HOSTNAME 3600 in aaaa $current_ipv6\n" | |
fi | |
if [[ -n $update_cmd ]]; then | |
update_cmd+="show\nsend\n" | |
else | |
echo 'No addresses to update, exiting.' | |
exit 0 | |
fi | |
nsupdate -k /etc/"${HOSTNAME%%.*}".tsig <<<"$(echo -e "$update_cmd")" | |
echo "Record update complete." | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need to account for possible
nsupdate
failures on this one.