We threw together a quick & simple script to dynamically update your .htaccess files within apache to add your dynamic IP address to the allow / deny fields.
If you’re looking to password protect an admin area (for example) but your office only has a dynamic IP address, then this script might be handy for you.
Its an extremely simple script that polls your dynamic hostname (if you use no-ip.org or dyndns.org for example) every 15 minutes as a cron job and, if it has changed, updates the .htaccess file
Hopefully it will make your life just a little bit easier
Sample Cron entry :
And now the script :
# Dynamic IP .htaccess file generator
# Written by Star Dot Hosting
dynIP=$(/usr/bin/dig +short $dynDomain)
echo "dynip: $dynIP"
# verify dynIP resembles an IP
if ! echo -n $dynIP | grep -Eq "[0-9.]+"; then
# if dynIP has changed
if ! cat $htaccessLoc | /bin/grep -q "$dynIP"; then
# grab the old IP
# 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 > /usr/local/bin/htold-ip.txt