[HOWTO] Auto Re-Connect to dsl pppoe in Linux

[ad#ad-1] I am using Ubuntu 10.04 Lucid Lynx Alpha. I have a dsl connection (WiZoom, Wireless via ubnt but wired to my PC) from Worldlink Pvt. Ltd., Nepal. I need to connect via pppoe. I occasionally keep my PC to download overnight while I sleep or even when I am away. It works well with torrents (with deluge as my torrent client, I haven’t used transmission torrent client much) but sometimes it disconnects from the internet. So, I had to configure it to auto re-connect on disconnect. It should also connect on logon. Since network manager does not meet my requirements, I had to delete the DSL connection from my network manager (which I had configured to connect at logon). Then I would need to configure pppoe connection via pppoeconf. To do so, I bring up my terminal and type in:

sudo pppoeconf

After that I just answer a series of questions, which include my username and password. I leave all the other values to defaults as they work for me. If they don’t work for you, you may need some tweaking. You should look for help elsewhere as I am not aware of other connection types/settings.
Now, I can connect from my terminal using:

sudo pon dsl-provider

and disconnect using:

sudo poff -a

Now, I have verified that it works and am ready to move on. But disconnect does not disconnect me immediately. pppd does not terminate immediately after I use sudo poff -a so I had to write a small script that helps me disconnect immediately. To achieve that, I created a killpppd script and placed in /usr/local/sbin/. To do so, launch nautilus as root. To do so, press Alt+F2 and type in:

gksu nautilus /usr/local/sbin

Create a new text file and copy/paste or type in the following:

#!/bin/sh
#kill pppd brutally
for i in `ps -eaf | grep "pppd" | tr -s " " | cut -f2 -d " "`
do
sudo kill -9 ${i}
done

Then I made it executable (Right Click on the file>Properties>Permissions>Allow executing file as Programs). Now, we can just run

killpppd

from terminal and the pppd dies and connection is lost.
I have created yet another file that tries to disconnect using poff and also kills pppd with killpppd. I have named it dcon and it contains the following text:

#!/bin/sh
sudo poff -a
sudo killpppd
sudo poff -a

Then I made it executable. This script first tries to disconnect gracefully and tries to kill pppd if that was not successful. I have made this like this because poff gives

/usr/bin/poff: No pppd is running.  None stopped.

if no pppd was running. So, I will get to know if pppd was running or not and if it did stop.
Now, I need a connect script that only connects if the connection has been lost and does nothing if it has not. To achieve I use a small script by AT-HE and place it with name “connect” in /usr/local/sbin/. Here is the script:

#!/bin/sh
# adsl-reconnect.sh by AT-HE (at_he hotmail) nov 2008. 
# Modified by _khAttAm_, March 2010

IFACE=ppp0
DOWN=dcon
UP="pon dsl-provider"
LOG=/var/log/dsl-reconnect.log

PTP=`ifconfig $IFACE 2>&1|grep P-t-P|cut -d : -f 3|cut -d " " -f 1`
RECV=`ping -c 1 $PTP 2>&1|grep received|cut -d , -f 2|cut -d " " -f 2`

if [ "$RECV" != "1" ]
then
echo "connecting..."
echo ----- >>$LOG
date>>$LOG
$DOWN >>$LOG 2>&1
#wait for kill
sleep 3
echo "hopefully killed"
$UP >>$LOG 2>&1
fi

Then I made it executable. I have named this one connect and placed at /usr/local/sbin. The script uses our dcon script to disconnect and pon dsl-provider to connect. It checks if the IP of our server (placed at PTP variable) is ping-able. In that case, the connection has not terminated and it does nothing. However, if the IP of the server is not ping-able, it disconnects (which has probably already been disconnected but pppd may not have died, so we need to kill it otherwise the connection is made with interface ppp1, ppp2 and the script will not work) and then tries to connect. This must be run as root so I created another file named con in the same directory (usr/local/sbin/) with contents:

#!/bin/sh
sudo connect

Now, we can just run:

con

from the terminal.
Now, we have four files in /usr/local/sbin namely con, dcon, connect and killpppd. They can all be used from the terminal. Basically, only con is required as it does everything. It connects to the internet if there is no connection and re-connects if connection has been lost.
Now, I needed to run the “con” script periodically. I have chosen to run it every minute. I have used cron to do that. To do that, I ran

sudo crontab -e

. If you are running it for the first time, you will need to choose an editor. I just chose nano. Now, we need to write in the following line at the end of the file that opens in nano. You can use Shift+Insert to paste the following line:

* * * * * /usr/local/sbin/con

. Remember, the line needs to be exactly the same with all the asterisks in place. Save it with Ctrl+O and exit using Ctrl+X and crontab should update the scheduled task. This makes cron run the script “con” every minute.

Hope this helps others too.

References:
http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/
http://geekhut.blogspot.com/2007/05/auto-reconnecting-lost-dsl-connection.html

  • Why don’t you add yourself to ‘dip’ group. That way you don’t have to use sudo for pon and poff.

  • admin

    ^hi t0x1caT0r
    Thanks for pointing that out.

    Note: If someone does not intend to use sudo, s/he should also make sure that the access log is writable with that user. Also, killing pppd might require root privileges.

  • Crab

    Thanks, really useful for me.

  • Abdulla

    I think the proper way to test and reconnect a ppp connection in linux is to use certain configuration elements available in ppp.

    Add the following to your dsl-provider config file and ppp will test connectivity and retry connection after failure indefinitely while waiting 10 seconds between retries. Connection is considered failed if 3 consecutive pings fail 20 seconds apart.


    persist
    maxfail 0
    holdoff 10
    lcp-echo-interval 20
    lcp-echo-failure 3

    Hope this helps.

  • NewToLinux

    I am trying to get my dsl to auto connect.

    $ ifconfig
    eth0 Link encap:Ethernet HWaddr 00:21:97:45:29:5f
    inet6 addr: fe80::221:97ff:fe45:295f/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:26551 errors:0 dropped:0 overruns:0 frame:0
    TX packets:15549 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:38909662 (38.9 MB) TX bytes:1383630 (1.3 MB)

    lo Link encap:Local Loopback
    inet addr:127.0.0.1 Mask:255.0.0.0
    inet6 addr: ::1/128 Scope:Host
    UP LOOPBACK RUNNING MTU:16436 Metric:1
    RX packets:134 errors:0 dropped:0 overruns:0 frame:0
    TX packets:134 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:13193 (13.1 KB) TX bytes:13193 (13.1 KB)

    ppp0 Link encap:Point-to-Point Protocol
    inet addr:71.146.130.112 P-t-P:71.146.143.254 Mask:255.255.255.255
    UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1492 Metric:1
    RX packets:26484 errors:0 dropped:0 overruns:0 frame:0
    TX packets:15472 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:3
    RX bytes:38322920 (38.3 MB) TX bytes:1040591 (1.0 MB)

    $ route -e
    Kernel IP routing table
    Destination Gateway Genmask Flags MSS Window irtt Iface
    adsl-71-146-143 * 255.255.255.255 UH 0 0 0 ppp0
    link-local * 255.255.0.0 U 0 0 0 ppp0
    default adsl-71-146-143 0.0.0.0 UG 0 0 0 ppp0

    $ route -C
    Kernel IP routing cache
    Source Destination Gateway Flags Metric Ref Use Iface
    adsl-71-146-130 cawesh.canonica adsl-71-146-143 0 0 0 ppp0
    dnsr1.sbcglobal adsl-71-146-130 adsl-71-146-130 l 0 0 19 lo
    adsl-71-146-130 pw-in-f93.1e100 adsl-71-146-143 0 0 0 ppp0
    adsl-71-146-130 atemoya.canonic

    I am using Ubuntu Lucid 10.04
    I followed your instructions to the letter and created the four files (killpppd.sh, dcon.sh, connect.sh, and con. sh.
    I put them in the folder usr/local/sbin and
    set the permission to : run as a program.

    I opened terminal and typed the name of the four files and received:
    $ killpppd
    sh: killpppd: not found
    $ dcon
    sh: dcon: not found
    $ connect
    sh: connect: not found
    $ con
    sh: con: not found
    $

    HELP !!!!

    the executable as a program.

  • NewToLinux

    I am trying to get my dsl to auto connect.

    $ ifconfig
    eth0 Link encap:Ethernet HWaddr 00:21:97:45:29:5f
    inet6 addr: fe80::221:97ff:fe45:295f/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:26551 errors:0 dropped:0 overruns:0 frame:0
    TX packets:15549 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:38909662 (38.9 MB) TX bytes:1383630 (1.3 MB)

    lo Link encap:Local Loopback
    inet addr:127.0.0.1 Mask:255.0.0.0
    inet6 addr: ::1/128 Scope:Host
    UP LOOPBACK RUNNING MTU:16436 Metric:1
    RX packets:134 errors:0 dropped:0 overruns:0 frame:0
    TX packets:134 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:13193 (13.1 KB) TX bytes:13193 (13.1 KB)

    ppp0 Link encap:Point-to-Point Protocol
    inet addr:71.146.130.112 P-t-P:71.146.143.254 Mask:255.255.255.255
    UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1492 Metric:1
    RX packets:26484 errors:0 dropped:0 overruns:0 frame:0
    TX packets:15472 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:3
    RX bytes:38322920 (38.3 MB) TX bytes:1040591 (1.0 MB)

    $ route -e
    Kernel IP routing table
    Destination Gateway Genmask Flags MSS Window irtt Iface
    adsl-71-146-143 * 255.255.255.255 UH 0 0 0 ppp0
    link-local * 255.255.0.0 U 0 0 0 ppp0
    default adsl-71-146-143 0.0.0.0 UG 0 0 0 ppp0

    $ route -C
    Kernel IP routing cache
    Source Destination Gateway Flags Metric Ref Use Iface
    adsl-71-146-130 cawesh.canonica adsl-71-146-143 0 0 0 ppp0
    dnsr1.sbcglobal adsl-71-146-130 adsl-71-146-130 l 0 0 19 lo
    adsl-71-146-130 pw-in-f93.1e100 adsl-71-146-143 0 0 0 ppp0
    adsl-71-146-130 atemoya.canonic

    I am using Ubuntu Lucid 10.04
    I followed your instructions to the letter and created the four files (killpppd.sh, dcon.sh, connect.sh, and con. sh.)
    I put them in the folder usr/local/sbin and
    set the permission to : run as a program.

    I opened terminal and typed the name of the four files and received:
    $ killpppd
    sh: killpppd: not found
    $ dcon
    sh: dcon: not found
    $ connect
    sh: connect: not found
    $ con
    sh: con: not found
    $

    HELP !!!!

    • admin

      Did you try /usr/local/sbin/con??

      • NewToLinux

        $ /usr/local/sbin/killpppd
        sh: /usr/local/sbin/killpppd: not found
        $ /usr/local/sbin/dcon
        sh: /usr/local/sbin/dcon: not found
        $ /usr/local/sbin/connect
        sh: /usr/local/sbin/connect: not found
        $ /usr/local/sbin/con
        sh: /usr/local/sbin/con: not found
        $

        The files are in usr/local/sbin a
        the permissions are set to execute as program
        ????

  • NewToLinux

    $ /usr/local/sbin/killpppd
    sh: /usr/local/sbin/killpppd: not found
    $ /usr/local/sbin/dcon
    sh: /usr/local/sbin/dcon: not found
    $ /usr/local/sbin/connect
    sh: /usr/local/sbin/connect: not found
    $ /usr/local/sbin/con
    sh: /usr/local/sbin/con: not found
    $

    The files are in usr/local/sbin and
    the permissions are set to execute as program
    ????

  • jefri

    strange i i can’t ping my P-T-P address,.. use http://www.google.com instead,.. hmm