http://qs1969.pair.com?node_id=24547

JP Sama has asked for the wisdom of the Perl Monks concerning the following question:

I use ADSL connection, and my connection has an "unexplicable" problem!
So I've decided to use a pl in my cron... :) but, I am wandering if the best way to do it is using:
(for example)
#!/usr/bin/perl @ips = ('200.219.199.32','204.71.200.75'); foreach $ip (@ips) { $ping = system("ping -c 1 $ip"); if ($ping < 256) { # next; } elsif ($ping = 256) { # timed out # do something to restart the connection # probably, set internal modem (not my Adsl modem)to pick up t +he phone line, and hangup, to cause a "sync break"! } }
If someone could give me a "light", or a sugestion about the best way to do it!

tks to all!

-jpsama

Replies are listed 'Best First'.
Re: Checking ADSL connection
by lhoward (Vicar) on Jul 26, 2000 at 22:56 UTC
    A while ago Ozymandias posted his code for a Cable Modem Check. I've also written similar code for my DSL modem. Both Ozymandias and myself use X10 (a home automation system .. x10.com) to power-cycle the modem. The particulars of detecting an outage and resetting your connection will vary based on too many factors to give you an absolute solution (DSL provider, protocol, modem brand/model, etc...). You may however be able to use the experiences of myself and Ozymandias as a starting point. My code works as follows:
    Ping the modem every 30 seconds Ping the "internet" every 5 minutes if either ping fails stop PPPOE process power-cycle modem while(modem not responding to pings for > 30 seconds after last po +wer-cycle) power-cycle modem wait 10 seconds start PPPOE process wait 10 seconds ping internet if internet still not up, repeat entire process
    I will be happy to post my complete code; if anyone is interested.
      Please do post your code. I never completed my intended final version; instead I used Downtime and pasted in my X10 code. However, I'd be interested in seeing what you did differently.

      The logic I used for my script is similar to yours, like so:

      Ping the modem every 30 seconds # Not possible with mine, or I would Ping the "internet" every 5 minutes if ping fails Increment failure counter If counter >= 3 Notify via internal email / telephone page of serious problem +and exit power-cycle modem wait 45 seconds ping internet if up, notify of problem/resolution if internet still not up, repeat

      - email Ozymandias
        Here is my code... It is VERY tuned to my particular situation and doesn't have many (any?) comments...

        Ozymandias, I haven't looked at your code recently, but one of the things emphasized in mine is a strong logging capability (via syslog).

        One thing I'd like to add is "upstream failure detection" where it will not restart the connection if the modem and the next router "upstream to the internet" both respond to pings. Also I'd like to add code to do error-checking of the results of the "/etc/rc.d/init.d/pppoe start" command.

        #!/usr/bin/perl -w use strict; use Device::SerialPort; use ControlX10::CM11; use Net::Ping; use Net::Syslog; use POSIX; # DAEMON stuff my $pid=fork; exit if $pid; die "Couldn't fork: $!" unless defined $pid; POSIX::setsid() or die "Can't start a new session: $!"; # end DAEMON stuff my $s=new Net::Syslog(Facility=>'local5', Priority=>'notice', Name=>'dslmonitor'); $SIG{TERM}=sub { $s->send('DSL monitoring stopping'); exit; }; $s->send('DSL monitoring started'); my $i; my $m; my $ctr=0; my $tdown; while(1){ $m=is_modem_up(); if(!$m){ $s->send("modem not responding to ping"); } $i=is_internet_up() if(($ctr==0)||(!$m)); if((!$i)||(!$m)){ $s->send('lost internet connection'); $tdown=time(); } while((!$i)||(!$m)){ $s->send('stopping pppoe'); stop_pppoe(); $m=0; while(!$m){ $s->send('power cycling DSL modem'); power_cycle_modem(); $s->send('power cycling complete, waiting for modem to respond t +o ping'); my $b=time; my $tdiff=time()-$b; while(($tdiff<30)&&(!$m)){ sleep 1; $m=is_modem_up(); $tdiff=time()-$b; } if($m){ $s->send("modem up $tdiff sec after cycle\n"); }else{ $s->send("modem not responding to pings $tdiff seconds after r +eboot, power cycle modem again"); } } $s->send("waiting 30 seconds for modem DSL negotiation to occur"); sleep 30; $s->send("starting pppoe"); start_pppoe(); $s->send("pppoe start complete, waiting 5 seconds"); sleep 5; $s->send("probing for connectivity"); $i=is_internet_up(); $m=is_modem_up(); if(!$m){ $s->send("modem not responding to ping"); } if(($i)&&($m)){ $tdown=time()-$tdown; my $se=$tdown%60; my $m=(int($tdown/(60)))%60; my $h=(int($tdown/(60*60)))%24; my $d=(int($tdown/(60*60*24))); $s->send("internet connection restored after $d days, $h hours, +$m minutes and $se seconds of downtime"); }else{ $s->send("pppoe negotiation failed"); } } sleep 30; $ctr=($ctr+1)%10; } sub is_modem_up{ my $p=new Net::Ping("icmp"); for(my $c=0;$c<3;$c++){ my $i=$p->ping('10.0.0.1',2);; return $i if($i); } return 0; } sub is_internet_up{ my $p=new Net::Ping("icmp"); my @inethosts=('192.5.5.241','202.12.27.33','128.63.2.53'); for(my $c=0;$c<3;$c++){ foreach(@inethosts){ my $i= $p->ping($_,5); return $i if($i); } } $s->send("internet not responding to ping"); return 0; } sub stop_pppoe{ system '/etc/rc.d/init.d/adsl stop >/dev/null 2>/dev/null'; } sub start_pppoe{ system '/etc/rc.d/init.d/adsl start >/dev/null 2>/dev/null'; # checking output would be a good idea } sub power_cycle_modem{ #my $serial_port = new Device::SerialPort('/dev/x10'); my $serial_port = new Device::SerialPort('/dev/ttyS0'); if(!defined $serial_port){ die "error openign serial \"$!\""; } $serial_port->baudrate(4800); $serial_port->databits(8); $serial_port->parity('none'); $serial_port->stopbits(1); $serial_port->handshake('none'); $serial_port->stty_echo(0); $serial_port->read_const_time(5000); $serial_port->read_char_time(5); &ControlX10::CM11::send($serial_port, 'B1'); &ControlX10::CM11::send($serial_port, 'BK'); sleep 5; &ControlX10::CM11::send($serial_port, 'B1'); &ControlX10::CM11::send($serial_port, 'BJ'); }
