Re: Automatic login to a web page



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:
On 23 Oct 2006 23:32:29 -0700, "Jagjeet_Singh" <jagjeet.malhi@xxxxxxxxx>
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

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.

.