in reply to Re: Basic authentication with LWP::UserAgent
in thread Basic authentication with LWP::UserAgent

Realm turned out to be model number of my router (default, changeable). So, after grabbing the realm, the modified version looks like:

#!/usr/bin/perl #use warnings; use strict; use LWP::UserAgent; my $ua = LWP::UserAgent->new(); $ua->credentials("192.168.0.103","DI-614+","admin","<OMITTED>"); my $response = $ua->get("http://192.168.0.1/st_devic.html"); die "Error while getting ", $response->request->uri, " -- ", $response->status_line, "\nAborting" unless $response->is_success; print $response->content, "\n";

Had to comment out the warnings because it wouldn't compile:

Can't locate warnings.pm in @INC (@INC contains: /usr/local/lib/perl5/ +site_perl/5.005/i386-freebsd /usr/local/lib/perl5/site_perl/5.005 . / +usr/libdata/perl/5.00503/mach /usr/libdata/perl/5.00503) at ip2.pl li +ne 3. BEGIN failed--compilation aborted at ip2.pl line 3.

I'm still getting the 401 error message with the code above. I've tried changing the first argument to be the router instead (192.168.0.1), with no luck. I know this can be done, for I can access the page through a web browser on the local net, and if I can do that, I should be able to have a script do the same, right?

Replies are listed 'Best First'.
Re: Re: Re: Basic authentication with LWP::UserAgent
by davis (Vicar) on Nov 13, 2003 at 09:21 UTC

    Ok, interesting. I tried that myself and I couldn't get it to work either. I then tried subclassing LWP::UserAgent, and overriding the get_basic_credentials method (you should have a program called lwp_request, the source of which should provide an example). I tested my bot with my local cups server, which requires HTTP authentication, and it worked.

    code shown below:

    #!/usr/bin/perl use warnings; use strict; use LWP::UserAgent; { #Create a new package that subclasses LWP::UserAgent. # we do this to allow overriding of the get_basic_credentials( +) method. package RequestAgent; our @ISA = qw(LWP::UserAgent); # All other subs would come from LWP::UserAgent. sub get_basic_credentials { #we only return the password for one location, so no m +agic necessary return ("user", "password"); } } my $ua = RequestAgent->new(); my $response = $ua->get("http://host:port/location"); die "Error while getting ", $response->request->uri, " -- ", $response->status_line, "\nAborting" unless $response->is_success; print $response->content, "\n";
    (You'll have to comment out the "use warnings" line again)
    I'm not sure why the first version didn't work - perhaps I didn't understand the docs correctly

    Update: LTjake got there first.


    davis
    It's not easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
Re: Re: Re: Basic authentication with LWP::UserAgent
by vek (Prior) on Nov 12, 2003 at 23:31 UTC

    The warnings pragma was only introduced in v5.6, you seem to be running a rather old version of Perl hence the error.

    -- vek --