Automatic login to a web page

J

Jagjeet_Singh

Hi All,

I am new to perl and do not have any idea about this language.

I need your help to solve my problem.

I have internet access on my desktop but each time my machine got
rebooted I need to log-in on
my ISP web's page by providing username,password and submit the button.

Some one told me that we can do this automatically using perl and use
of "LWP::Useragent"
and getcredential () module.

But I do not know perl, If this code is really small, can anyone post
it ..

Or if it takes time to write this then please provide me some from
where I can learn something to write this code.

I know I am fully expecting from others, But I would be really helpfull
if someone can do this for me.

like -- I use this url " http://my_isp_ip_address/indexmail.php" and
there are only 3 fields.

1 -- text box for username
2 -- text box for password
3 -- "Submit" button

Regards,
 
U

usenet

Jagjeet_Singh said:
[ a question about automating browser logins]

To CLPMisc: At first, when I read the OP's question, I thought, "no
way," but I got to wondering...

Would it be possible to use WWW::Mechanize (via a program in the
startup group) to obtain a cookie and put it in a directory which just
happened to also be the browser's cookie jar, and thus have the session
pre-authenticated when the browser starts up? That would be kinda
cool...
 
J

Jagjeet_Singh

Hi David,

Thanks for your reply, Actually I was looking for any perl script which
can be run
in background.

Regards,
Js

Jagjeet_Singh said:
[ a question about automating browser logins]

To CLPMisc: At first, when I read the OP's question, I thought, "no
way," but I got to wondering...

Would it be possible to use WWW::Mechanize (via a program in the
startup group) to obtain a cookie and put it in a directory which just
happened to also be the browser's cookie jar, and thus have the session
pre-authenticated when the browser starts up? That would be kinda
cool...
 
Z

zentara

I am new to perl and do not have any idea about this language.
I need your help to solve my problem.

I have internet access on my desktop but each time my machine got
rebooted I need to log-in on
my ISP web's page by providing username,password and submit the button.
But I do not know perl, If this code is really small, can anyone post
it ..

Or if it takes time to write this then please provide me some from
where I can learn something to write this code.

like -- I use this url " http://my_isp_ip_address/indexmail.php" and
there are only 3 fields.

1 -- text box for username
2 -- text box for password
3 -- "Submit" button

I'll give you some tips. First get something like tcpick or ethereal, so
you can watch in realtime what is going on during your login. Do it
from their webpage a few times and see what is actually happening.

Then you can use WWW::Mechanize to simulate it. Their are tons of
examples on the net, and WWW::Mech has alot of examples, look in the
man3 subdir of the module distribution.

A simple example
#!/usr/bin/perl
use warnings;
use strict;
use WWW::Mechanize;

my $username = 'user';
my $password = 'pass';
my $url = 'http://www.xyz.com/Login.aspx';
my $mech = WWW::Mechanize->new(autocheck => 1);

$mech->get( $url );
$mech->form_name("login form name");
$mech->field("username", $username);
$mech->field("password", $password);
$mech->click("login button/submit name");

$mech->content() =~ /sm.th. from Track.aspx/ and print "logged in\n"
__END__


As an alternative to WWW::Mechanize, you can also use a combination of
LWP::UserAgent and HTTP::Request::Common

#!/usr/bin/perl
use warnings;
use strict;
use HTTP::Request::Common qw(GET POST);
use LWP::UserAgent;

my $url ="http://my_isp/login.cgi";

my $ua = new LWP::UserAgent(timeout=> 5);

#now actually post the form
my $request = POST $url, [
username => 'foobar',
password => 'foobaz',
];

my $response = $ua->request($request);
my $content = $response->as_string();

print "$content\n";
__END__


###################################################

But like I said, you need to be able to watch your transaction with
tcpick or ethereal, to see exactly what you need to do. There are
many complications, like it may set a cookie.
 
B

Brian Wilkins

zentara said:
I am new to perl and do not have any idea about this language.
I need your help to solve my problem.

I have internet access on my desktop but each time my machine got
rebooted I need to log-in on
my ISP web's page by providing username,password and submit the button.
But I do not know perl, If this code is really small, can anyone post
it ..

Or if it takes time to write this then please provide me some from
where I can learn something to write this code.

like -- I use this url " http://my_isp_ip_address/indexmail.php" and
there are only 3 fields.

1 -- text box for username
2 -- text box for password
3 -- "Submit" button

I'll give you some tips. First get something like tcpick or ethereal, so
you can watch in realtime what is going on during your login. Do it
from their webpage a few times and see what is actually happening.

Then you can use WWW::Mechanize to simulate it. Their are tons of
examples on the net, and WWW::Mech has alot of examples, look in the
man3 subdir of the module distribution.

A simple example
#!/usr/bin/perl
use warnings;
use strict;
use WWW::Mechanize;

my $username = 'user';
my $password = 'pass';
my $url = 'http://www.xyz.com/Login.aspx';
my $mech = WWW::Mechanize->new(autocheck => 1);

$mech->get( $url );
$mech->form_name("login form name");
$mech->field("username", $username);
$mech->field("password", $password);
$mech->click("login button/submit name");

$mech->content() =~ /sm.th. from Track.aspx/ and print "logged in\n"
__END__


As an alternative to WWW::Mechanize, you can also use a combination of
LWP::UserAgent and HTTP::Request::Common

#!/usr/bin/perl
use warnings;
use strict;
use HTTP::Request::Common qw(GET POST);
use LWP::UserAgent;

my $url ="http://my_isp/login.cgi";

my $ua = new LWP::UserAgent(timeout=> 5);

#now actually post the form
my $request = POST $url, [
username => 'foobar',
password => 'foobaz',
];

my $response = $ua->request($request);
my $content = $response->as_string();

print "$content\n";
__END__


###################################################

But like I said, you need to be able to watch your transaction with
tcpick or ethereal, to see exactly what you need to do. There are
many complications, like it may set a cookie.

Or you can try Curl.

sub do_curl {

my $curl = Curl::easy::init();

if(!$curl) {
die "curl init failed!\n";
}

my $url = "enter your url here";
my $rawHTML = ""; # stores the return HTML

$::errbuf = "";
Curl::easy::setopt($curl, CURLOPT_ERRORBUFFER, "::errbuf");

Curl::easy::setopt($curl, CURLOPT_URL, $url);
Curl::easy::setopt($curl, CURLOPT_NOPROGRESS, 1);
Curl::easy::setopt($curl, CURLOPT_TIMEOUT, 30);
Curl::easy::setopt($curl, CURLOPT_HEADERFUNCTION, \&header_callb);
Curl::easy::setopt($curl, CURLOPT_WRITEFUNCTION, \&body_callb);
Curl::easy::setopt($curl, CURLOPT_POST, 1);
Curl::easy::setopt($curl,
CURLOPT_POSTFIELDS,"username=xyz&password=xyz");
Curl::easy::setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
Curl::easy::setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
Curl::easy::setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0");
Curl::easy::perform($curl);
Curl::easy::cleanup($curl);

}

# Used with cURL; stores the raw HTML retrieved

sub body_callb {
my($chunk,$handle)=@_;
${handle} .= $chunk;
$rawHTML .= $chunk;
return length($chunk);

}

# Used with cURL; gets header for debugging purposes.

sub header_callb {
return length($_[0]);

}

If it sets a cookie, Curl can handle that too, but it gets more
complicated. It's hard for us to give you an accurate script without
having a valid logon and a way to test.
 
J

Jagjeet_Singh

Thanks Zentara and Brian Wilkins for your reply.

This is mentioned in my isp login page "Cookies must be enabled"

I ran All three examples -- Please excuse me If I was not able even to
execute it properly


I took the first code and copied in 1.pl file.

and run like " perl 1.pl"

It gave me this output ..

Can't locate WWW/Mechanize.pm in @INC (@INC contains:
/usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0
/usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi
/usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl
/usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi
/usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl
/usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 .) at
1.pl line 4.


and with second example [ using LWP::UserAgent] I copied it to 2.pl and
run it by same way

and it prints all the html code of browser ---



And with third example [ using Curl ]

I ran it like "perl 3.pl" but it is not giving any output and comming
outout with fraction of second.

Please tell me if I am not running/doing the things properly.

My ISP login page can be accessed using
"http://210.7.90.193/indexmain.php"

I replaced variables with my url and username,password.

Regards,
Jagjeet Singh


zentara said:
I am new to perl and do not have any idea about this language.
I need your help to solve my problem.

I have internet access on my desktop but each time my machine got
rebooted I need to log-in on
my ISP web's page by providing username,password and submit the button.
But I do not know perl, If this code is really small, can anyone post
it ..

Or if it takes time to write this then please provide me some from
where I can learn something to write this code.

like -- I use this url " http://my_isp_ip_address/indexmail.php" and
there are only 3 fields.

1 -- text box for username
2 -- text box for password
3 -- "Submit" button

I'll give you some tips. First get something like tcpick or ethereal, so
you can watch in realtime what is going on during your login. Do it
from their webpage a few times and see what is actually happening.

Then you can use WWW::Mechanize to simulate it. Their are tons of
examples on the net, and WWW::Mech has alot of examples, look in the
man3 subdir of the module distribution.

A simple example
#!/usr/bin/perl
use warnings;
use strict;
use WWW::Mechanize;

my $username = 'user';
my $password = 'pass';
my $url = 'http://www.xyz.com/Login.aspx';
my $mech = WWW::Mechanize->new(autocheck => 1);

$mech->get( $url );
$mech->form_name("login form name");
$mech->field("username", $username);
$mech->field("password", $password);
$mech->click("login button/submit name");

$mech->content() =~ /sm.th. from Track.aspx/ and print "logged in\n"
__END__


As an alternative to WWW::Mechanize, you can also use a combination of
LWP::UserAgent and HTTP::Request::Common

#!/usr/bin/perl
use warnings;
use strict;
use HTTP::Request::Common qw(GET POST);
use LWP::UserAgent;

my $url ="http://my_isp/login.cgi";

my $ua = new LWP::UserAgent(timeout=> 5);

#now actually post the form
my $request = POST $url, [
username => 'foobar',
password => 'foobaz',
];

my $response = $ua->request($request);
my $content = $response->as_string();

print "$content\n";
__END__


###################################################

But like I said, you need to be able to watch your transaction with
tcpick or ethereal, to see exactly what you need to do. There are
many complications, like it may set a cookie.

Or you can try Curl.

sub do_curl {

my $curl = Curl::easy::init();

if(!$curl) {
die "curl init failed!\n";
}

my $url = "enter your url here";
my $rawHTML = ""; # stores the return HTML

$::errbuf = "";
Curl::easy::setopt($curl, CURLOPT_ERRORBUFFER, "::errbuf");

Curl::easy::setopt($curl, CURLOPT_URL, $url);
Curl::easy::setopt($curl, CURLOPT_NOPROGRESS, 1);
Curl::easy::setopt($curl, CURLOPT_TIMEOUT, 30);
Curl::easy::setopt($curl, CURLOPT_HEADERFUNCTION, \&header_callb);
Curl::easy::setopt($curl, CURLOPT_WRITEFUNCTION, \&body_callb);
Curl::easy::setopt($curl, CURLOPT_POST, 1);
Curl::easy::setopt($curl,
CURLOPT_POSTFIELDS,"username=xyz&password=xyz");
Curl::easy::setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
Curl::easy::setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
Curl::easy::setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0");
Curl::easy::perform($curl);
Curl::easy::cleanup($curl);

}

# Used with cURL; stores the raw HTML retrieved

sub body_callb {
my($chunk,$handle)=@_;
${handle} .= $chunk;
$rawHTML .= $chunk;
return length($chunk);

}

# Used with cURL; gets header for debugging purposes.

sub header_callb {
return length($_[0]);

}

If it sets a cookie, Curl can handle that too, but it gets more
complicated. It's hard for us to give you an accurate script without
having a valid logon and a way to test.
 
J

Jagjeet_Singh

Zentara,

I have downloaded this install as per instructions given by you.

And I have copied old code + new code [ for cokies ] into one file and
execute it
and getting this error.

################################################

[root@Vicky root]# cat a.pl
#!/usr/bin/perl
use warnings;
use strict;
use WWW::Mechanize;

my $mechanize_with_a_memory_only_cookie_jar =
WWW::Mechanize->new ( cookie_jar => {} );
my $agent = WWW::Mechanize->new();
my $cj = HTTP::Cookies->new(file => "/file/to/cookie_jar");
$agent->cookie_jar($cj);

my $username = 'jagjeetm';
my $password = 'hello';
my $url = 'http://210.7.90.193/indexmain.php'
my $mech = WWW::Mechanize->new(autocheck => 1);

$mech->get( $url );
$mech->form_name("login form name");
$mech->field("username", $username);
$mech->field("password", $password);
$mech->click("login button/submit name");

$mech->content() =~ /sm.th. from Track.aspx/ and print "logged in\n"


# update: By the way, that's the same as writing
my $mechanize_with_a_memory_only_cookie_jar =
WWW::Mechanize->new(); #(that's the default).

################################################

[root@Vicky root]# perl a.pl

syntax error at a.pl line 15, near "my "
Global symbol "$mech" requires explicit package name at a.pl line 15.
Global symbol "$mech" requires explicit package name at a.pl line 17.
Global symbol "$mech" requires explicit package name at a.pl line 18.
Global symbol "$mech" requires explicit package name at a.pl line 19.
Global symbol "$mech" requires explicit package name at a.pl line 20.
Global symbol "$mech" requires explicit package name at a.pl line 21.
Global symbol "$mech" requires explicit package name at a.pl line 23.
syntax error at a.pl line 27, near "my "
Execution of a.pl aborted due to compilation errors.


Regards,
Jagjeet Singh
 
Z

zentara

Zentara,

I have downloaded this install as per instructions given by you.

And I have copied old code + new code [ for cokies ] into one file and
execute it
and getting this error.

We are not going to hold your hand, while you make every
little Perl mistake that a newbie ever encounters. Why don't
you learn a little about Perl first, by reading a book, or
tutorial? You just can't slap together copies of code and
expect them to work.

There is a perl.beginners maillist at
http://learn.perl.org/
################################################
[root@Vicky root]# cat a.pl
#!/usr/bin/perl
use warnings;
use strict;
use WWW::Mechanize;

my $mechanize_with_a_memory_only_cookie_jar =
WWW::Mechanize->new ( cookie_jar => {} );
my $agent = WWW::Mechanize->new();
my $cj = HTTP::Cookies->new(file => "/file/to/cookie_jar");
$agent->cookie_jar($cj);

my $username = 'jagjeetm';
my $password = 'hello';
my $url = 'http://210.7.90.193/indexmain.php'
my $mech = WWW::Mechanize->new(autocheck => 1);

You don't use the cookie setup properly, you are just
copying code verbatim, and hoping it will work.
You set up 3 separate WWW::Mechanize objects, and
end up using the one without a cookie jar. You need to choose
just 1.

Try:

my $mech = WWW::Mechanize->new ( cookie_jar => {} );

then:
$mech->get( $url );
$mech->form_name("login form name");
$mech->field("username", $username);
$mech->field("password", $password);
$mech->click("login button/submit name");

print $mech->content,"\n";
$mech->content() =~ /sm.th. from Track.aspx/ and print "logged in\n"
#the above regex to Track.aspx surely will not work in your situation
#you need to customize it to what you receive back
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top