Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

WWW-Authenticate with [WWW::Mechanize::Firefox]

by ptizoom (Scribe)
on Jul 25, 2017 at 19:32 UTC ( [id://1196039]=perlquestion: print w/replies, xml ) Need Help??

ptizoom has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am trying to automate data scraping of a well know router which has not SNMP services. It needs to answer a basic authentication challenge which starts with this request:

    WWW-Authenticate: basic realm="'Default: admin/1234"
but with WWW::Mechanize::Firefox, we do not seem to build this WWW-Authenticate response...

following the examples in... https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsILoginManager/Using_nsILoginManager I use this code:
use WWW::Mechanize::Firefox; use File::Temp qw/ :seekable /; my $url = 'http://192.168.1.1/'; my $formSubmitURL; my $httprealm = 'Default: admin/1234'; my $username = 'admin'; my $password = '1234'; my $res; my $hostname = $url; (my $u = "$url") =~ s!/$!!; my $mech = eval { WWW::Mechanize::Firefox->new( autodie => 0 , launch => '/usr/bin/firefox' || system(qq(which firefox) +) , autoclose => 0 , activate => 1 # bring the tab to the foreground , log => [qw[debug]] ) }; my $nsILoginManager = $mech->repl->expr(<<'JS'); Components.classes["@mozilla.org/login-manager;1"] .getService(Components.interfaces.nsILoginManager) JS my $logins = $nsILoginManager->findLogins({}, $url, $formSubmitURL, $h +ttprealm); my $count = $logins->{length}; my $nsILoginInfo; unless($count) { $nsILoginInfo = $mech->repl->expr(<<"JS"); var nsILI = new Components.Constructor( "\@mozilla.org/login-manager/loginInfo;1" , Components.interfaces.nsILoginInfo , "init" ); new nsILI("$hostname", null, "$httprealm", "$username", "$password +", "", "") JS # in resource://gre/components/nsLoginManager.js $res = $nsILoginManager->addLogin($nsILoginInfo); } (my $tpl_f = $url) =~ s/.*\/\///g; $tpl_f =~ s/\/.*//g; $tpl_f .= '_XXXXXX'; my $temp_p = File::Temp->newdir(TEMPLATE => $tpl_f, OPEN => 0); my %attr = (':content_file' => $temp_p->dirname . '.html', no_cache => + 1); $res = $mech->get($url, %attr);

but I always get this response ...

$mech->content '<html><head></head><body>WWW::Mechanize::Firefox</body></html>'
which is the first page.

Whereas remarkably, the file... "" $temp_p->dirname . '.html' "", contains the routers starting page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://ww +w.w3.org/TR/html4/frameset.dtd"> <html><head><meta http-equiv="Content-Type" content="text/html; chars +et=utf-8"> <title>Some Technology</title></head>(.v.)<noframes><body></body></no +frames></html>
  • what is the way to automatically build this basic authentication challenge response with WWW::Mechanize::Firefox ?
  • thx.

    Log In?
    Username:
    Password:

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others browsing the Monastery: (3)
    As of 2024-04-19 00:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found