Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Lwp and NTLM authentication

by miniwinz (Initiate)
on Feb 10, 2012 at 14:39 UTC ( #953031=perlquestion: print w/replies, xml ) Need Help??
miniwinz has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to get a google analytics lwp script running. On my dev machine everything is working, however in production I have to use a HTTP proxy server with NTLM authentication.
I am looking for a working example script.
I had a look at Lwp::Authen::Ntlm, however it never sent any authentication header. The code was something like this.
use LWP::UserAgent; use LWP::Authen::Ntlm; $ua = LWP::UserAgent->new(keep_alive=>1); $ua->proxy('http://theproxy:80'); $ua->credentials('', '', "MyDomain\\MyUserCode", 'MyPassword'); $ua->get("");
I also had a look at Authen::NTLM::HTTP. Which looked promising, but documentation sucks badly. Is there any monk living in Microsoft world, who has solved this "standard" issue already?

Replies are listed 'Best First'.
Re: Lwp and NTLM authentication
by bcarroll (Pilgrim) on Feb 10, 2012 at 15:02 UTC
    The following code works well for me behind a proxy with NTLM authentication.
    my $ua = LWP::UserAgent->new; $ua->env_proxy; my $response = $ua->get($URL); if ($response->is_success) { print $response->decoded_content; }

      If $ua->env_proxy; doesn't work, here is another example I have used in the past.

      sub GetURL { ########## # # GetURL is a generic LWP sub used to GET a URL using the provided # proxy settings, upon success it returns the page content +s or UNDEF # ########## my $url = shift; my $userAgent = new LWP::UserAgent; $userAgent->timeout( 3 ); # 3 sec timeout $userAgent->proxy( 'http' , $PROXY ) if $PROXY; my $request = HTTP::Request->new( GET => "$url" ); $request->proxy_authorization_basic( $USERNAME , $PASSWORD ) if $P +ASSWORD; my $result = $userAgent->request( $request) ; if ( $result->is_success ) { return $result->content; } return undef; }
        Is there really no need to use of Authen::NTLM / LWP::Authen::Ntlm?
        Is it really going to set NTLM authentication whereas proxy_authorization_basic suggests that HTTP Basic authentication is used??

      In which environment variables do you have to set the user/password. How do you specify NTLM instead of BasicAuth?


        This is what I have...

        set http_proxy=http://DOMAIN\

        I guess I am using BasicAuth instead of NTLM...sorry

Re: Lwp and NTLM authentication
by Khen1950fx (Canon) on Feb 11, 2012 at 21:09 UTC
    Here's my stab at it. As far as I know, if you want to use a proxy, then there's no need for credentials; that is, you either use a proxy or use a server. This is what I came up with.
    #!/usr/bin/perl -l use strict; use warnings; use Authen::NTLM; use LWP::UserAgent; use HTTP::Request::Common; my $url = ''; my $ntlm = Authen::NTLM->new( host => $url, user => 'user', password => 'password', ); my $reply = $ntlm->challenge; my $ua = LWP::UserAgent->new(keep_alive => 1); $ua->env_proxy; $ua->protocols_allowed(['http']); my $req = GET $url; print "====Performing request now========="; my $res = $ua->request($req); if ($res->is_success) { print $res->content; } else { print "Error: " . $res->status_line . "\n"; print $res->headers()->as_string(), "\n"; } print "====Done with request==============="; ntlm_reset; exit;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://953031]
Approved by ww
Front-paged by codeacrobat
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2017-08-20 21:16 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (317 votes). Check out past polls.