天天看点

常用脚本 – perl登陆

此脚本的主要功能为:
1、利用perl的LWP模块登陆指定的网站。
2、返回登录是否成功的状态。      
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Cookies;
$_=@ARGV;
if($_!=2){
  print "请输入正确的参数n";
  print "login.pl  用户名 密码"; 
}else{
my  $user=$ARGV[0];
my  $pwd=$ARGV[1];
    &login($user,$pwd);
}
 
sub login{
my ($a,$b)=($_[0],$_[1]);
my $url="http://passport.baidu.com/?login";
my $cfile="cookie/${a}.txt";
my $ua=new LWP::UserAgent;
my $cookie_jar=new HTTP::Cookies;
$cookie_jar->load($cfile);                 #load cookie file
my @heads=('User-Agent' => 'Mozilla/4.76 [en] (Win98; U)',
           'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
           'Accept-Charset' => 'iso-8859-1,*,utf-8',
           'Accept-Language' => 'zh-cn',
           'Connection'=>'Keep-Alive',
           'Content-Type'=>'application/x-www-form-urlencoded',
           'Referer' => 'http://passport.baidu.com/?login&tpl=mn&u=http%3A//www.baidu.com/',
           );
my @logdata=["tpl_ok" => "",
             "next_target" => "",
             "tpl" => "mn",
             "skip_ok" => "",
             "aid" => "",
             "need_pay" => "",
             "need_coin" => "",
             "pay_method" => "",
             "u" => "http%3A%2F%2Fwww.baidu.com%2F",
             "return_method" => "get",
             "more_param" => "",
             "return_type" => "",
             "psp_tt" => "0",
             "username" => $a,                             #用户名
             "password" => $b,                           #密码
             "mem_pass" => "on",
             ];           
 
my $r=$ua->post($url,@logdata,@heads);
if(!$r->is_error){$cookie_jar->extract_cookies($r);}else{print "未知错误n";}
if(length($r->header("title"))==8){
    print "登录成功!n";
    print "cookie文件已存入 ==>  ${cfile}" if $cookie_jar->save($cfile);   
}else{
    print "登录失败!n";
    print $r->header("title")."n";
    }
}      
转载:http://www.lazysa.com/applications/perl_lwp_login.html