(Ozymandias: Why reinvent the wheel?)RE: Checking ADSL connection
by Ozymandias (Hermit) on Jul 26, 2000 at 22:57 UTC
    Two items:

    The first is my own script over at Cable Modem Check, which I wrote to use X10 modules to stop/restart my cable modem when there's a problem.

    The second is a program I use called "Downtime". This is a Perl script, freely modifiable, that monitors one or more connections and tracks the downtime. I modified my copy to replace the Cable Modem Check and to do several other things in the office; it's extremely well written and documented, and very easy to modify for any purpose.

    - email Ozymandias
Re: Checking ADSL connection
by steveAZ98 (Monk) on Jul 26, 2000 at 23:23 UTC
    You could use the Net::Ping package. I'm not sure that this is a more efficent way to do it or not? The module offers many options, so check out the docs. Here's a quick implementation of the code you have above.

    HTH
    #!/usr/bin/perl -w use Net::Ping; my @hosts = ('10.0.0.10','12.35.236.130'); my $p = Net::Ping->new('icmp') or die "Can't create ping obj: $!"; foreach my $host (@hosts) { if(!$p->ping($host, 2)) { print "$host down, do something\n"; } } $p->close();
    Also be careful of your comparison operator, it's == not =, probably a typo, but just wanted to bring it to your attention.
Re: Checking ADSL connection
by atl (Pilgrim) on Jul 26, 2000 at 23:11 UTC
    So, if I understand correctly, you have a flat rate for your internet access and want to make sure your box is always on. And probably your ISP disconnects you every once in a while. Right?

    Well then, if you are disconnected after a certain idle time, you could prevent that by sending out a ping (one packet is enough) every 60 second or so.

    Usually, you are disconnected anyway after a certain time, wether your link is active or not (T-Online does it after 24h, I believe, AOL even earlier; don't know for sure, though, cause I use other ISP's). In that case, you can want to reconnect.

    Obviously you're using Unix. The following refers to Linux but should be valid for other flavours, too. Look for appropiate commands there.

    Checking for a link can be done by checking wether or not the modem device exists, usually named ppp0. Check with ifconfig to see all devices or ifconfig ppp0 to check ppp0 directly.

    To restart automagically, use the diald. A simple ping (or any network action) will cause your box to dial in.

    Hope that helps ...

    Andreas

      prevent that (disconnect) by sending out a ping every 60 seconds or so

      My ISP won't keep a dial connection live on ICMP traffic, only FTP and HTTP.
      So I cron a webget every 5 minutes instead.   They still disconnect every 12 hours, though.
          cheers,
          ybiC

Re: Checking ADSL connection
by JP Sama (Hermit) on Jul 26, 2000 at 23:17 UTC
    Thank you Ozymandias and lhoward
    ; but, the major problem i have is the model of my adsl modem, an Alcatel SPEED TOUCH™ Home, and I "looked" in the manual, and found no comment about pinging the dsl modem, or stuff like that to this modem model...

    so, I believe i've got to do things in a different way than you just did!

    But your code's source, will be a giant push for my @#$@$ adsl connection to stay on-line... :)
    -jpsama
      The manual that came w/ my DSL modem (which has an Alcatel chipset) gave no refrences to it having any IP capabilities at all: just have your PC speak PPPoE and the modem will handle the rest. After spending enough time on the phone w/ tech support they admited to me that the modem does in fact have an IP address. My modem's default IP was 10.0.0.1/24. After configuring my ethernet interface connected to the modem to have an address on that network I was able to ping the modem, telnet into the modem to view/change configuration information, and use SNMP to poll stats the modem.

      When my modem fails it generally goes to a "dsl-down ethernet-down" condition that I can detect by pinging the modem (if the modem is up and the internet is down, I can detect that by SNMP poling the modem and/or pinging the "internet").

      Poke around and you'll probably find out that your modem does actually have an IP support and an IP address that you can ping. The manufacturer is probably just hiding this from you.

Re: Checking ADSL connection
by JP Sama (Hermit) on Jul 26, 2000 at 23:38 UTC
    atl : my ISP is in Brazil, UOL to be precise... but i think the problem is also on the Phone service provider...
    :o)

    lhoward : thanks man, i'll try this poking techinique!! probably my modem supports the services you mentioned!! thanks!!

    steveAZ98 : thanks... == is the desired operator !!
    I'm not sure it was a typo, but probably a STUPID and rookie usage of = ... :o)

    -jpsama
Re: Checking ADSL connection
by FiReWaLL (Scribe) on Jul 27, 2000 at 17:47 UTC

    Well, JP Sama, You're not the only one to have this problem in Brazil, and you're not the only one that's looking for an anwser for that. ^_^
    So if you find something new, remember to post here *or something like it) for us (me?) and I'll do the same.


    -------
    []'s
    FiReWaLL
    The only thing you regret in life, is the risk you don't take.

      You just need to ask... ! And of course, if I found a valid solution, I'll share it!!!

      :o)

      #!/jpsama/bin/perl -w
      $tks = `mount`;
      $jpsama = $! if $!;
      print $jpsama;