Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Cable Modem Check

by Ozymandias (Hermit)
on May 30, 2000 at 07:40 UTC ( #15345=CUFP: print w/ replies, xml ) Need Help??

Nascent asked a question about a Perl code snippit that would reconnect his dial-up modem if it failed. Well, this is something similar.

I have a cable modem connection, and every couple of days or so, the modem dies. It's a SURFboard SB3100, manufactured by General Instrument, although I'm told this happens with other modems, too.

Anyway. From my router/firewall box, everything appears normal; IFCONFIG reports the connection as being up, but I can't ping anything past the modem. The only fix is to reset the modem; since this modem doesn't have a power switch, the only way to do that is to unplug it, wait 30 to 45 seconds, and plug it back in.

Being a lazy X10 geek, I soon installed an appliance module and used my X10 remote to stop and restart the modem. Then I started thinking about how nice it would be not to have to hit that button. This is the result, although it's far from complete; my first "from scratch" Perl script. My intention, once I've completed it, is to use it in a cron job every 1/2 hour or so.

#!/usr/bin/perl # # File: modemcheck.pl # Description: Tests for Internet connectivity through standard cable # modem; uses X10 to reset modem in the event of failure # # Author: Ozymandias # Email: Ozymandias29@hotmail.com # Version: .3 # Copyright : 2000 Matt Beland. All rights reserved. # License: This script is redistributable under the terms of the +GPL. # # Dependencies: Heyu X10 Control software, CM11A X10 two-way controlle +r, X10 # module for modem # # Heyu can be obtained from http://www.prado.com/~dbs/ # This script was written using version 1.28h # # Variables: # # $admin_email: Email address notifications should go to # $error_email: Errorlog email; used as "From: in notifications # $device: Heyu address of cable modem; e.g., a5 or modem # $target: IP address of test target; I recommend the network gat +eway # (of your cable service, of course.) # # # User-defined variables - THESE MUST BE CHANGED FOR THE SCRIPT TO WOR +K $admin_email = "you@/didntchangeit.com"; $error_email = "did@/you.com"; $device = "a1"; $target = "192.168.1.1"; # define commands needed $ping ="/bin/ping -c 1 "; # Ping $kill_modem = "/usr/local/bin/heyu turn $device off"; # Kill modem $start_modem = "/usr/local/bin/heyu turn $device on"; # Start modem # begin test $linecount = 0; $errortime = localtime; open (I,"$ping $target |"); # Is it down? while(<I>) { chop $_; $linecount = $linecount + 1; if($linecount == 2) { if(/unreachable/i) { # Connection down $errortime = localtime; $wait = time; # Test again in 5 minutes $wait = $wait + 300; until($go >= $wait) { $go = time; }; open (II,"$ping $target |"); while(<II>) { chop $_; if(/Unreachable/) { $error = 2; $errortime = localtime; system "$kill_modem"; $wait = time; $wait = $wait + 60; until($go >= $wait) { $go = time; }; system "$start_modem"; # send "restarted" email open(III,"|/usr/lib/sendmail -t"); print III "From: $errormail\n"; print III "To: $adminmail\n"; print III "Subject: NET CONNECTION ERROR\n\n"; print III "At $errortime the net connection was"; + print III "restarted by the errorhandler.\n"; close(III); }; # send minor glitch email open(IV,"|/usr/lib/sendmail -t"); print IV "From: $errormail\n"; print IV "To: $adminmail\n"; print IV "Subject: NET CONNECTION ER +ROR\n\n"; print IV "At $errortime there was a +minor problem.\n"; print IV "No restart was necessary.\n"; close(IV); }; close(II); }; print "Test successfully completed at $errortime\n"; }; }; close(I);
This code does work; I've tested it both for "OK" conditions and failure conditions. But there's still a lot I want to do with this:

  • Add testing to avoid infinite looping (if there's another cause for the problem)
  • Add support for other X10 backends (simple)
  • Add URL-based network support - to execute X10 commands on another system
  • Extra notification if the script can't solve the problem (most likely using X10 to flash a light)
As I said, this is my first from scratch code, so helpful comments would be appreciated.

- Ozymandias

Comment on Cable Modem Check
Download Code
RE: Cable Modem Check
by providencia (Pilgrim) on Jun 03, 2000 at 23:54 UTC
    That's good work and a good idea. I have DSL and have the
    same problem after about a day of inactivity.

    I have some of the components hooked up to some appliances.
    Being a newbie I always like to have examples
    I never thought there was a perl module for it.

    Now I know.
    I couldn't use the Firecracker software because of Win2k(roomate's 'puter, mine has been
    dead for over a year.)
    This is just another reason to love and hate perl.
    Love: So many uses on SO MANY SYSTEMS/EASY TO PORT!
    Hate: I'm going to buy MORE X10 Appliance Modules(**CHA-CHING)!!!

    Now I wish there was a module to work with my Yo!MP3 player.
      I'm almost done with the next version; been on hold for a few days while I do some work that actually pays the rent.

      Next version will support the other two popular backends; ppower and xtend. I'm not currently planning any Windows support, but it shouldn't be too hard to make the conversion, if you can find a program with the same functionality as Heyu.

      I think the easier way out, though, would be to spend a Saturday morning trolling through the garage sales in your area. My X10 box is a 386-25 running RedHat 6.2; it works fine, even running the BlueLava CGI web interface. Just make sure the system you run the X10 from is NOT addressable from the outside world; I don't think you'd like to have a script kiddie find it at 3 AM. <G>

      - Ozymandias

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2014-12-28 08:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (179 votes), past polls