On 15 May 2006 06:55:14 -0700,
(E-Mail Removed) said:
> Hi, Is there a utility peice of software out there that restarts
> your router when if detects that the ADSL connection hangs from your
> ISP?
Here's some Perl that I've been playing with at home and at work
(being diverted from seeing some friends on Saturday morning to detour
into work to reboot the router provides an incentive to make sure the
ADSL line stays up). I run it from a cron job every 5 minutes.
Work: Vigor2600
Home: SpeedTouch 510
Both of them sometimes need a kick.
Notes: you will need to:
run the script as root if you use ICMP echo requests
change the IP addresses that it pings
change the username and password
change the command to reboot the system
do a bit of tidying up, especially if you are using Windows
Apologies to any Perl hackers out there for the coding style, from an
old fart who still thinks in Algol-60, -68, Fortran and C.
------8<------8<------8<------8<------8<------8<------8<------8<------8<
#! /usr/bin/perl -w
use strict;
use Net::Telnet;
use Net::Ping;
use Sys::Syslog;
use Mail::Mailer;
# RFC1918 IP address of your router
my $localip = "192.168.0.254";
# a list of IP addresses to ping that are 1) few hops away, and 2)
# likely to be stable
# cheapest first
# next IP upstream 0.0.0.0 (from a traceroute)
# Your ISP's DNS servers: 0.0.0.1 & 0.0.0.2
# Your ISP's web server: 0.0.0.3
# i.root-servers.net: 192.36.148.17
# and any others you can think of
my @netips = ("0.0.0.0", "0.0.0.1", "0.0.0.2", "0.0.0.3", "192.36.148.17" );
my $ip;
my $ok = 0;
my $mailer;
my $t; # telnet socket
my $p; # ping
my @lines;
my $i;
my $tmp;
$p = Net::Ping->new("icmp");
# log to syslog
openlog( "routerping", "", "user" );
foreach $ip (@netips)
{
if ( $p->ping($ip) )
{
$ok = 1;
last;
}
else
{
syslog( 'debug', "can't ping %s", $ip );
}
}
if ( ! $ok )
{
syslog( 'debug', "%s", "rebooting router" );
# make sure that there is a route to the ADSL router's local
# IP address
system( "ip route add " . $localip . "/32 dev eth1" );
$t = Net::Telnet->new( Timeout => 10, Prompt => '/> /', Host => $localip );
$t->input_log( "/var/log/router.log" );
# if your router asks for a username and password, use this:
$t->login("Administrator", "zxc314" ) or die "login failed";
# else if it just prompts for a password use this:
$t->waitfor("/Password:/" ) or die "login waitfor 1 failed";
$t->print("fr0gR0utr" ) or die "login print failed";
$t->waitfor("/> /" ) or die "login waitfor 2 failed";
@lines = $t->cmd("adsl status");
# or perhaps
@lines = $t->cmd("adsl info");
$t->print("system reboot");
# or perhaps
$t->print("sys reboot");
$t->close;
$mailer = Mail::Mailer->new("smtp", Server => "localhost") || die "can't new mail";
$mailer->open({ From => 'root',
To => 'root',
Subject=> "router reboot",
}) or die "can't open mail";
print $mailer "router reboot";
$mailer->close() || die "can't close mail";
}
------8<------8<------8<------8<------8<------8<------8<------8<------8<
--
Alan J. Wylie
http://www.wylie.me.uk/
"Perfection [in design] is achieved not when there is nothing left to add,
but rather when there is nothing left to take away."
-- Antoine de Saint-Exupery