Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

htaccess type authorization with WWW::Mechanize

by davido (Cardinal)
on Dec 13, 2004 at 07:39 UTC ( #414329=perlquestion: print w/replies, xml ) Need Help??

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( '', 'WGR614v4', 'user', 'password' ); $agent->get( '' ); print $agent->content();

The output is:

Error GETing Unauthorized at 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.


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( '', '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( '', '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.

      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.

      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.



Re: htaccess type authorization with WWW::Mechanize
by sasikumar (Monk) on Dec 13, 2004 at 09:19 UTC
    I am not familiar with these modules. Please look at this module and try this one. +ize/

    This could also help
    This can help you.
    Sasi Kumar

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://414329]
Approved by atcroft
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2023-02-06 12:10 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (34 votes). Check out past polls.