Home > Archive > PERL Miscellaneous > October 2006 > Automatic login to a web page
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Automatic login to a web page
|
|
| Jagjeet_Singh 2006-10-24, 3:58 am |
| 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,
| |
| usenet@DavidFilmer.com 2006-10-24, 3:58 am |
| Jagjeet_Singh wrote:
> [ 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
...
--
David Filmer (http://DavidFilmer.com)
| |
| Jagjeet_Singh 2006-10-24, 3:58 am |
| Hi David,
Thanks for your reply, Actually I was looking for any perl script which
can be run
in background.
Regards,
Js
usenet@DavidFilmer.com wrote:
> Jagjeet_Singh wrote:
>
> 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
> ...
>
> --
> David Filmer (http://DavidFilmer.com)
| |
| zentara 2006-10-30, 7:09 pm |
| On 23 Oct 2006 23:32:29 -0700, "Jagjeet_Singh" <jagjeet.malhi@gmail.com>
wrote:
>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.
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
| |
| Brian Wilkins 2006-10-30, 7:09 pm |
|
zentara wrote:
> On 23 Oct 2006 23:32:29 -0700, "Jagjeet_Singh" <jagjeet.malhi@gmail.com>
> wrote:
>
>
[color=darkred]
>
> 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.
>
>
>
> --
> I'm not really a human, but I play one on earth.
> http://zentara.net/japh.html
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.
| |
| Jagjeet_Singh 2006-10-30, 7:09 pm |
| 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
Brian Wilkins wrote:
> zentara wrote:
>
>
>
> 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.
| |
| zentara 2006-10-30, 7:09 pm |
| On 25 Oct 2006 00:42:15 -0700, "Jagjeet_Singh" <jagjeet.malhi@gmail.com>
wrote:
>Thanks Zentara and Brian Wilkins for your reply.
>
>This is mentioned in my isp login page "Cookies must be enabled"
Here is how to use cookies with WWW::Mechanize
#!/usr/bin/perl
use WWW::Mechanize;
my $mechanize_with_a_memory_only_cookie_jar
=
WWW::Mechanize->new ( cookie_jar => {} );
# 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).
########################################
###############################
# You can create an in-memory cookie jar using the constructor of
# WWW::Mechanize. Using an existing cookie jar, you can use the
# HTTP::Cookies module. The file format this module uses is different
# from, for example, Mozilla. You can use HTTP::Cookies::Netscape
# or HTTP::Cookies::Explorer to read these browser-specific files.
# Using an existing file of cookies with WWW::Mechanize can be
# done as follows:
# ...
my $agent = WWW::Mechanize->new();
my $cj = HTTP::Cookies->new(file => "/file/to/cookie_jar");
$agent->cookie_jar($cj);
# ...
# (Substitute HTTP::Cookies::Netscape or HTTP::Cookies::Explorer to
# use a cookie file from those browsers.
>I ran All three examples -- Please excuse me If I was not able even to
>execute it properly
>Can't locate WWW/Mechanize.pm in @INC (@INC contains:
Go to http://search.cpan.org and search for WWW::Mechanize
Download and install it with,
perl Makefile.PL
make
make install ( as root)
>My ISP login page can be accessed using
>"http://210.7.90.193/indexmain.php"
I couldn't access that page, the browser just kept waiting.
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
| |
| Jagjeet_Singh 2006-10-30, 7:09 pm |
| 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
| |
| Tad McClellan 2006-10-30, 7:09 pm |
| Jagjeet_Singh <jagjeet.malhi@gmail.com> wrote:
> my $url = 'http://210.7.90.193/indexmain.php'
^^
> my $mech = WWW::Mechanize->new(autocheck => 1);
[snip]
> syntax error at a.pl line 15, near "my "
> Global symbol "$mech" requires explicit package name at a.pl line 15.
The semicolon character serves as a statement separator in Perl.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| zentara 2006-10-30, 7:09 pm |
| On 25 Oct 2006 08:36:04 -0700, "Jagjeet_Singh" <jagjeet.malhi@gmail.com>
wrote:
>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
>
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
|
|
|
|
|