[TriLUG] Strategies for dynamic-IP machines
Greg Brown
gregbrown at mindspring.com
Tue Oct 1 20:59:39 EDT 2002
I have a script that does this. The script lives in /home/greg/scripts and
it depends on the file ip-eth1 which is created by the script (it also reads
the e-mail addresses in from a file called ip-list (I know, poorly named).
The ip-list file contains one e-mail address per line. I run the script,
ip-watch, from cron every 30 minutes (crontab runs as root for no good
reason). The following is my cron entry followed by the perl script. Hope
this helps!
Cron:
00,30 * * * * /home/greg/scripts/ip-watch 2>/dev/null
the ip-watch script:
#!/usr/bin/perl
#######################################################
#
# watches eth1 to see if the IP address changes
#
# Greg Brown
# gregbrown at mindspring.com
# Sat Feb 16 22:56:47 EST 2002
#
#######################################################
# first read in the ip address in the current file
open(CURRENTIP, "/home/greg/scripts/ip-eth1");
foreach $line (<CURRENTIP>) {
chomp($line); # get rid of the newline
$currentIP=$line;
}
close(CURRENTIP);
# now that we know what the IP address was the last time the script ran
# let's see what it is now to see if it changed
open(CHECKIP, "/sbin/ifconfig eth1 |");
foreach $ipLine (<CHECKIP>) {
# now sort through this crap until we find the IP address
chomp($ipLine); # get rid of the newline
if ($ipLine =~ m/\binet addr\b/) {
$ipLine=~ s/\s+/ /g; # get rid of the extra spaces
@ipLineLong=split(/\s/,$ipLine); # split at the space
# at this point the ip address is stored in
# $ipLineLong[2]
# print "$ipLineLong[2]\n";
$ipInfo=$ipLineLong[2];
@ipInfo=split(/\:/,$ipInfo);
# the current IP address is now stored in $ipInfo[1]
# now check the two IPs aginst each other
# print "$ipInfo[1] $currentIP\n";
} # end the if inet addr
} #end the foreach loop
# now we have the old IP address ($currentIP) and the new IP Address
# (ipInfo[1]) so let's compare them to see if they are the same
if ($ipInfo[1] eq $currentIP) {
# print "we are the same $ipInfo[1] $currentIP\n";
}
else {
# print "We are NOT the same: $ipInfo[1] $currentIP\n";
# if we are here then it can be said that the IP Addresses
# are not the same so we'll have to notify everyone that the
# IP Address has changed
# first open the list containg the people to mail
open(MAILLIST, "/home/greg/scripts/ip-list");
# now process the list
foreach $entry (<MAILLIST>) {
chomp($entry); # get rid of that newline
# print "$entry\n"; # test line
# make sure the line isn't commented out
if ($entry =~ /^#/) {
# print "We have a match: $entry\n"; # test line
# do nothing with these entries, they are commented
# out
} # end the if commented
else {
# this line isn't commented so send the notice
# to the user
open(MAILER, "| /bin/mail -s \"Gregs web server
address is now $ipInfo[1]\" $entry ") || die "can't figure this out: #!\n";
print MAILER "\n\nThe address of Greg Brown's
personal web server has changed.\n";
print MAILER "\nThe new IP Address is $ipInfo[1]\n";
print MAILER "\nGreg's Korean Adventure page can now
be reached at the following address:\n";
print MAILER "\nhttp://$ipInfo[1]/korea/\n";
print MAILER "\n";
print MAILER ".\n";
print MAILER "\n";
close(MAILER);
} # end the else not a commented line
} #end the foreach loop
} # end the else loop
# now that we've send out all the e-mails telling everyone that there
# is a new IP addres let's write the new IP address to the file ip-eth1
open(IPINFONEW, ">/home/greg/scripts/ip-eth1");
print IPINFONEW "$ipInfo[1]\n";
close(IPINFONEW);
# and that's all she wrote, folks.
On Tuesday 01 October 2002 08:33 pm, you wrote:
> On Tue, 2002-10-01 at 19:04, Andrew Perrin wrote:
> > I was thinking about some scheme where when the IP changed, the
> > machine would scp a file containing its new IP address to my office
> > machine (which is fixed IP).
> >
> > Thanks for any comments!
> >
> > ----------------------------------------------------------------------
> > Andrew J Perrin - http://www.unc.edu/~aperrin
>
> That's exactly what I use for remote sites with dynamic IP addresses.
>
> _______________________________________________
> TriLUG mailing list
> http://www.trilug.org/mailman/listinfo/trilug
> TriLUG Organizational FAQ:
> http://www.trilug.org/~lovelace/faq/TriLUG-faq.html
More information about the TriLUG
mailing list