Advertisement
Guest User

reboot-surfboard.pl

a guest
Jun 1st, 2010
1,246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.51 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use strict;
  3.  
  4. use constant USER   => '*****';
  5. use constant PASS   => '*****';
  6. use constant ROUTER_IP  => '192.168.0.1';
  7. use constant COOKIES    => "$ENV{'HOME'}/cookies.txt";
  8.  
  9. use constant LOGIN_FORM => 'splashLogin';   # form element name/id
  10. use constant LOGIN_USER => 'userId';        # input element name
  11. use constant LOGIN_PASS => 'password';
  12. use constant REBOOT_FRAME => 'left';
  13. use constant REBOOT_FORM=> 'rebootForm';
  14.  
  15. use LWP::UserAgent      qw//;
  16. use HTML::Form          qw//;
  17. use HTTP::Cookies       qw//;
  18. use Getopt::Std         qw/ getopts /;
  19.  
  20. my %opts = (
  21.     'v' => 0,
  22.     'i' => ROUTER_IP,
  23.     'c' => COOKIES,
  24.     'u' => USER,
  25.     'p' => PASS,
  26. );
  27.  
  28. &getopts('vi:c:u:p:', \%opts);
  29.  
  30. my $ua = LWP::UserAgent->new(
  31.     'cookie_jar'    => HTTP::Cookies->new(
  32.         'file'      => $opts{'c'},
  33.         'autosave'  => 1,
  34.     ),
  35. );
  36.  
  37. # pull the login form out of the splash page
  38. print STDERR 'logging in...' if $opts{'v'};
  39. my $login = &getform($ua->get('http://' . $opts{'i'}), LOGIN_FORM);
  40.  
  41. die 'failed to retrieve login form' unless $login;
  42.  
  43. # fill in form values
  44. # note: when using strict, this will croak if it can't find the input
  45. $login->value(LOGIN_USER, $opts{'u'});
  46. $login->value(LOGIN_PASS, $opts{'p'});
  47.  
  48. # send the HTTP POST request from the form
  49. my $login_response = $ua->request($login->click);
  50.  
  51. # get the URL of the frame with the reboot form on it
  52. # (simpler to just perform a regex)
  53. my $framename = REBOOT_FRAME;
  54. if ($login_response->content =~ /<frame\s+([^>]*)name=['"]($framename)['"]([^>]*)>/) {
  55.     print STDERR "done\n" if $opts{'v'};
  56.     my $attrs = $1 . $3;
  57.     if ($attrs =~ /src=['"]([^'"]+)['"]/) {
  58.  
  59.         print STDERR 'retrieving reboot form...'
  60.             if $opts{'v'};
  61.  
  62.         my $framesrc = 'http://' . $opts{'i'} . '/' . $1;
  63.         my $reboot = &getform($ua->get($framesrc), REBOOT_FORM);
  64.        
  65.         die 'cannot find reboot form on toolbar frame' unless $reboot;
  66.  
  67.         print STDERR "done\n"
  68.             if $opts{'v'};
  69.  
  70.         # it's an empty form, so no need to fill in the values before
  71.         # submitting
  72.         print STDERR 'sending reboot request...'
  73.             if $opts{'v'};
  74.         $ua->request($reboot->click);
  75.  
  76.         print STDERR "done\n"
  77.             if $opts{'v'};
  78.     }
  79. } else {
  80.     print STDERR "failed\n"
  81.         if $opts{'v'};
  82.  
  83.     die 'cannot find toolbar frame';
  84. }
  85.  
  86. # retrieves a named form from a HTTP::Response object
  87. sub getform {
  88.     my ($resp, $form) = @_;
  89.  
  90.     my $login_form = (grep {
  91.             my $form_name = $_->attr('id') || $_->attr('name');
  92.             $form_name and $form_name eq $form
  93.         } HTML::Form->parse($resp)
  94.     )[0];
  95.  
  96.     die 'failed to retrieve login form' unless $login_form;
  97.  
  98.     $login_form
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement