#!/usr/bin/perl

#    befsr41-dhcp.pl - work around a bug in version 1.45.11 of the Linksys
#                      BEFSR41 firmware.
#
#    Version 1.1.0, copyright Peter Oliver
#    <p.d.oliver@mavit.org.uk>, 2004-06-19
#
#    There's a bug in the latest Linksys firmware, version 1.45.11, that
#    causes it to loose it's DHCP lease.  Unfortunately, this firmware
#    is needed to fix a security bug.  Running this script regularly
#    should keep your lease renewed.  See
#    http://www.the-inquirer.com/?article=16496 for details of the
#    firmware problem.
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the GNU General Public License,
#    version 2, as published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    http://www.opensource.org/licenses/gpl-license.html

use strict;
use warnings;
use LWP::UserAgent;

my $ip = '192.168.1.1'; # change this to your router's address if necessary 
my $user = 'admin';
my $password = ''; # fill in your password here
my $release = 0; # Set this to 1 to release the lease before renewing.  Some 
                 # people who otherwise get a one hour lease find they get a 
                 # one day lease if they try this.

get( "http://$user:$password\@$ip/Gozila.cgi?dhcpAction=0" ) if $release;
get( "http://$user:$password\@$ip/Gozila.cgi?dhcpAction=1" );

sub get {
    my $url = shift;
    
    my $ua = LWP::UserAgent->new;
    my $req = HTTP::Request->new(
	GET => $url,
    );

    my $res = $ua->request($req);
    unless ($res->is_success) {
	print $res->status_line, "\n";
    }
}
