Solved

Perl fetch URL Username and Password

Posted on 2003-11-02
6
1,664 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 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