Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/perl -w
use strict;
# Reboot a Surfboard modem because it'll give wired connections a little bit of
# bandwidth while hogging wifi users are forced to renegotiate a connection.
# >:-)
# Last updated 2016-06-20
use constant USER => '*****';
use constant PASS => '*****';
use constant ROUTER_IP => '';
use constant COOKIES => "$ENV{'HOME'}/cookies.txt";
use constant LOGIN_FORM => 'splashLogin'; # form element name/id
use constant LOGIN_USER => 'userId'; # input element name
use constant LOGIN_PASS => 'password';
use constant REBOOT_FRAME => 'left';
use constant REBOOT_FORM=> 'rebootForm';
use LWP::UserAgent qw//;
use HTML::Form qw//;
use HTTP::Cookies qw//;
use Getopt::Std qw/ getopts /;
my %opts = (
'v' => 0,
'i' => ROUTER_IP,
'c' => COOKIES,
'u' => USER,
'p' => PASS,
&getopts('vi:c:u:p:', \%opts);
my $ua = LWP::UserAgent->new(
'cookie_jar' => HTTP::Cookies->new(
'file' => $opts{'c'},
'autosave' => 1,
# pull the login form out of the splash page
print STDERR 'logging in...' if $opts{'v'};
my $login = &getform($ua->get('http://' . $opts{'i'}), LOGIN_FORM);
die 'failed to retrieve login form' unless $login;
# fill in form values
# note: when using strict, this will croak if it can't find the input
$login->value(LOGIN_USER, $opts{'u'});
$login->value(LOGIN_PASS, $opts{'p'});
# send the HTTP POST request from the form
my $login_response = $ua->request($login->click);
# get the URL of the frame with the reboot form on it
# (simpler to just perform a regex)
my $framename = REBOOT_FRAME;
if ($login_response->content =~ /<frame\s+([^>]*)name=['"]($framename)['"]([^>]*)>/) {
print STDERR "done\n" if $opts{'v'};
my $attrs = $1 . $3;
if ($attrs =~ /src=['"]([^'"]+)['"]/) {
print STDERR 'retrieving reboot form...'
if $opts{'v'};
my $framesrc = 'http://' . $opts{'i'} . '/' . $1;
my $reboot = &getform($ua->get($framesrc), REBOOT_FORM);
die 'cannot find reboot form on toolbar frame' unless $reboot;
print STDERR "done\n"
if $opts{'v'};
# it's an empty form, so no need to fill in the values before
# submitting
print STDERR 'sending reboot request...'
if $opts{'v'};
print STDERR "done\n"
if $opts{'v'};
} else {
print STDERR "failed\n"
if $opts{'v'};
die 'cannot find toolbar frame';
# retrieves a named form from a HTTP::Response object
sub getform {
my ($resp, $form) = @_;
my $login_form = (grep {
my $form_name = $_->attr('id') || $_->attr('name');
$form_name and $form_name eq $form
} HTML::Form->parse($resp)
die 'failed to retrieve login form' unless $login_form;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.