Skip to content

Instantly share code, notes, and snippets.

@jasontucker
Created October 5, 2012 03:02
Show Gist options
  • Save jasontucker/3837819 to your computer and use it in GitHub Desktop.
Save jasontucker/3837819 to your computer and use it in GitHub Desktop.
Protect a site using a dyndns host and htaccess.
#!/bin/bash
## Crontab Example:
## #*/3 * * * * /bin/sh ~/htaccessdynamic.sh whatevermydyndnsis.dyndns-remote.com ~/domainiwanttoprotect.com/.htaccess > /dev/null 2>&1
dynDomain="$1"
htaccessLoc="$2"
dynIP=$(/usr/bin/dig +short $dynDomain)
echo "dynip: $dynIP"
# verify dynIP resembles an IP
if ! echo -n $dynIP | grep -Eq "[0-9.]+"; then
exit 1
fi
# if dynIP has changed
if ! cat $htaccessLoc | /bin/grep -q "$dynIP"; then
# grab the old IP
oldIP=`cat ~/htold-ip.txt`
# output .htaccess file
echo "order deny,allow" > $htaccessLoc 2>&1
echo "allow from $dynIP" >> $htaccessLoc 2>&1
echo "allow from x.x.x.x" >> $htaccessLoc 2>&1
echo "deny from all" >> $htaccessLoc 2>&1
# save the new ip to remove next time it changes, overwriting previous old IP
echo $dynIP > ~/htold-ip.txt
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment