Script to restart ADSL if it's down


Results 1 to 3 of 3

Thread: Script to restart ADSL if it's down

  1. #1
    X_console Guest

    Script to restart ADSL if it's down

    I thought I'd share this with everyone. I'm not sure if there's already something like this on the Net... probably is but I was too lazy to look around so I wrote my own.

    Anyways, the following script will check to see if your ADSL connection is up and running. If it's not, it restarts it for you. It performs two checks. First to see if the PID of your ADSL client is running, and if it is, it checks to see if you can actually ping servers on the Net. If it fails either the first or second check, the connection is restarted. I recommend having it started from root's crontab:

    * * * * * /usr/local/sbin/adsl-restart 2> /dev/null 1> /dev/null

    Now for the script:

    Code:
    #!/bin/bash
    # adsl-restart - Restarts an ADSL connection if it's not active.
    # This program is tailored for the Roaring Penguin PPPoE client.
    # X_console <shellscope@yahoo.com>
    
    # firewall script:
    FIREWALL="/etc/firewall/gShield.rc"
    
    # servers to ping in order to determine if we have an active
    # connection. you must NOT put in a machine that is in your LAN!
    # make sure the server is somewhere on the Internet and that it does
    # not block pings:
    S1="linux.com"
    S2="slackware.com"
    S3="freebsd.org"
    S4="yahoo.com"
    S5="yorku.ca"
    
    # this variable reflects on the total number of servers
    # you want to ping:
    totalServer=5
    
    # this is a counter to determine how many servers responded
    # to the ping sent:
    
    response=0
    
    # restart() restarts the ADSL connection:
    restart()
    {
            # connection is down. we need to restart it along
            # with the firewall script:
            /usr/sbin/adsl-stop; wait
            echo "Activating ADSL connection..."
            /usr/sbin/adsl-start; wait
            $FIREWALL
    }
    
    # CHECK 1: see if the ADSL process is running:
    if [ ! -f /var/run/adsl.pid ]; then
            # connection is not running, so we restart it:
            restart
    else
            # it looks like the PID is still there, but sometimes
            # we still can't access the Net... so we move on to
            # CHECK 2...
            # CHECK 2: start pinging each of the servers:
            for server in $S1 $S2 $S3 $S4 $S5; do
    
                    # we send one ping request to each server. if all or any of
                    # the servers respond, then the connection is up. if none of
                    # them respond, then we restart the connection:
                    printf "Now pinging $server...\n"
                    ping -c1 $server 1>&2 > /dev/null
    
                    if [ "$?" -eq 0 ]; then # server responded!
                            printf "Response from $server\n"
                            response=$((response + 1));
                    else
                            printf "No response from $server\n"
                            response=$((response + 0));
                    fi
            done
    
            # if the response is greater than 0, then the ADSL connection
            # is up. if it's 0, then we have to restart the ADSL connection:
    
            if [ "$response" -gt 0 ]; then
                    # ADSL connection is up so we exit:
                    exit 0
            else
                    # ADSL connection is down, so we restart it:
                    restart
            fi
    fi
    By the way, the above was made for the Roaring Penguin PPPoE client... it can probably be modified to suit your PPPoE client though.

  2. #2
    Join Date
    Jun 2007
    Posts
    1

    Smile error mesages

    Hi.my name is Andrei and I have used your script but seems it doesn't work on Mandriva 2006.Can you please send me a working script for Mandriva 2006 ?.Thx.
    Last edited by andreiu; 06-25-2007 at 09:43 AM.

  3. #3
    Join Date
    Sep 2003
    Location
    Rochester, MN
    Posts
    3,604
    Well, this is a very old thread and the user that originally posted it doesn't frequent JustLinux anymore. I don't use ADSL myself, but glancing through the script it looks like you'll need to alter the restart function and possibly the "if [ ! -f /var/run/adsl.pid ]; then" line since those two things will probably be different now in Mandriva. Basically figure out how you would normally restart your connection and then stick those commands in the restart function, and maybe even remove the check for whether it's running entirely, since I suspect it's not necessary.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •