Skip to content

Instantly share code, notes, and snippets.

@plaa plaa/noip Secret
Created Mar 23, 2014

Embed
What would you like to do?
#!/bin/bash
set -e
############################# CONFIGURATION #############################
# Dynamic DNS user
USERNAME="user"
# Dynamic DNS password
PASSWORD="secret"
# Dynamic DNS update URL (default uses no-ip.com)
UPDATE_URL="https://${USERNAME}:${PASSWORD}@dynupdate.no-ip.com/nic/update"
# Regex for preferred local IP address (in case multiple are found)
PREFERRED_IP="^192.168"
# Default domain for device names ("android" -> "android.no-ip.biz")
DEFAULT_DEV_DOMAIN="no-ip.biz"
# Default domain for environment names ("dev" -> "dev.example.com")
DEFAULT_ENV_DOMAIN="example.com"
###########################################################################
DEVICE="$1"
IP="$2"
if [ -z "$DEVICE" ]; then
echo "Usage: noip <device name|hostname> [environment name|ip-address|hostname]"
echo "Examples:"
echo " noip android -> point android.${DEFAULT_DEV_DOMAIN} to your local IP"
echo " noip android dev -> point android.${DEFAULT_DEV_DOMAIN} to dev.${DEFAULT_ENV_DOMAIN}"
echo " noip android google.com -> point android.${DEFAULT_DEV_DOMAIN} to google.com"
echo " noip android 1.2.3.4 -> point android.${DEFAULT_DEV_DOMAIN} to 1.2.3.4"
exit 1
fi
# Convert device name into host name
if echo "$DEVICE" | grep -vq '\.'; then
DEVICE="${DEVICE}.${DEFAULT_DEV_DOMAIN}"
fi
# Convert environment name into host name
if echo "$IP" | grep -q '^[^.][^.]*$' ; then
IP="${IP}.${DEFAULT_ENV_DOMAIN}"
fi
# Convert environment host name into IP address
if echo "$IP" | grep -q "[A-Za-z]" ; then
IPADDR="$(dig +short "$IP")"
if [ -z "$IPADDR" ]; then
echo "Could not resolve host name $IP"
exit 1
fi
IP="$IPADDR"
fi
# Use local IP address if none provided
if [ -z "$IP" ]; then
# Following works with both Linux and Mac style of ifconfig output
IPS="$(ifconfig | grep "inet " | grep -v '[: ]127\.0\.' | perl -pe 's/^.*?([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*$/$1/' | tr '\n' ' ')"
COUNT=0
PREFERRED=0
for i in $IPS; do
[ -z "$IP" ] && IP="$i"
if echo "$i" | grep -q "$PREFERRED_IP"; then
IP="$i"
PREFERRED=$(($PREFERRED+1))
fi
COUNT=$(($COUNT+1))
done
case $COUNT in
0)
echo "No local IP address was found"
exit 1 ;;
1) ;;
*)
case $PREFERRED in
0)
echo "WARNING: Multiple IP addresses found with none preferred: $IPS"
echo " Selecting arbitrarily IP address $IP" ;;
1) ;;
*)
echo "WARNING: Multiple preferred IP addresses found in $IPS"
echo " Selecting arbitrarily IP address $IP" ;;
esac ;;
esac
echo "Using local IP address $IP"
fi
# Check IP address validity
if ! echo "$IP" | grep -q '^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$'; then
echo "Invalid IP address $IP"
exit 1
fi
# Perform update
echo "Setting host $DEVICE to IP address $IP"
RESULT="$(curl -s "${UPDATE_URL}?hostname=${DEVICE}&myip=${IP}")"
echo "$RESULT"
if ! echo "$RESULT" | grep -q "good\|nochg"; then
echo "Error setting IP address"
exit 1
fi
# Wait until DNS has been updated
COUNT=0
NEWIP="xxx"
while [ "$NEWIP" != "$IP" ]; do
sleep 1
NEWIP="$(dig +short "$DEVICE")"
COUNT=$(($COUNT+1))
echo -ne "Waiting for DNS update... (${COUNT}s ip=$NEWIP) \r"
done
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.