Using Google Chrome?

Download my
free Chrome Extension, Power Notes Searcher, to make searching for and evaluating SAP notes, much easier.

Recent Posts

Sunday, October 05, 2014

Netgear ReadyNAS & DynDNS Replacement

Print Friendly and PDF
With the demise of free DNS services from companies such as DynDNS, I was left with no free method of reliably accessing my Netgear ReadyNAS via the internet.

Before these free services closed down, my ReadyNAS was configured to automatically update it's IP address to the DynDNS service website, which allowed me to use a DynDNS provided host address (e.g. mydevice.dyndns.org) to access the ReadyNAS no matter what it's IP address was changed to by my home ISP provider.

When the free service eventually stopped, I decided that I didn't want to pay for such a small service offering, so I wrote a small script to perform the same sort of service.
The script simply checks my current ISP assigned IP address and compares it to the previous run of the script.  If the IP address has changed between checked, the script sends an email to my email account, which I can then use to access the device by directly entering the IP address into my web browser.

It's not neat, but it works.
Here's how I did it.

What you'll need:
- Root access to your ReadyNAS (you need the SSH addon installed).
- Configure email alerting on your ReadyNAS (do this in FrontView by giving it access to your email provider e.g. smtp.gmail.com).

Step 1 - Create the script.

As the root user connected to your ReadyNAS via SSH, use vi to create a small script in the home directory of the root user:

# cd $HOME
# vi check_ip_address

#!/bin/bash
#--------------------------------------------------------
#  Get IP address and mail it to us.
#  Place this script in $HOME/root
#  Call it from crontab.
#
#-------------------------------------------------------- user="ReadyNAS"
email_from=$(grep '^from' /etc/msmtprc | cut -f2 -d' ')
email_addr=`grep email /etc/frontview/alert.conf | awk -F '!!' '{ print $2 }' | awk -F ',' '{ print $1 " " $2 " " $3 }'`

subject="IP Address Changed" newfile=/tmp/myip.html
oldfile=/tmp/myip.old

# Call a generic site to get our IP.
wget --no-cookies --no-cache --user-agent="Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36" --no-verbose --output-document=/tmp/myip.html http://www.askapache.com/online-tools/whoami/

# Read output.
myipnew=`awk -F "tt>" '{ if($2 ~/\./){ sub(/<\//,"",$2); print $2} }' $newfile`
myipold=`cat $oldfile 2>&1`
if [ "$myipnew" != "$myipold" ]; then
  # IP is different to last, so save it and mail it.
  echo "$myipnew" > $oldfile
  mesg="New IP Address: $myipnew"   headers="From: $email_from\nTo: $email_addr\n"
  full_content="${headers}Subject: ${subject}\n${content}\n\n$mesg"
  echo -e "$full_content" | /usr/sbin/sendmail -B8BITMIME -f $email_from $email_addr &> /dev/null
  echo "RC: $?"
fi

Set permissions on the script:

# chmod 750 check_ip_address

Step 2 - Schedule the Script.

Add the script to a cron schedule:

# export EDITOR=vi
# crontab -e
30 09 * * * /root/check_ip_address
30 14 * * * /root/check_ip_address

The above schedules the script to run at 09:30am every day, and again at 14:30.
That's it!

No comments: