#!/usr/bin/perl ##### # bpa-check.pl - use to check current IP, and update a GnuDIP server if changed # ------------ # Please check the following config items ##### #sendmail or equivalent location $sendmail = "/usr/lib/sendmail"; #email address to send notifications to # undef $email; # to disable sending email $email = "email\@host"; # interface where dynamic IP (dhcp) is $interface = "eth0"; # username for GnuDIP service $username = "username"; # password for GnuDIP service $password = "password"; # hostname handle for your host $hosthandle = "handle"; # word to grep for on the ifconfig display $grepword = "inet"; # file to use to store currently checked IP $bpaipfile = "/etc/bpaipaddress"; # update command, will append the current IP address after this command (jddns3) $updatecommand = "/usr/local/bin/jddns3 --user $username --pass $password set $hosthandle"; #uncomment this if you want to use the non java client #$updatecommand = "/sbin/ddns3 -u $username -p $password -s $hosthandle"; #script to run if IP changes # $script = "/usr/local/sbin/bind-restart.sh"; # undef $script; # to disable script running... # change the ifconfig call if need be if it fails $_ = `/sbin/ifconfig $interface | grep -w $grepword`; #print $_; (/inet ((\d{1,3}\.?){4})/m) ? ( $ipaddr = $1 ) : (warn 'ifconfig failed'); #print "$ipaddr\n"; print "\nUpdating dynamic dns...\n"; if (-s $bpaipfile) { open(IN,$bpaipfile); while() { $oldip = $_; } close(IN); #print "$oldip\n"; if ($oldip ne $ipaddr) { print "No Match!!\n"; $return = `$updatecommand $ipaddr`; print $return; if ( $return =~ m/OK/ || $return =~ m/ip is unchanged/sg ) { open(OUT,">$bpaipfile"); print OUT $ipaddr; close(OUT); } # send mail to notifer user that the IP has changed if ($email){ print "Sending notification to $email...\n"; open (MAIL, "|$sendmail $email"); print MAIL "Subject: IP changed on $hosthandle to $ipaddr\n\n"; print MAIL "$0 noticed your IP has changed...\n"; print MAIL "This is the new ip [$ipaddr]\n"; print MAIL "This is the old ip [$oldip]\n\n"; close (MAIL); } #run the command because of IP change if ($script ){ exec $script; } } } else { open(OUT,">$bpaipfile"); print OUT $ipaddr; close(OUT); } print "This is the new ip [$ipaddr]\n"; print "This is the old ip [$oldip]\n\n";