Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use strict;
- 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
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement