in reply to Basic authentication with LWP::UserAgent

I'll admit, I'm a bit iffy on the realm thing...
I'm assuming your D-link router does HTTP Basic authentication here. To be certain of the realm, use a web-browser to visit the page, and pay attention to the dialogue box that you get. With my browser, I get the string "Please enter username and password for "Realmname"..." — this gives you the realm name you should be using.

Here's my attempt at what you might want... untested, because I don't have access to a D-Link router, and I'm a bit too lazy to set up a webserver with HTTP Basic authentication.

#!/usr/bin/perl use warnings; use strict; use LWP::UserAgent; my $ua = LWP::UserAgent->new(); $ua->credentials("192.168.0.1","192.168.0.1","admin", "password"); 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";

3rd Update (yikes!): You want to use the server's location as the first argument to credentials. I missed the different IP addresses when I first read your code.


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.

Update: Apparently I'm confusing Latex commands with HTML entities. Sigh

Replies are listed 'Best First'.
Re: Re: Basic authentication with LWP::UserAgent
by abeal (Initiate) on Nov 12, 2003 at 19:07 UTC

    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?

      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.

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

      -- vek --