Skip to content

Instantly share code, notes, and snippets.

@Envek
Last active August 12, 2017 23:55
Show Gist options
  • Save Envek/9aa53b06e7df369626a9 to your computer and use it in GitHub Desktop.
Save Envek/9aa53b06e7df369626a9 to your computer and use it in GitHub Desktop.
Updates A and/or AAAA DNS records in DNS Master provider of Dynamic DNS. Works on Linux and OS X.
#!/bin/sh
usage() {
echo "DNS Master DynDNS update script. Updates A and/or AAAA records with public IPs that you have on your interfaces."
echo "Usage: $0 -h hostname[,hostname…] -u username -p password"
}
if [ $# -eq 0 ]; then usage; exit 1; fi
USERNAME=""
PASSWORD=""
HOSTNAME=""
# Read configuration from parameters
while [ "$1" != "" ]; do
case $1 in
-h | --host ) shift
HOSTNAME=$1
;;
-u | --user ) shift
USERNAME=$1
;;
-p | --password ) shift
PASSWORD=$1
;;
--help ) usage
exit 0
;;
* ) usage
exit 1
esac
shift
done
[ -z $HOSTNAME ] && echo "You should specify hostname(s) in the -h switch" && exit 1
[ -z $USERNAME ] && echo "You should specify username in the -u switch" && exit 1
[ -z $PASSWORD ] && echo "You should specify password in the -p switch" && exit 1
# Get the IPs under which we are known in the internets
ipv4=$(curl -4 -s https://ipv4.wtfismyip.com/text)
ipv6=$(curl -6 -s https://ipv6.wtfismyip.com/text)
# Filter those that we really have on our interfaces
if command -v ip >/dev/null 2>&1; then # First try to use ip command as only available in Red Hat 7
ipv4=$(ip -o -4 addr | grep inet | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | grep $ipv4)
ipv6=$(ip -o -6 addr | grep inet6 | grep -E -o "(([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{1,4})" | grep $ipv6)
elif command -v ifconfig >/dev/null 2>&1; then # Try to use ifconfig, it's deprecated but is only choice for OS X
ipv4=$(ifconfig | grep inet | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | grep $ipv4)
ipv6=$(ifconfig | grep inet6 | grep -E -o "(([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{1,4})" | grep $ipv6)
else
echo "Required either ip or ifconfig command to be available in PATH!" 1>&2
exit 2
fi
# Build query string
QUERY=""
[ -n "$ipv4" ] && QUERY="${QUERY}&myip=${ipv4}"
[ -n "$ipv6" ] && QUERY="${QUERY}&ipv6=${ipv6}"
# Ask DNS Master to update records
if [ -n "$QUERY" ]; then
curl -s --basic --user "${USERNAME}:${PASSWORD}" "https://api.nic.ru/dyndns/update?hostname=${HOSTNAME}${QUERY}"
else
echo "No public addresses found on interfaces!" 1>&2
exit 3
fi
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment