Unable to login into a site using www:Mechanize - perl

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;

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',
);

Perl and LWP not authenticating

I'm trying to get an LWP request working to an https server. I have been given a user & pass, advised to use basic authentication. I've tried various chunks of code, and all seem to get an authentication error. My current code is...
use warnings;
use strict;
use Data::Dumper;
use LWP;
my $ua = LWP::UserAgent->new( keep_alive => 1 );
##also tried by $ua->credentials('domain','','user','pass');
##not sure if I need 'realm' or how I get it, as no popup on screen.
my $request = HTTP::Request->new( GET => "https://my.url.com/somepath/" );
$request->authorization_basic('myuser','mypass');
$request->header( 'Cache-Control' => 'no-cache' );
print $response->content;
print Dumper $response;
The server gives a security error, but if I look at a dump of $response, I see the following...
'_rc' => '401',
'_headers' => bless( { .... lots of stuff
'title' => 'Security Exception',
'client-warning' => 'Missing Authenticate header',
'client-ssl-socket-class' => 'IO::Socket::SSL',
...
'expires' => '-1'
}, 'HTTP::Headers' ),
'_msg' => 'Unauthorized',
'_request' => bless( {
'_content' => '',
'_uri' => bless( do{\(my $o = 'https:theurlabove')}, 'URI::https' ),
'_method' => 'GET',
'_uri_canonical' => $VAR1->{'_request'}{'_uri'}
'_headers' => bless( {
'user-agent' => 'libwww-perl/6.04',
'cache-control' => 'no-cache',
'authorization' => 'Basic dzx..........'
}, 'HTTP::Headers' ),
I'm trying to understand whats happening, it looks like in the original request, it has the headers in there, but in the response, its saying I'm 'Missing Authenticate Header'.
Is there something amiss with the code, or something I'm misunderstanding with the request/respinse ?
Thanks.
The "Missing Authenticate header" message is coming from LWP itself. This means that it couldn't find an authenticate header in the target response. This might mean that your proxy settings are misconfigured, if you have anything like that.
I don't know if this is what you are looking for but I came across the same problem trying to authenticate to a webpage and had to solve it with WWW::Mechanize. I had to go to the first page and login then request the page I wanted.
use WWW::Mechanize;
my $loginPage = "http://my.url.com/login.htm"; # Authentication page
my $mech = WWW::Mechanize->new(); # Create new brower object
$mech->get($loginPage); # Go to login page
$mech->form_name('LogonForm'); # Search form named LogonForm
$mech->field("username", myuser); # Fill out username field
$mech->field("password", mypass); # Fill out password field
$mech->click("loginloginbutton"); # submit form
$mech->get("http://my.url.com/somepath/"); # Get webpage
# Some more code here with $mech->content()

Logging into Jenkins via Perl script

I am able to change a build's description with the following program. This will change the build's description to "FOO FOO FOO". Unfortunately, my login doesn't work. Right now, this test Jenkins build server has no security on it. However, on our regular Jenkins server, you need to be logged in to change a build's description.:
#! /usr/bin/env perl
use 5.12.0;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
use Data::Dumper;
use constant {
JENKINS_BASE => "http://build.vegibank.com/",
USER_ID => "buildguy",
PASSWORD => "swordfish",
};
use constant {
LOGIN_URL => JENKINS_BASE . '/j_acegi_security_check',
JOB_URL => JENKINS_BASE . '/job',
SUBMIT_DESCRIPTION => 'submitDescription',
};
my $job_number = 4;
my $job_name = "proginator-2.0";
my $description = "FOO FOO FOO";
my $user_agent = LWP::UserAgent->new or die qq(Can't get User Agent);
#
# My Login Stuff (but it doesn't do anything w/ security off
#
my $response = $user_agent->request (
POST LOGIN_URL,
[
j_username => USER_ID,
j_password => PASSWORD,
],
);
$response = $user_agent->request (
POST "#{[JOB_URL]}/$job_name/$job_number/#{[SUBMIT_DESCRIPTION]}",
[
description => "$description",
],
);
I'm trying to connect to the Jenkins login session, but I don't believe I'm doing it quite right. When I attempt to login, I get a 302 response and the following dump of my response object:
$VAR1 = bless( {
'_protocol' => 'HTTP/1.1',
'_content' => '',
'_rc' => '302',
'_headers' => bless( {
'connection' => 'close',
'client-response-num' => 1,
'set-cookie' => 'JSESSIONID=1D5DF6FAF8714B2ACA4D496FBFE6E983; Path=/jenkins/; HttpOnly',
'location' => 'http://build.vegicorp.com/;jsessionid=1D5DF6FAF8714B2ACA4D496FBFE6E983',
'date' => 'Mon, 13 May 2013 20:02:35 GMT',
'client-peer' => '10.10.21.96:80',
'content-length' => '0',
'client-date' => 'Mon, 13 May 2013 20:02:35 GMT',
'content-type' => 'text/plain; charset=UTF-8',
'server' => 'Apache-Coyote/1.1'
}, 'HTTP::Headers' ),
'_msg' => 'Moved Temporarily',
'_request' => bless( {
'_content' => 'j_username=buildguy&j_password=swordfish',
'_uri' => bless( do{\(my $o = 'http://build.vegicorp.com/j_acegi_security_check')}, 'URI::http' ),
'_headers' => bless( {
'user-agent' => 'libwww-perl/6.03',
'content-type' => 'application/x-www-form-urlencoded',
'content-length' => 42
}, 'HTTP::Headers' ),
'_method' => 'POST',
'_uri_canonical' => $VAR1->{'_request'}{'_uri'}
}, 'HTTP::Request' )
}, 'HTTP::Response' );
I figure I must be hitting a valid page since I'm getting a 302 code, but my fields might not be correct (or I'm going to the wrong page).
Can anyone help?
My authorization is failing because ...what is the technical term? Oh yeah... "doing it all wrong."
After Googling and getting a lot of unhelpful junk, I, on a lark, decided to see if the Jenkins website might have something on this. And, it did right under a page called Authenticating scripted clients. In fact, they even give a Perl LWP example for a scripted client.
Ha ha, I was trying too hard. It seems that Jenkins will use the basic HTTP authentication mechanism, and I don't have to go through conniptions trying to figure out how their login form works. Apparently, Jenkins is simplifying the basic authentication mechanism for you even if your authentication mechanism is far from basic -- like a good program should do.
So, all I had to do was use the basic authentication mechanism.
my $browser = LWP::UserAgent->new or die qq(Cannot get User Agent);
my $request = HTTP::Request->new;
$request->authorization_basic(USER_ID, PASSWORD);
$request->method("GET");
$request->url("$jenkins_url");
my $response = $browser->request($request);
if ( not $response->is_success ) {
die qq(Something went horribly wrong...);
}
I've seen the redirect when the login is successful -- it sets the session cookie and redirects you to the main page.
Your post might be failing because the UA object isn't persisting the session cookie. Per the documentation, 'The default is to have no cookie_jar, i.e. never automatically add "Cookie" headers to the requests.' Try:
my $ua = LWP::UserAgent->new( cookie_jar => HTTP::Cookies->new() );
To store and reuse your session for the description change post.
(Also, credentials are visible in your header dump, may want to edit... Edit: I'm an idiot, they're in your constants too and're likely fake.)

Perl WWW::Mechanize Credentials

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.