Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Perl Script to login to a secure WebSite

by kiran.b.naik (Initiate)
on Aug 02, 2012 at 23:47 UTC ( [id://985122]=perlquestion: print w/replies, xml ) Need Help??

kiran.b.naik has asked for the wisdom of the Perl Monks concerning the following question:

Hi Experts, I am trying to login to a secure website (here using GMAIL) using perl script. PFB script I am using. This script executes fine but it is not able to login, when I open the outfile it shows main login screen only. its not going next page. Please help me here...
use strict; use warnings; use strict; use WWW::Mechanize; use HTTP::Cookies; my $outfile = "out2.htm"; my $url = "https://accounts.google.com/ServiceLogin?service=mail&passi +ve=true&rm=false&continue=http://mail.google.com/mail/&scc=1&ltmpl=de +fault&ltmplcache=2"; my $username = "<user>"; my $password = "<PWD>"; my $mech = WWW::Mechanize->new(); $mech->cookie_jar(HTTP::Cookies->new()); $mech->get($url); $mech->form_name('login_form'); $mech->field("Username", $username); $mech->field("Password", $password); $mech->submit_form(form_number => 1); my $output_page = $mech->content(); open(OUTFILE, ">$outfile"); binmode(OUTFILE, ":utf8"); print OUTFILE "$output_page"; close(OUTFILE);

Replies are listed 'Best First'.
Re: Perl Script to login to a secure WebSite
by ckj (Chaplain) on Aug 03, 2012 at 06:28 UTC
    You are using wrong form name and field names too. Through the same code I was able to login to gmail :
    use strict; use warnings; use strict; use WWW::Mechanize; use HTTP::Cookies; my $outfile = "out2.htm"; my $url = "https://accounts.google.com/ServiceLogin?service=mail&passi +ve=true&rm=false&continue=http://mail.google.com/mail/&scc=1&ltmpl=de +fault&ltmplcache=2"; my $username = 'yourgmailid'; my $password = 'yourpasswrd'; my $mech = WWW::Mechanize->new(); $mech->cookie_jar(HTTP::Cookies->new()); $mech->get($url); $mech->form_id('gaia_loginform'); $mech->field("Email", $username); $mech->field("Passwd", $password); $mech->click; my $output_page = $mech->content(); print $output_page; open(OUTFILE, ">$outfile"); binmode(OUTFILE, ":utf8"); print OUTFILE "$output_page"; close(OUTFILE);
    I also used print $output_page; to check the contents separately, you can ignore that one.
      Hi, I have used this script, its working fine. But I have one doubt that If particular System is slow in that case also this will work? or we need to do any changes in script. Please reply. Thanks
        can u please share u script in the reply.. because the above is not working properly
      Hi, Many thanks for your help, now I am able to login to Gmail and my another secure website, on which again I was using wrong Form details... Much appreciate your help on this :) Now I am trying to go the link which I get after login.. Plz let me know if follow_link(text => 'string') will work on this or do I need to use any other function...

        Plz let me know if follow_link(text => 'string') will work on this or do I need to use any other function...

        What happens when you T.I.T.S - Try It To See ?

        use strict; use warnings; use strict; use WWW::Mechanize; use HTTP::Cookies; my $outfile = "out2.htm"; my $url = "https://accounts.google.com/ServiceLogin?service=mail&passi +ve=true&rm=false&continue=http://mail.google.com/mail/&scc=1&ltmpl=de +fault&ltmplcache=2"; my $username = 'yourgmailid'; my $password = 'yourpasswrd'; my $mech = WWW::Mechanize->new(); $mech->cookie_jar(HTTP::Cookies->new()); $mech->get($url); $mech->form_id('gaia_loginform'); $mech->field("Email", $username); $mech->field("Passwd", $password); $mech->click; my $output_page = $mech->content(); print $output_page; open(OUTFILE, ">$outfile"); binmode(OUTFILE, ":utf8"); print OUTFILE "$output_page"; close(OUTFILE);
        ----------------------------------------------------------- Very thanks for the above script. After once logged in to the gmail I would like display output like "Log in successful", if log in fails I would like to display "Log in fail"; Please help me on the above condition. Thanks, you Jineesh.K
      Hi, I want to export/download the containt/data through CSV file from a https: site. Can you please suggest me a script and also suggest me how to run it, as I am a biginer for perl.
      when i am executing the above code, i am getting a error generated as : Can't locate WWW/Mechanize.pm in @INC (@INC contains: /usr/local/lib64/perl5 /us /perl5 /usr/share/perl5 .) at login.pl line 4. BEGIN failed--compilation aborted at login.pl line 4. can u pls resolve the issue asap..
        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Perl Script to login to a secure WebSite
by Anonymous Monk on Aug 03, 2012 at 00:06 UTC
      thanks for replying... Acutally my requirement is, I need to login to one of my secure website with username & password. After that I need to get some data from one more link in the next page.. So I need general code to login to secure website as Mail::Webmail::Gmail is only for GMAIL.. I will check on POP3, as of now I am not sure on this..
      When i am running yua code. there is a error which i generated as : Error: Can't locate object method "new" via package "Mail::Webmail::Gmail" (perhaps you forgot to load "Mail::Webmail::Gmail"?) at login1.pl line 4. Can u try to resolve the issue.. and come up with the new code.. What does it mean??

        The parent post does not mention Mail::Webmail::Gmail at all, so you are not running the code you are replying to.

        Note that the bug reports for Mail::Webmail::Gmail show that it has not been maintained since 2006, which makes it quite unlikely for a web scraper to still work.

        If you want to continue with the code you have, you will have to install Mail::Webmail::Gmail. Either use your OSes package manager to install the module or install it through the cpan tool:

        cpan Mail::Webmail::Gmail

        Most likely, you will find it much easier to acces Gmail through IMAP.

        You keep ignoring the advice given to your previous posts and by perl itself, you've not shown the code you are running. This isn't a code writing service.

        Please stop reposting the same (or almost identical) responses. Read the replies you have which address the issue. Hint, if you create an account and login then you'll ne notified or replies to your posts.

Re: Perl Script to login to a secure WebSite
by linuxkid (Sexton) on Aug 03, 2012 at 01:16 UTC

    if you know the names of the fields, try using LWP::UserAgent, and the post(); function.

    --linuxkid


    imrunningoutofideas.co.cc

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://985122]
Approved by ww
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-03-19 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found