I'm a little slow to follow up, but I think I have what I want. When my ISP changes my DHCP assigned IP address, I will find out about it within 24 hours and can make appropriate reference changes.
I'm guessing that this has all been done before, but as the Perl slogan goes "There's more than one way to do it." I thought that readers here might be in the same situation I am and might appreciate having this little script.
First, I created the subdirectory 'changes' in my home directory, and included the following perl script.
Code:
#!/usr/bin/perl -w
# script written in perl to see if IP has changed
# from linuxquestions.org discussion groups
# written by Buckyjunior 2004/2/26
# set up working directory on Linux box
chdir "/home/me/changes" or die "cannot chdir to home/me/changes\n: $!";
# uncomment the following line to create the file oldip.txt
# system touch "oldip.txt";
# get IP address from whatismyip.org as "newip.txt"
system "wget --quiet --output-document newip.txt whatismyip.org";
# open each file and assign the single line of content to variables
open (OLD, ' while() {
chomp($oldip = $_); #get the old file and assign variable
}
open (NEW, ' while() {
chomp($newip = $_); #get the new file and assign variable
}
if ($oldip ne $newip) {
# files are different, IP has changed, create newipmsg.txt
system "cat mailmsg.txt oldip.txt RT.txt newip.txt > newipmsg.txt";
# send mail home with the new IP
system "mail -n -s 'IP Address Note' -c me\@localhost.localdomain bucky\@anotherISP.com < newipmsg.txt";
# move the new file to become the old file
system "mv newip.txt oldip.txt";
# remove the "new" file
system "rm newip.txt";
}
# no changes, so we don't need to do anything
# drop out
I saved the file as
chkIP. I also wrote a cute text file and named it
mailmsg.txt so that I would have something to read in the message in addition to the IP numbers. I couldn't figure out how to insert a carriage return into the
cat command, so I created another file
RT.txt with a single carriage return.
For the
chkIP job, this command and entry made the whole thing work.
Code:
crontab
37 1 * * * perl /home/me/changes/chkIP
This was followed by ^D. The crontab entry reads: at 37 minutes into the hour 1, all days of the month, all months of the year, all days of the week, have Perl run the file chkIP in the subdirectory named.
To test it, I made a false IP address for the
oldip.txt file and waited overnight for the cron job to run. Works fine. My response looks something like this:
Subject: IP Address Note
Appended below is your old IP address followed by
your new IP address. Please make the appropriate
changes to your system configuration.
cron
123.456.78.90
234.56.789.120
Obviously, if you want to use this code, you'll have to change "me" to whomever you are on your system, make sure you are allowed to run cron jobs, etc.
Enjoy. Be well.
Bucky