http://qs1969.pair.com?node_id=414329

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

I've read the POD for WWW::Mechanize, and done a bit of searching and reading here at the Monastery, but haven't been able to come up with a solution to what I believe is probably a pretty simple question.

I want to log in to an htaccess-protected site via WWW::Mechanize. As an example of such a site I have been trying to log in to my router's configuration page. Here is a simple example script, which isn't successful:

use strict; use warnings; use WWW::Mechanize; my $agent = WWW::Mechanize->new( autocheck => 1 ); $agent->credentials( 'www.routerlogin.com', 'WGR614v4', 'user', 'password' ); $agent->get( 'http://www.routerlogin.com/s_status.htm' ); print $agent->content();

The output is:

Error GETing http://www.routerlogin.com/s_status.htm: Unauthorized at mytest.pl line 14

Note: The URL I'm using is automatically trapped by my router and associated with the router's configuration screen. I could also have just used the router's local IP address of 192.168......etc.

I know this can't be that difficult. After all, WWW::Mechanize is a proper subclass of LWP::UserAgent, but I just can't seem to quite get it right. Any suggestions or pointers to a good discussion on the topic would be greatly appreciated.


Dave

Replies are listed 'Best First'.
Re: htaccess type authorization with WWW::Mechanize
by bart (Canon) on Dec 13, 2004 at 10:00 UTC
    $agent->credentials( 'www.routerlogin.com', 'WGR614v4', 'user', 'password' );

    That's a weird realm (the second parameter).

    The "realm" is the info text that appears in the dialog box when you try to log in with a browser. I don't know how exact a fit the text you enter must be. I sincerely doubt that the text would ever be "WGR614v4".

    update: Even with the proper values filled in, it doesn't work for me. And yes, judging by the looks of examining the $agent with Data::Dumper, setting the credentials must have worked.

    update: Got it! You need a port number for the $netloc. Append a ":80" to the domain name, the first parameter, and authentication works. You still appear to need an exact realm, though. Leaving it blank, or otherwise filling in a partly matching string, just doesn't work.

    $agent->credentials( 'www.routerlogin.com:80', 'WGR614v4', 'user', 'password' );

      I sincerely doubt that the text would ever be "WGR614v4".
      It probably is if you're logging in to a particular Netgear router.


      davis
      It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.

      bart,
      You got it right in your final update. It appears that, as you suggested, I needed the port number. When I added that to the URL it began working just fine. I knew it had to be something simple that I just wasn't seeing. Little did I know how simple it would turn out to be.

      Thanks!


      Dave

Re: htaccess type authorization with WWW::Mechanize
by sasikumar (Monk) on Dec 13, 2004 at 09:19 UTC
    Hi
    I am not familiar with these modules. Please look at this module and try this one.
    http://search.cpan.org/~corion/WWW-Mechanize-Shell-0.34/lib/WWW/Mechan +ize/Shell.pm

    This could also help
    http://httpd.apache.org/docs/howto/auth.html#basicworks
    This can help you.
    Thanks
    Sasi Kumar