Created
April 3, 2019 03:50
-
-
Save h3xx/6648824ec45cb2551a7e825de5682b1d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 => '192.168.0.1'; | |
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'}; | |
$ua->request($reboot->click); | |
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) | |
)[0]; | |
die 'failed to retrieve login form' unless $login_form; | |
$login_form | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment