Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Perl Script to login to a secure WebSite

by kiran.b.naik (Initiate)
on Aug 02, 2012 at 23:47 UTC ( #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);

Comment on Perl Script to login to a secure WebSite
Download Code
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
      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.
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..
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
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? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2015-08-28 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...













    Results (339 votes), past polls