Solved

Perl fetch URL Username and Password

Posted on 2003-11-02
6
1,662 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connecting to linux server using Net::Telnet module 25 220
Passing shell scripts to run on the SFTP cmd object 12 180
Perl - Mawk 2 87
html form to write data to csv 24 96
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now