Solved

Perl fetch URL Username and Password

Posted on 2003-11-02
6
1,678 Views
Last Modified: 2007-12-19
Hi

I am writing a script to fetch content of a page...this page requires a user name and password .... the login page is like this

<script language="javascript">
function validate(){
      if (document.LOGINfrm.USER.value==""){
            alert("No user ID supplied !")
            return false
      }

      if (document.LOGINfrm.PSWD.value==""){
            alert("No password supplied !")
            return false
      }
      return true
}
</script>

<FORM NAME="LOGINfrm" METHOD="POST" ACTION="/LOGIN" onSubmit="return validate()">

<TD ALIGN=RIGHT><B>User id:</B>&nbsp;</TD>
<TD><INPUT NAME="USER" SIZE=20 MAXLENGTH=20></TD>
<B>Password:</B>&nbsp;</TD>
<TD><INPUT TYPE="PASSWORD" NAME="PSWD" SIZE=20 MAXLENGTH=20></TD>

<INPUT TYPE="HIDDEN" NAME="REFER" VALUE="/cgi-bin/BRW?BRW=BDN=1234">

---------------------------------------------
this my script

#!c:/perl/bin/perl

use CGI::Carp;
use CGI::Carp qw(fatalsToBrowser);
use strict "vars";
use LWP::Simple;
print "Content-type: text/html\n\n";
$url = 'http://somesite.com/cgi-bin/BRW?BRW=BDN=1234';
$script_insides = get($url);
print $script_insides;

----------------------------------------------
How do i put my username and password in the script?

i have tried this already: 'http://username:password@somesite.com/cgi-bin/BRW?BRW=BDN=1234'


Cheers
0
Comment
Question by:baz_can_fix_it
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Expert Comment

by:cwp
ID: 9669168
See if you can get away with http://somesite.com/LOGIN?REFER=%2fcgi-bin%2fBRW%3fBRW%3dBDN%3d1234&USER=[username]&PSWD=[password]. If that doesn't work, you'll need something more complex than just stuff from LWP::Simple.
0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 9669266
Hello... Nope no luck...I even tried using it manually without the script.... I'll have a read on LWP::Simple...I'll get back to you...


Cheers
0
 
LVL 3

Accepted Solution

by:
cwp earned 30 total points
ID: 9669348
Try this:
   use HTTP::Request::Common;
   $ua = LWP::UserAgent->new;
   $response = $ua->request(POST 'http://somesite.com/LOGIN',
      [USER => [username],
       PSWD => [password],
       REFER => '/cgi-bin/BRW?BRW=BDN=1234'
      ]
   );

   if ($response->is_success) {
       print $response->content;
   } else {
       print $response->error_as_HTML;
   }

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:jmcg
ID: 9669377
The Javascript validate routine is not a barrier in this case. It's just checking that the two values are supplied, it's not changing them in any way.

Here's a script I've been using on Experts-Exchange. It solves nearly the same problem that you have with your somesite.com login form. I'd change it for you, but then I wouldn't be able to test it.

================
#! /usr/bin/env perl

use LWP;
use HTTP::Cookies;
use HTTP::Request::Common;

my $url = "http://www.experts-exchange.com/";

my $ua = LWP::UserAgent->new;
$cookie_jar = HTTP::Cookies->new( file=>'cookies.txt',
        autosave=>1);
$ua->cookie_jar($cookie_jar);
$ua->agent( 'Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Creative)');
push @{ $ua->requests_redirectable }, 'POST';

# establish session
my $req = POST $url . 'login.jsp' ,
   [
     redirectURL=>"/Programming/Programming_Languages/Perl/",
     msuLoginName=>'jmcg',
     msuPassword=>'put-a-real-password-here',
     msuRememberMe=>'on',
     msuLoginSubmit.x=>0,
     msuLoginSubmit.y=>0,
   ];

my $res = $ua->request( $req);

##      print "===== Request Headers =====\n";
##      print $req->headers->as_string;
##      print "===== Response Headers ====\n";
##      print $res->headers->as_string;
##      print "===== Response Content ====\n";
##      print $res->content;

# turn Expert mode on
my $req = GET $url . 'toggleExpertMode.jsp' .
     "?toggle=on" .
     "&redirectURL=/Programming/Programming_Languages/Perl/"
     ;

my $res = $ua->request( $req);

##      print "===== Request Headers =====\n";
##      print $req->headers->as_string;
##      print "===== Response Headers ====\n";
##      print $res->headers->as_string;
##      print "===== Response Content ====\n";
##      print $res->content;


while( <> ) {
    next unless /(Q_\d+)/;
    my ($qid) = $1;

    print STDERR "fetching $qid ...";
    my $req = GET ($url . $qid . '.html');
    my $res = $ua->request( $req );

    print STDERR " saving ...";
    if( $res->is_success) {
        open QID, ">../../ee_data/QID/$qid.html" or
          die "could not open QID file $qid: $!";
        print QID $res->content;
        close QID;
      } else {

      print 'Error: ', $res->status_line, "\n";
      print "===== Request Headers =====\n";
      print $req->headers->as_string;
      print "===== Response Headers ====\n";
      print $res->headers->as_string;
      print "===== Response Content ====\n";
      print $res->content;
      exit 1;
      }

    print STDERR "\n";
    sleep 2;
  }

0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 20 total points
ID: 9669379
http://somesite.com/LOGIN?USER=username&PSWD=base64-encoded-password

You can retrieve the values with CGI, like:
  use CGI; my $q new CGI;
  my $user = $q->param('USER');
0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 9675440
Hi cwp & ahoffmann together you make an good answer...so I'll increase the points and split....

Sorry jmcg I am sure yours probably would of worked...but too much modifications for my needs...

Cheers

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question