Skip to content

Remember current public IP and hit Hetzner API endpoint only if it changes #37

@shuuryou

Description

@shuuryou

It might be a good idea not to hit the Hetzner DNS API every five or so minutes when your script is used as part of a cron job. Since you're getting the IP addresses anyway, maybe consider refactoring the script a little bit so it remembers the last public IP address, compares it with the current one, and only invokes any part of Hetzner's DNS API if there is a change.

I had to roll my own DynDNS updater for Namecheap years ago and here's how I achieved the above:

IP=$(curl -sS -A "$AGENT" "https://api.ipify.org")

if [ -z "${IP}" ]; then
	echo "No public IP address at all. Internet down? Not doing anything."
	exit 1
fi

if [ ! -f /tmp/ddnslastip ]
then
	OLDIP="unknown"
else
	OLDIP=$(</tmp/ddnslastip)
fi

printf "The last public IP address was \"%s\" and the current public IP address is \"%s\".\n" "$OLDIP" "$IP"

if [ "$OLDIP" = "$IP" ]; then
	echo "Public IP address has not changed. Nothing to do."
	exit 0
fi

echo "$IP" > /tmp/ddnslastip

# ... perform the update ...

This needs to be updated slightly for IPv6 but I'm sure you get the general idea. Also "api.ipify.org" doesn't do any user agent sniffing (Hetzner's IP address service does to decide whether to return a website or just the IP as plain text). If you switch to it, you can get rid of the pipe to grep.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions