Perl WWW::Mechanize Credentials - perl

I want to search content from a site using WWW::Mechanize but to be able to do that first I must be logged in with my registered username and password, which I can't do with this code. What must be changed so I can submit the form successfully. Thanks.
use strict;
use warnings;
use WWW::Mechanize;
my $username = "username";
my $password = "password";
my $cookie_jar;
my $url = "http://www.albumartexchange.com/forums/ucp.php?mode=login";
my $mech = WWW::Mechanize->new( cookie_jar => $cookie_jar );
$mech->credentials($username, $password);
$mech->get($url);
$mech->success() or die "Failed";
$mech->submit_form(
form_number => 4,
);
die "Submit failed" unless $mech->success;
$mech->save_content('log.txt');
UPDATE:
use strict;
use warnings;
use WWW::Mechanize;
my $cookie_jar;
my $mech = WWW::Mechanize->new( cookie_jar => $cookie_jar );
$mech->get( 'http://www.albumartexchange.com/forums/ucp.php?mode=login' );
$mech->submit_form(
form_number => 4,
fields => {
'username'
=> 'some_username',
'password'
=> 'some_password',
}
);
$mech->save_content('log.txt');

You don't need credentials here. Just use:
$mech->submit_form(
form_number => 4,
fields => {
username => $user,
password => $pass,
},
);
Of course don't forget to change username and password to the actual field names for your target page.

That page doesn't use HTTP authentication, which is what credentials is for. Just use Mech to fill in the username and password form fields and submit the login form.

Related

Perl Mechanize, making a script to login on a webpage

I'm making a script to automatically login on a webpage, it starts with this:
use HTTP::Cookies;
use WWW::Mechanize;
my $cookie_jar = HTTP::Cookies->new;
my $agent = WWW::Mechanize->new( cookie_jar => $cookie_jar );
my $server_endpoint = "http://10.11.5.2/index.php";
$agent->post($server_endpoint,[tg => 'login', referer => 'index.php',login => 'login',sAuthType=>'LOL',nickname=>'admin',password=>'012345678',submit=>'Login']);
print "Set Cookie Jar?\n", $agent->cookie_jar->as_string, "\n";
print $agent->content;
And I get a page saying "you are not logged in"...but when I use the same credentials in the browser everything works.
So I retrieved the value of the cookie sent by the server (located in the set-cookie header of the response) with $agent->cookie_jar->as_string, here it's OV3176019645=3inkmpee0r5gpfm41c3iltvda1.
THen I put it in the POST request before sending it, like the following:
use HTTP::Cookies;
use WWW::Mechanize;
my $cookie_jar = HTTP::Cookies->new;
my $agent = WWW::Mechanize->new( cookie_jar => $cookie_jar );
my $server_endpoint = "http://10.11.5.2/index.php";
$agent->add_header( Cookie => 'OV3176019645=osovm5u0vfc2dmkuo6bqn6hah1' );
$agent->post($server_endpoint,[tg => 'login', referer => 'index.php',login => 'login',sAuthType=>'LOL',nickname=>'admin',password=>'012345678',submit=>'Login']);
print $agent->content;
This time everything works...
So, my question is: how can I automatically get the value of the cookie given by the server before I send my request ?
Another problem also appears, it's that the server sends back a cookie with the following shape (in the set-cookie header):
Set-Cookie3: OV3176019645=3inkmpee0r5gpfm41c3iltvda1; path="/"; domain=10.11.5.2; path_spec; discard; version=0
And I just need the 1st item of this cookie (OV3176019....).
I hope I was clear in my explanations.
Thanks

How to use WWW:Mechanize to login

I want to login to a persian blogging service . This is my code:
#!/usr/bin/perl
use WWW::Mechanize;
$mech = WWW::Mechanize->new();
$url = "http://blogfa.com/Desktop/Login.aspx?t=1";
$mech->get($url);
$result = $mech->submit_form(
form_name => 'aspnetForm', #name of the form
#instead of form name you can specify
#form_number => 1
fields =>
{
'master$ContentPlaceHolder1$Uid' => 'my username', # name of the input field and value
'master$ContentPlaceHolder1$Password' => 'my password',
}
,'master$ContentPlaceHolder1$btnSubmit' => 'ورود به بخش مدیریت' #name of the submit button
);
$result->content();
if ($result =~ /میز کار/) {
print "Done\n"; }
else {
print "Failed!\n"; }
But it doesn't work at all. What is the problem?
The problem is, that WWW:Mechanize does not execute javascript. Since the site you want to log in uses javascript for logging in, its not able to do that.
You could fix that problem by using WWW::Mechanize::Firefox, which allows you to execute javascript.
This should work:
my $mech = WWW::Mechanize->new();
$mech->get("http://blogfa.com/Desktop/Login.aspx?t=1");
$mech->submit_form(
with_fields => {
'master$ContentPlaceHolder1$Uid' => 'my username',
'master$ContentPlaceHolder1$Password' => 'my password',
},
button => 'master$ContentPlaceHolder1$btnSubmit',
);

POSTing to form using LWP::UserAgent gets no response (mostly)

