此腳本的主要功能為:
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