0

I am trying to retrieve an MRTG graph using Perl in Linux environment .

#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use Data::Dumper;

my $ua = LWP::UserAgent->new;
$ua->timeout(10);

my $response = $ua->get('http://www.myview.internetaccess.tatacommunications.com/cgi-bin/');


if ($response->is_success) {
    print $response->decoded_content;  # or whatever
}
else {
    die $response->status_line;
}

I used the code , but got the following error :-

  Error:   401 Authorization Required at mrtg.pl line 18.

Then I added this to the code ,

$ua->credentials("url","username","password");

But I get the same error . Can anyone please help me fix it . Thanks .

Newbie
  • 2,664
  • 7
  • 34
  • 75

1 Answers1

1

Check the documentation for LWP::UserAgent again, you're missing a parameter:

$ua->credentials( $netloc, $realm, $uname, $pass )

Get/set the user name and password to be used for a realm.

The $netloc is a string of the form "<host>:<port>". The username and password will only be passed to this server. Example:

$ua->credentials("www.example.com:80", "Some Realm", "foo", "secret");

Probably an easier method would be just to include the u/p in the url.

my $response = $ua->get('http://user:pass@www.myview.internetaccess.tatacommunications.com/cgi-bin/');
Miller
  • 34,962
  • 4
  • 39
  • 60
  • what is a realm in this context? – Newbie Apr 02 '14 at 08:07
  • Honestly, don't know. That's another reason why i suggested an alternative solution. – Miller Apr 02 '14 at 08:10
  • 1
    The realm is the identifier provided by the server when asking for a user/password. In Apache, it is the "AuthName" configuration parameter. If you don't know what the Realm is, or if it changes, then your only way around it is to define a superclass of LWP:UserAgent and override get_basic_credentials to return the credentials you want regardless of the $realm parameter. Then use this LWP::MyUserAgent instead. – Steve Shipway Apr 02 '14 at 21:46