Retrieve WAN IP from LAN or Remotely
So most of us with a LAN setup have run into this situation. We wish to connect to our LAN remotely while at work or at the coffee shop. Our firewall is setup and ready for NAT on our desired ports and our internal computers/servers on the LAN have the appropriate services running. But our ISP gives us our IP dynamically so it may stay the same for a month or maybe a couple, or maybe it will only stay at the current address for another day. Well let's figure out how we can find our IP in the sea of addresses when far from the comforts of the LAN.So there are many ways to achieve this. One could use a service like dyndns. There are a few free, a few that are rather cheap. A few minutes googling around will give you a few options to have a static service route to your home IP. However, unless you are really desiring the ease of typing in myfunnydomainname.com (or whatever your desire is), you can follow this little method to figure out your WAN IP and then just type in that number. (From there you can still use a service to assign your IP to a name -- essentially a nameserver access) A friend (again thanks Erik at sublevo.com and venai.com) had nameserver access for me. So I can use my method to update my IP address to the nameserver, and in case it is changed and I am away, I can find out what the new address is and use it manually in the meantime.
So what we are going to do (in a nutshell). First we will write a script to fetch our WAN IP and upload the IP information to a site which we have access. Next we will create a crontab entry to automatically run our script and update our IP information.
This is the script that I use:
#!/bin/bash
# check for previous IP file downloads and remove them to make room for our new one
if [ -e index.html ]; then
rm index.html
fi
# grab our IP by downloading the page resulting for checkip.dyndns.com
wget -O index.html checkip.dyndns.com
# now upload this page we created called index.html to our ftp site
ftp -v -n "site-we-have-access-to.com" << EOF
user "username-here" "password-here"
cd /our-user-account-directory/htdocs/directory-for-ip/
put index.html
quit
EOF
So we need to have FTP access to a site and will want to create a directory in htdocs 'directory-for-ip' (of course you will want to name it something better). You could then access your WAN IP by visiting http://our-user-account.site-we-have-access-to.com/directory-for-ip (sorry for the horrible example).
Now we want to create a crontab entry to run our script and do this automatically. This is great so we don't forget to run it, or we are on vacation, or even when our IP changes in the middle of the day when we are gone from home.
We will edit our crontab:
crontab -e
and add our entry. We can choose when to run by looking up the crontab man page. I will try to explain briefly my entry and you can try to go from there:
# If you don't want the output of a cron job mailed to you, you have to direct
# any output to /dev/null. We'll do this here since these jobs should run
# properly on a newly installed system, but if they don't the average newbie
# might get quite perplexed about getting strange mail every 5 minutes. :^)
#
# Run the hourly, daily, weekly, and monthly cron jobs.
# Jobs that need different timing may be entered into the crontab as before,
# but most really don't need greater granularity than this. If the exact
# times of the hourly, daily, weekly, and monthly cron jobs do not suit your
# needs, feel free to adjust them.
#
# Run hourly cron jobs at 47 minutes after the hour:
47 * * * * /usr/bin/script /etc/cron.hourly 1> /dev/null
#
# Run daily cron jobs at 4:40 every day:
40 4 * * * /usr/bin/script /etc/cron.daily 1> /dev/null
#
# Run weekly cron jobs at 4:30 on the first day of the week:
30 4 * * 0 /usr/bin/script /etc/cron.weekly 1> /dev/null
#
# Run monthly cron jobs at 4:20 on the first day of the month:
20 4 1 * * /usr/bin/script /etc/cron.monthly 1> /dev/null
###
#So that is an example from slackware 10.2 crontab. Here is an example of how we could run our IP script at 1 minute after the hour at 4,8,10,noon,2,4,6,8, and 10pm daily:
# Run IP script
01 4,8,10,12,2,4,6,8,10 * * * /usr/bin/ip 1> /dev/null
Now save and exit according to the editor you are using to edit your crontab. You are set to go!
Some things I would like to adjust when I find some time:
-Rather then going through the whole FTP process, first grab the IP from dyndns and diff it against the existing IP on file and if it is the same then just exit, if it is different, then replace and continue. This shouldn't matter much but would be a good practice in smarter execution.
-Set up a script to automate the nameserver updates. This will be another post, another time.
Hope you can now grab your WAN IP from the coffee shop or work and enjoy surfing your LAN from the WAN.

