Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Salvation for Dial Up Users (with some awful Perl)

by Anonymous Monk
on Jan 21, 2002 at 00:01 UTC ( [id://140236]=CUFP: print w/replies, xml ) Need Help??

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); } }

Replies are listed 'Best First'.
Re: Salvation for Dial Up Users (with some awful Perl)
by sparkyichi (Deacon) on Jan 21, 2002 at 13:48 UTC
    Very nice work. I was planning on making a Perl script to check when the connection is dropped and to reinitiate it as well as do some other things. I think you code will be of some help to me.
    BTW
    Isn't there a better way to get your IP info such a route. I guess it doesn't make much difference just seems a bit easier to me.
    Good Luck

    Sparky
    FMTEYEWTK
      Hey there Sparky, That's my bit of lame bained code - lost my login somewhere between writing and posting. route might work, just didn't think of it at the time. I was trying to capture the dynamicly generated IP address that my ISP was giving me, and my routing table only showed that ppp0 was my default gateway, not what the dynamicly generated IP was. I could be wrong, networking is not only not my forte, I tend to rely on two terminals at a time, one with a man page and the other with a prompt.

      Looking over my code, I think I could probably cut it down a lot, maybe break the connection/file checking into a sub to minimize overhead, etc.. The version I posted was also designed to email a standup account my dynamic IP address - I also have a version that is really paired down, just checking to see if you are online or not (incorporated into a small shell script, it creates the ONLINE file, the shell script just checks for that file before running your tasks). If you want to save time on cutting it up, I can send that along as well.

      z3d

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://140236]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-20 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found