Solved

Perl fetch URL Username and Password

Posted on 2003-11-02
6
1,666 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

791 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