holli has asked for the wisdom of the Perl Monks concerning the following question:

Brethren,
I'm struggling with this for a while now and it seems i can't get it to work. I have written a script to log into PM, it runs smooth, just it doesn't work/login.
use warnings; use strict; use Digest::SHA1 qw( sha1_hex ); use WWW::Mechanize; use HTTP::Cookies; use LWP::Debug qw(+); login($ARGV[0], $ARGV[1]); sub login { my ($user, $pass) = @_; my $kennung = sha1_hex("$user + $pass"); my $cookie_jar = HTTP::Cookies->new( file => "/cookies/$kennung.dat", autosave => 1, ); my $mech = WWW::Mechanize->new( cookie_jar => $cookie_jar ); my $resp = $mech->get( 'http://www.perlmonks.org?node_id=109' ); die "HTTP-Error 1\n" unless $resp->is_success; $resp = $mech->submit_form( form_name => 'login', fields => { user => $user, passwd => $pass, } ); die "HTTP-Error 2\n" unless $resp->is_success; if ( $resp->content =~ /<a href="\?node_id=109;op=logout">/ ) { print "log in ok"; } else { print "could not log in"; } }
This is the output of LWP::Debug
C:\devel>pmlogin.pl holli xxxxxxx LWP::UserAgent::new: () LWP::UserAgent::request: () HTTP::Cookies::add_cookie_header: Checking www.perlmonks.org for cooki +es HTTP::Cookies::add_cookie_header: Checking .perlmonks.org for cookies HTTP::Cookies::add_cookie_header: Checking perlmonks.org for cookies HTTP::Cookies::add_cookie_header: Checking .org for cookies LWP::UserAgent::send_request: GET http://www.perlmonks.org?node_id=109 LWP::UserAgent::_need_proxy: Not proxied LWP::Protocol::http::request: () LWP::Protocol::collect: read 847 bytes LWP::Protocol::collect: read 312 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 130 bytes LWP::UserAgent::request: Simple response: OK LWP::UserAgent::request: () HTTP::Cookies::add_cookie_header: Checking www.perlmonks.org for cooki +es HTTP::Cookies::add_cookie_header: Checking .perlmonks.org for cookies HTTP::Cookies::add_cookie_header: Checking perlmonks.org for cookies HTTP::Cookies::add_cookie_header: Checking .org for cookies LWP::UserAgent::send_request: POST http://www.perlmonks.org? LWP::UserAgent::_need_proxy: Not proxied LWP::Protocol::http::request: () LWP::Protocol::collect: read 847 bytes LWP::Protocol::collect: read 1648 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 1336 bytes LWP::Protocol::collect: read 310 bytes LWP::UserAgent::request: Simple response: OK could not log in
Of course I have doublechecked the credentials.


holli, /regexed monk/

Replies are listed 'Best First'.
Re: problem loggin into pm
by davidrw (Prior) on Jan 08, 2008 at 13:17 UTC
    Make it:
    passwd => substr($pass,0,8),
    And it should work.

    I d/l'd your script and ran into the same frustration ... Everything looked good compared to mech-dump 'http://www.perlmonks.org?node_id=109' and browser/ViewScource ... tried removing the cookie jar, changing the agent, etc.

    Finally went to Live HTTP headers in firefox to take the POST'd data to manually create a POST w/LWP (instead of mech), and then noticed that my password was passed as 12345678 when it's really 123456789 ... Checked the html source again and there is a maxlength=8 for the password field ...

    /me goes off to research now how to recognize that in WWW::Mechanize ...
    Update: See RFC: Where to patch to enforce maxlength in Mech? for thoughts on possibly adding a feature/patch for it.
      Many thanks!
      So the last 2 digits of my password are actually ignored, eh? That bugs me a little, because without them my password is an ordinary word, prone to dictionary attacks. I wonder what's the reason for this oviously arbitrary limitation.


      holli, /regexed monk/
Re: problem loggin into pm
by shmem (Chancellor) on Jan 08, 2008 at 13:37 UTC
    Typo:
    my $resp = $mech->get( 'http://www.perlmonks.org?node_id=109' ); # here ----------------------------------------^^

    That should be

    my $resp = $mech->get( 'http://www.perlmonks.org/?node_id=109' );
    "works for me"[tm] changing that ;-)
    (I guess WWW::Mechanize doesn't repost the credentials upon redirects)

    update: I see you want to store your cookie under /cookies - is that right? root dir? is that your folder, do you have write access to that?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      That doesnt change anything on my end.


      holli, /regexed monk/