Here is my dilemma: I am trying to fill out a web form and get a result back from that form using LWP::UserAgent. Here is an example of my code:
#!/usr/bin/perl -w
use strict;
use LWP;
use HTTP::Request::Common;
use LWP::Debug qw(+);
my $ua = LWP::UserAgent->new(protocols_allowed=>["https"]);
my $req = POST 'https://their.securesite.com/index.php',
[ 'firstName' => 'Me',
'lastName' => 'Testing',
'addressLine1' => '123 Main Street',
'addressLine2' => '',
'city' => 'Anyplace',
'state' => 'MN',
'zipCode' => '55555',
'card' => 'visa',
'cardNumber' => '41111111111111111',
'ccv2' => '123',
'exp_month' => '07',
'exp_year' => '2015',
'shared_key' => 'hellos',
];
my $response = $ua->request($req);
print $response->is_success() . "\n";
print $response->status_line . "\n";
print $response->content . "\n";
When I run this, I get back a 200 OK and a "1" for success, but not the response page from the form. Just the closing tags:
</body>
</html>
Could this possibly be due to the fact that the form page and response page both have the same URL? I am new to LWP, so I am grasping at straws here. It may still be on the clients end, but I want to rule out any issues on my end as well.
Thanks in advance for any help you guys can give - I am Googled out.
If you can use Mojo::UserAgent (part of the Mojolicious suite of tools) the code would look like this. Note that you might need IO::Socket::SSL in order to use HTTPS.
#!/usr/bin/env perl
use strict;
use warnings;
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
my $tx = $ua->post('https://their.securesite.com/index.php', form =>
{ 'firstName' => 'Me',
'lastName' => 'Testing',
'addressLine1' => '123 Main Street',
'addressLine2' => '',
'city' => 'Anyplace',
'state' => 'MN',
'zipCode' => '55555',
'card' => 'visa',
'cardNumber' => '41111111111111111',
'ccv2' => '123',
'exp_month' => '07',
'exp_year' => '2015',
'shared_key' => 'hellos',
});
if ( $tx->success ) {
print $tx->res->body;
# or work with the resulting DOM
# my $dom = $tx->res->dom;
} else {
my ($err, $code) = $tx->error;
print $code ? "$code response: $err\n" : "Connection error: $err\n";
}
The interface is a little different, but it has lots of nice features, including Mojo::DOM integration for parsing the response HTML.
Use $response->decoded_content to get the content without the headers. See HTTP::Message for more information.
#!/usr/bin/perl -w
use strict;
use URI;
use LWP::UserAgent;
use HTTP::Request;
my $url = URI->new('https://their.securesite.com/index.php');
my $ua = LWP::UserAgent->new();
my $request = HTTP::Request->new(
'POST',
$url,
HTTP::Headers->new(
'User-Agent' => "perl ua/ v0.001",
'Accept' => "text/xml, multipart/*, application/soap"
),
[ 'firstName' => 'Me',
'lastName' => 'Testing',
'addressLine1' => '123 Main Street',
'addressLine2' => '',
'city' => 'Anyplace',
'state' => 'MN',
'zipCode' => '55555',
'card' => 'visa',
'cardNumber' => '41111111111111111',
'ccv2' => '123',
'exp_month' => '07',
'exp_year' => '2015',
'shared_key' => 'hellos',
]
) or die "Error initiating Request: $#\n";
my $response = $ua->request( $request );
if ($response->is_success) {
print $response->decoded_content, "\n";
} else {
die $response->status_line;
}
Check the value of $response->as_string
It'll show you full http response with headers

Unable to login into a site using www:Mechanize

I am using WWW:Mechanize to try to login to a site.
Code
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
$mech->get("https://www.amazon.com/gp/css/homepage.html/");
$mech->submit_form(
form_name => 'yaSignIn',
fields => {
email => 'email',
qpassword=> 'pass'
}
);
print $mech->content();
However it is not being logged into the site. What am i doing wrong. The website redirects and says please enable cookies to continue. How do i do that .
Try putting this block before your get.
$mech->cookie_jar(
HTTP::Cookies->new(
file => "cookies.txt",
autosave => 1,
ignore_discard => 1,
)
);
SuperEdit2: I just tried this myself and it seemed to work. Give it a try.(changed the form number to 3 and added an agent alias)
use strict;
use warnings;
use WWW::Mechanize;
# Create a new instance of Mechanize
my $bot = WWW::Mechanize->new();
$bot->agent_alias( 'Linux Mozilla' );
# Create a cookie jar for the login credentials
$bot->cookie_jar(
HTTP::Cookies->new(
file => "cookies.txt",
autosave => 1,
ignore_discard => 1,
)
);
# Connect to the login page
my $response = $bot->get( 'https://www.amazon.com/gp/css/homepage.html/' );
# Get the login form. You might need to change the number.
$bot->form_number(3);
# Enter the login credentials.
$bot->field( email => 'email' );
$bot->field( password => 'pass' );
$response = $bot->click();
print $response->decoded_content;

How to ignore 'Certificate Verify Failed' error in perl?

I want to access a website where the certificate cannot be verified. I'm using WWW::Mechanize get request. So how would go about ignoring this and continues to connect to the website?
use IO::Socket::SSL qw();
use WWW::Mechanize qw();
my $mech = WWW::Mechanize->new(ssl_opts => {
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
verify_hostname => 0, # this key is likely going to be removed in future LWP >6.04
});
With IO::Socket::SSL earlier than 1.79, see PERL_LWP_SSL_VERIFY_HOSTNAME.
my $mech = WWW::Mechanize->new( 'ssl_opts' => { 'verify_hostname' => 0 } );