Advertisement
Guest User

Login code

a guest
Sep 19th, 2013
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.43 KB | None | 0 0
  1. # Insert into crawl_update
  2. # This will enable login to a Mindtouch Wiki, and possibly other sites with form-based authentication and http redirect instead of 401 codes.
  3. # See login form here: http://pastebin.com/cnRv96hv
  4.  
  5. use LWP::UserAgent;
  6. use HTML::Form;
  7. use HTTP::Cookies;
  8.    
  9. my $userAgent = LWP::UserAgent->new;
  10. $userAgent->timeout(30);
  11. my $loginUrl = "http://<<sitename>>/index.php?title=Special:Userlogin&returntotitle="; # change
  12.    
  13. # Form-auth
  14. my @inputs;
  15. my $inp;
  16. my $type;
  17. my $name;
  18. my @form_out;
  19. my $form;
  20. my $cookie = HTTP::Cookies->new;
  21. my $response = $userAgent->get($loginUrl);
  22. my @forms = HTML::Form->parse($response);
  23.        
  24. undef(@form_out);
  25. $form = shift(@forms);  #Not the right form
  26. $form = shift(@forms);  
  27. @inputs = $form->inputs;
  28.  
  29. for (my $i=0 ; $i<=$#inputs ; $i++)  {
  30.     $inp = $inputs[$i];
  31.     $type = $inp->type;
  32.     $name = $inp->name;
  33.    
  34.     if ($inp->name eq "username") {
  35.         push(@form_out,$name,$user);
  36.     }
  37.     elsif ($inp->name eq "auth_id") {
  38.         push(@form_out,$name,"16");
  39.     }
  40.     elsif ($inp->name eq "returntotitle") {
  41.         push(@form_out,$name,"");
  42.     }
  43.     elsif ($inp->name eq "returntourl") {
  44.         push(@form_out,$name,"");
  45.     }
  46.     elsif ($inp->name eq "password") {
  47.         push(@form_out,$name,$passw);
  48.     }      
  49. }
  50.  
  51. $response = $userAgent->post("http://wiki.hemit.helsemn.no/Special:UserLogin", \@form_out);
  52. $cookie->extract_cookies($response);
  53.  
  54. # After initializing the robot, insert:
  55. $robot->cookie_jar($cookie);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement