Skip to content

Instantly share code, notes, and snippets.

@rpsu
Last active January 29, 2022 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpsu/7570c47ad3b5335087a0dee5170aac51 to your computer and use it in GitHub Desktop.
Save rpsu/7570c47ad3b5335087a0dee5170aac51 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# /path/to/dyndns-update.sh
# 1. Create a credentials file in the same folder with this script named
# 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
# Disable to get less output:
DEBUG=1
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