Tuesday, February 9, 2010

Another way to use cron to keep a log of your dynamic ip changes.

My cousin moved my desktop/fileserver over to his house and hooked it up to the internet to give me access to it via ssh. He has a dynamic ip from his isp which changes anytime the router is rebooted and at random times through the day. I wanted to both keep track of the ip and keep a log of how often it changed. I didn't think that I could keep the log if I used something like dyndns. I already use dropbox on all my machines, so I thought that this would be an easy way to get the ip back and forth and be accessable anywhere that I might travel. So I wrote a cron job to dump the ip address to a file in my dropbox folder every hour on the hour.

So if your going to do this my way, your going to need to have dropbox, lynx, cron, and some sort of text editor [vim].

I'm running archlinux on my desktop. First, I looked around in /etc for my cron folders and I found these:
  • /etc/cron.daily
  • /etc/cron.hourly
  • /etc/cron.monthly
  • /etc/cron.weekly
Since all I wanted was something to run every hour, I logged into root and ran cd /etc/cron.hourly. Now I used my favorite text editor, vim, to create a new shell script for cron to run, vim ip. Then I pressed 'i' to edit the file. I have used lynx on many the ocassion to browse the web on the command line so I'm familiar with it. This is what I ended up with in my shell script.
#Logs the ip to a file in the dropbox folder
#edit dropbox variable to correct path.

lynx -dump http://www.whatismyip.org >> $dropbox/ip.log

#end of file

Now just type :w and :q, for write and quit. Then change permissions so that cron can execute the script, chmod a+x /etc/cron.hourly/ip. Now give the script a test run /etc/cron.hourly/ip. You can cat the file in dropbox to see if it worked, cat /ip.log.

Enjoy this quick and dirty way to keep track of your ip address.