Up until the last few days, I was a member of the slower than molassis club. Doing work on the home LAN was no problem, but the second I stepped through the gateway into the outside world it was rough going. And while we were dialed up an awful lot, we weren't always online, so I needed a way to tell my primary box when a good time was to run rdate and fetchmail, and when not to. Not to mention, with a dail up modem came a random and dynamic IP address if I wanted/needed to get in from work. Hence the following bit of code. I should warn you, it's not beautiful. For starters, I probably should have used some variation of the Net:: modules, there were probably better ways to handle things, etc.. I kind of just got it far enough to be functional and then got the cable modem. For all you dial up users out there, perhaps this can be a starting point...?
Quick Walkthrough: The idea was (if my own train of logic is too chaotic), to check to see if the ppp0 adaptor had an IP address associated with it. If it didn't, we weren't online and needed to drop any legacy temp files lying around. If it did, the first thing to do was to pull out the IP address and then compare it to our last temp file. The reason for this was that unless I set my cron job to run every minute (and even then), it was possible that we might have gotten disconnected and reconnected between checks. This way, it makes sure not only that the temp files exist, but that they contain the same info. If so, cool, we run any tasks we want to. If not, we update the temp file and send it to some external mail account. Why 2 temp files? I originally used the ONLINE file for other scripts to check against before running.
OK, enough babble, the code...
#!/usr/bin/perl
$mail_to = 'SOME_ADDRESS@yahoo.com';
$tmp_file = '/tmp/ppp_status_report';
$ONLINE = '/tmp/ONLINE';
@ppp_status = `ifconfig ppp0`; #Grab the dynamic IP address
if (defined(@ppp_status))
{
@ppp_status[1] =~ /(addr\:[\S+]{4,15})/;
$ppp_line = $1;
if ( -f $tmp_file )
{
open(TMP,"$tmpfile");
$line_tmp = <TMP>;
close(TMP);
if ($ppp_line == $line_tmp) {next;}
else
{
open(TMP,">$tmp_file");
print TMP $ppp_line;
close(TMP);
+
open(TMP,">$ONLINE");
+
close(TMP);
system ("mail -s \"PPP Address for MYB
+OX\" $mail_to \<$tmp_file");
}
+
}
else
{
open(TMP,">$tmp_file");
print TMP $ppp_line;
close(TMP);
open(TMP,">$ONLINE");
close(TMP); system ("mail -s \"PPP Addr
+ess for MYBOX\" $mail_to \<$tmp_file");
}
system("/usr/bin/fetchmail");
}
else
{
if ( -f $ONLINE)
{
unlink($ONLINE);
unlink($tmp_file);
}
}