Thursday, May 24, 2007

auto-reconnecting lost DSL connection

Assuming you're using rp-pppoe, and have perl and ifconfig installed.

At start, configure your pppoe connection using usual pppoe-setup

Now you'll need 3 files:

1. /etc/ppp/pppoe-params (remove all "# this line must be empty", they really must be empty lines)


# this line must be empty
# this line must be empty
# this line must be empty
server
your_password
your_password
0
y
# this line must be empty


2. pppoe-reconnect.sh:

#!/bin/sh
pppoe-stop
pppoe-setup </etc/ppp/pppoe-params
pppoe-start


3. inet_reconnect.pl

#!/usr/bin/perl
use POSIX qw(setsid);

chdir '/' or die "Can't chdir to /: $!";
umask 0;
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDERR, '>/dev/null' or die "Can't write to /dev/null: $!";
defined(my $pid = fork) or die "Can't fork: $!";
exit if $pid;
setsid or die "Can't start a new session: $!";

while(1) {
while (!(`ifconfig` =~ /ppp0/)) {
print "inet is down, trying to reconnect..\n";
`pppoe-reconnect.sh`;
}
sleep(60);
}


Put both scripts to accessible locations (like /usr/local/sbin) and chmod +x them.
Then add inet_reconnect.pl to system startup file (I have it in /etc/conf.d/local.start in gentoo)
Done. This simple daemon will check for ppp0 in ifconfig output once per minute, and reconnect if it's not there.

And some background.. 'pppoe-setup' line in 'pppoe-reconnect.sh' may be omitted in most cases, as well as /etc/ppp/pppoe-params file. I need it because of weird behavior of our ISP's setup, so I forced to do pppoe-setup on each connect.

So, if doing simple pppoe-start is sufficient for you - just do 'pppoe-stop; pppoe-start' in reconnect script.

3 comments:

AT-HE said...

i wrote this .. works on spanish version of ubuntu .. i think you can test and correct parameters if dont, you may put in crontab

----------
#!/bin/sh
# adsl-reconnect.sh by AT-HE (at_he hotmail) nov 2008

IFACE=ppp0
DOWN=poff
UP="pon dsl-provider"
LOG=/var/log/adsl-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 ----- >>$LOG
date>>$LOG
$DOWN >>$LOG 2>&1
$UP >>$LOG 2>&1
#update dns
ddclient >>$LOG
fi

Anonymous said...

insert a sleep(60) command into the inner while loop

Inventory Management Software said...

Thanks for sharing your post and it was superb .I would like to hear more from you in future too.
Regards:
Inventory Management Software
http://www.blackitsoft.com/inventory-pos-software.aspx