Skip to content

Instantly share code, notes, and snippets.

@afresh1
Last active January 18, 2021 19:28
Show Gist options
  • Save afresh1/eed763f8adbb57de54eb1d0f80ee1dc3 to your computer and use it in GitHub Desktop.
Save afresh1/eed763f8adbb57de54eb1d0f80ee1dc3 to your computer and use it in GitHub Desktop.
Some helpers to set dynamic dns on an OpenBSD firewall on namecheap.com. Usage is `dyndns-matches $interface $host $domain || namecheap-dyndns $host $domain` (with your namecheap dyndns password in /etc/namecheap-dyndns). Should be suitable for putting in cron.
#!/bin/sh
set -e
if=$1
host=$2
domain=$3
lastip="/tmp/dyndns-$host-$domain"
ttl=3600
myip=$( ifconfig $if | awk '/inet / { print $2 }' | head -1 )
# We can trust a longer TTL since we're going to be changing the IP
[ -e "$lastip" ] \
&& [ $(( $( date +%s ) - $( stat -f %m "$lastip" ) )) -lt $ttl ] \
&& [ "$myip" = $(< "$lastip" ) ] \
&& exit
for s in $( host -t NS $domain | awk '/ name server / { print $4 }' ); do
dnsip=$( host -r -t A $host.$domain $s |
awk '/ has address / { print $4 }' | head -1 )
if [ "$myip" = "$dnsip" ]; then
echo "$dnsip" > "$lastip"
break
else
echo "$host.$domain has address $dnsip, should be $myip" >&2
exit 1
fi
done
#!/bin/sh
set -e
# https://www.namecheap.com/support/knowledgebase/article.aspx/29/11/how-do-i-use-a-browser-to-dynamically-update-the-hosts-ip/
if [ ! "$1" -o ! "$2" ] ; then
echo "Usage: $0 host domain [password_file]" >&2
exit 1
fi
url=https://dynamicdns.park-your-domain.com/update
url="$url?host=$1&domain=$2"
password_file=${3:-/etc/namecheap-dyndns}
if [ ! "$password_file" -o ! -s "$password_file" ]; then
echo "Password file ($password_file) does not exist" >&2
exit 2
fi
url="$url&password=$(< $password_file )"
# TIP: If you don't want to use any DNS client, you can just bookmark
# the URL (after inputting proper values there) and access it whenever
# you need to update your IP.
#url=$url&ip=[your_ip]"
status=$( ftp -VMN namecheap-dyndns -o- $url )
# While I would like to make this XML prettier, what a pain
if [ "$status" = "${status%<ErrCount>0</ErrCount>*}" ]; then
echo $status >&2
exit 255
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment