脚本是从excel中读取用户信息,然后再用LWP模块模拟登陆提交表单。
额,windows平台执行的,windows平台默认编码gbk,butterfly 是utf-8的,所以需要转码。
程序有个小bug,excel中的用户截止日期列中的数据请先将客户给的用户信息数据黏贴到文本文件中,然后再复制到程序目录下的excel文件中。
userinfo.xlsx addpeople.pl cookie2.txt 为 同一目录下的3个文件
addpeople.pl
如下
#!/usr/bin/perl -w
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Cookies;
use Encode;
use Encode::CN;
use Win32::OLE;
use FindBin;
my $targetIP="10.112.9.90";
my $targeturl ='http://'.$targetIP.':8082/hds/user/login';
my $addpeopleurl ='http://'.$targetIP.':8082/hds/user/add-user';
my @allusers =OpenFileReadData();
my $usercount =@allusers ;
my $ua=new LWP::UserAgent;
&login("admin","111111");
for(my $i =0; $i<$usercount ;$i++ ){
my $str = $allusers[$i] ;
my @testuser = split /\s+/,$str;
printf $testuser[0]."======".encode("gbk", decode("utf8",$testuser[1]))."======".encode("gbk", decode("utf8",$testuser[2]))."======".$testuser[3]."======".$testuser[4]."\n";
# sleep(1);
# &AddUser(@testuser );
}
sub login{
my ($username,$password)=@_;
my $cfile="cookie2.txt";
$cfile = File::Spec->catfile($FindBin::Bin,$cfile);
my $cookie_jar = HTTP::Cookies->new(
file => $cfile,
autosave => 1,
ignore_discard => 1
);
#$cookie_jar->load($cfile);
$ua->cookie_jar($cookie_jar);
my @heads=('Accept'=>qq(text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8),
'Accept-Encoding'=>'gzip, deflate',
'Accept-Language'=>'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3',
'Connection'=>'keep-alive',
'Host'=>qq($targetIP:8082),
'Referer'=>qq(http://$targetIP:8082/hds/user/login.html?s=1&next=)
);
my @logdata=[ uname => $username,
password => $password,
urlSchemeReferer => 'https',
action => 'login',
nextUrl => '',
sys_locale => 'zh_CN',
];
my $r=$ua->post($targeturl,@logdata,@heads);
if(!$r->is_error){
$cookie_jar->extract_cookies($r);
# printf "======".$r->content."\n========" ;
}else{print "未知错误n";}
my $temp=qq(