Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 810
  • Last Modified:

perl script to view https page that is password protected

I have this script from adam314 to view a https page but the page requires a userid and password, which i have.  How can I change the script to insert userid and password?


use Net::SSLeay qw(get_https);

($page) = get_https('www.bacus.pt', 443, '/');
die "No page\n" unless $page;

open(OUT,">page.txt") or die "output: $!\n";
print OUT $page;
close(OUT);
0
mcgilljd
Asked:
mcgilljd
  • 7
  • 5
  • 4
4 Solutions
 
Adam314Commented:
How does it ask for a username/password when you go to the page with a browser?
0
 
mcgilljdAuthor Commented:
yes when I go to page with browser it asks for user name, password
0
 
Adam314Commented:
Do you get a web page asking for user/pass?  Or a window pop-up asking for user/pass?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
TintinCommented:
Assuming it uses basic authentication:

use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$req = HTTP::Request->new(GET => 'https://www.bacus.pt/');
$req->authorization_basic('username', 'password');

open FILE, ">page.txt" or die "Can not open page.txt $!\n";
print FILE $ua->request($req)->as_string;
close FILE;
0
 
mcgilljdAuthor Commented:
it is a popup window
0
 
TintinCommented:
That means it uses basic authentication.
0
 
mcgilljdAuthor Commented:
Tintin, I tried to use your code.
It doesn't give an error, but it doesn't print to page.txt either

use LWP::UserAgent;
my $urlhome ="https://website.com/WebUI/Servlet/Reviewer?axn=review";
$ua = LWP::UserAgent->new;
$req = HTTP::Request->new(GET => $urlhome) or die "no page\n";
$req->authorization_basic('myuserID', 'mypassword');

open FILE, ">C:\Desktop\prices.txt" or die "Can not open page.txt $!\n";
print FILE $ua->request($req)->as_string;
close FILE;


0
 
TintinCommented:
Try

use LWP::UserAgent;
my $urlhome ="https://website.com/WebUI/Servlet/Reviewer?axn=review";
$ua = LWP::UserAgent->new;
$req = HTTP::Request->new(GET => $urlhome) or die "no page\n";
$req->authorization_basic('myuserID', 'mypassword');
$res = $ua->request($req);

die $res->status_line unless $res->is_success;

open FILE, ">C:/Desktop/prices.txt" or die "Can not open prices.txt $!\n";
print FILE $res->decoded_content;
close FILE;
0
 
Adam314Commented:
A small change to Tintin's code:

use LWP::UserAgent;
my $urlhome ="https://website.com/WebUI/Servlet/Reviewer?axn=review";
$ua = LWP::UserAgent->new;
$us->credentials( $netloc, $realm, $uname, $pass );   #NOTE This line is new
$req = HTTP::Request->new(GET => $urlhome) or die "no page\n";
$res = $ua->request($req);

die $res->status_line unless $res->is_success;

open FILE, ">C:/Desktop/prices.txt" or die "Can not open prices.txt $!\n";
print FILE $res->decoded_content;
close FILE;

## Another option is to override the get_basic_credentials function.  For simple stuff, this isn't needed.
0
 
mcgilljdAuthor Commented:
C:\>Perl  C:\PerlScripts\login3.pl
501 Protocol scheme 'https' is not supported (Crypt::SSLeay not installed) at C:
\PerlScripts\interactivelogin3.pl line 11.

I will install Crypt::SSLeay and try again later

0
 
mcgilljdAuthor Commented:
Ok i installed crypt::SSLeay and added line
use Crypt::SSLeay ;

Now I get
401 Unauthorized at line 12

But if I use browser to go to page it works.
0
 
TintinCommented:
Possibly the site you are trying to connect to requires cookies or looks at the user agent string.

Try adding

$ua->agent("Mozilla/4.0");

and see if it makes a difference.
0
 
Adam314Commented:
You shouldn't need to "use" Crypt::SSLeay - it will be used by the other modules if needed.

It sounds as if you've provided incorrect username and/or password (and/or netloc and/or realm if using the ->credentials function)

Are you sure you have these correct?  It is likely case sensitive.
0
 
mcgilljdAuthor Commented:
added

$ua->agent("Mozilla/4.0");

no change, i will have to try later
0
 
mcgilljdAuthor Commented:
I tried using credentials method here is my script

use LWP::UserAgent;
my $urlhome ="https://website.com/WebUI/Servlet/Reviewer?axn=review";
my $userid ="MyID";
my $password ="P@ssword";
my $netloc ="111.222.333.444:80";
my $realm ="REALM";
my $outfile ="C:\Documents and Settings\Desktop\interactiveprices.txt";
$ua = LWP::UserAgent->new;
$ua->credentials( $netloc, $realm, $userid, $password );  
$req = HTTP::Request->new(GET => $urlhome) or die "no page\n";

$res = $ua->request($req);
die $res->status_line unless $res->is_success;

open (OUTFILE, ">$outfile") or die "Can not open prices.txt $!\n";
print OUTFILE $res->decoded_content;
close OUTFILE;


I still get 401 unauthorized
my password and username are correct but I am not sure exactly what to use for "realm"
0
 
Adam314Commented:
If the password contains the @ symbol (or another special character), it needs to be escaped in the $password:
my $password = "P\@ssword";
Otherwise, it will be interpreted as the @ssword variable.

Is real really 'REALM'?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now