Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env bash
# /path/to/dyndns-update.sh
# 1. Create a credentials file called
# dyndns-update.sh_EXAMPLE.COM.creds.txt
# with user credentials, such as
# example.com-username:USER-PASSWORD
#
# 2. Try this at least once. The output of curl commands tells if update is successful, not necessary or if something was wrong.
# sudo sh /path/to/dyndns-update.sh
#
# 3. Run this as a cronjob
# $ sudo crontab -e
# and add something like this to your crontab (this checks IP every 10 mins):
# */10 * * * * sh /path/to/dyndns-update.sh EXAMPLE.COM
# Change 1 here to get more output:
#DEBUG=1
DEBUG=
USER_ID=$(id -u)
if [ "$USER_ID" -ne "0" ]; then
echo This script must be run as a root.
exit 0
fi
DOMAIN=$1
if [ -z "$DOMAIN" ]; then
echo "Usage: "
echo "${0} example.com"
exit 1
fi
SCRIPT=`realpath $0`
SCRIPT_FOLDER=$(dirname $SCRIPT)
SCRIPT_NAME=$(basename $SCRIPT)
DOMAIN_IP=$(host $DOMAIN |awk '{print $4}' | xargs)
PUBLIC_IP=$(curl --silent https://ipinfo.io/ip | xargs)
if [ "$DOMAIN_IP" != "$PUBLIC_IP" ]; then
echo -n "Domain ${DOMAIN} public IP is ${DOMAIN_IP}. "
echo "Your local IP is ${PUBLIC_IP}, so DynHost needs updating. "
FILE="${SCRIPT_FOLDER}/${SCRIPT_NAME}_${DOMAIN}.creds.txt "
# Reads the 1st line of a file $FILE into a PASS varible.
read -r PASS < $FILE
if [ -z "$PASS" ]; then
echo "No password (tried $FILE), exiting!"
exit 1;
fi
[ ! -z "$DEBUG" ] && [ "$DEBUG" -ne "0" ] && echo "Next (with creds):" "https://www.ovh.com/nic/update?system=dyndns&hostname=${DOMAIN}&myip=${PUBLIC_IP}"
curl --silent --user "${PASS}" "https://www.ovh.com/nic/update?system=dyndns&hostname=${DOMAIN}&myip=${PUBLIC_IP}"
elif [ ! -z "$DEBUG" ] && [ "$DEBUG" -ne "0" ]; then
echo "Domain ${DOMAIN} public IP is ${DOMAIN_IP}."
echo "Your local IP is ${PUBLIC_IP}."
echo "Domain ${DOMAIN} IP still ok."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment