perl 在butterfly的HDS中加用户

脚本是从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(

);

if($r->content=~ /$temp/ ) {
    die "login failed \n";
}

my $res = $ua->get(qq(http://$targetIP:8082/hds/user/show-user?userid=1) );
if(!$res->is_error){$cookie_jar->extract_cookies($res);
                   
                    }else{print "未知错误2n";}

 $cookie_jar->save($cfile);
 #print $cookie_jar->as_string ;
#print Dumper($ua);

}

sub AddUser{
  
    my ($login,$nickname,$department,$email,$validetime)=@_;
    my @params=[action=>'add-user',
                login=>$login,
                password=>'111111',
                password_again=>'111111',
                valid_start_time=>'2011-12-31',
                valid_end_time=>$validetime,
                first_name=>'',
                last_name=>'',
                nick_name=>$nickname,
                title=>'',
                department=>$department,
                email=>$email,
                office_phone=>'',
                home_phone=>'',
                mobile=>'',
                photo_url=>'',
                home_page=>''

       
    ];
   
    my $r=$ua->post($addpeopleurl,@params,);
    if(!$r->is_error){
        my $webcontent="";
       $webcontent = encode("gbk", decode("utf8", $r->content ));
        
      # printf $webcontent ;
        
    
     if(($webcontent=~/错误/  || $webcontent eq "")   ){
        printf "创建用户$login---$email 失败\n";
     }else{
        printf "创建用户$login---$email 成功\n";
     }
                     
     }else{ die "创建用户$login---$email,未知错误\n";}
   
   
}

sub OpenFileReadData{
 

$Win32::OLE::Warn = 3;

my $file = 'userinfo.xlsx';
   $file =  File::Spec->catfile($FindBin::Bin,$file);
my $savefile = $file;
 
my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit');
$Excel->{DisplayAlerts}=0;
my $Book = $Excel->Workbooks->Open($file);
my $Sheet = $Book->Worksheets(1);
my $row_counts = $Sheet->{UsedRange}->{Rows}->{Count};     # 得到行数
my $column_counts = $Sheet->{UsedRange}->{Columns}->{Count};    #得到列数
my $curr_row=$row_counts+1;  
my @data;
for(my $i=2;$i<=$row_counts ;$i++){
   
  my $login=  $Sheet->Cells($i,'A')->{'Value'}  ;
  my $nickname=  $Sheet->Cells($i,'B')->{'Value'}  ;
  my $department=  $Sheet->Cells($i,'C')->{'Value'}  ;
  my $email=  $Sheet->Cells($i,'D')->{'Value'}  ;
  my $expiredate = $Sheet->Cells($i,'E')->{'Value'}->Date('yyyy-MM-dd')  ;

  my $single =  qq($login $nickname $department $email $expiredate );
     $single =~ s/^\s+|\s+$//;  #去掉开头结尾非打印字符
    
     $single  =   encode("utf8", decode("gbk",$single ));
  push @data,$single ;
 
}
 $Book->Close();
$Excel->Quit();
return @data;


}

 

你可能感兴趣的:(perl,login,email,excel,file,action,Butterfly,User,Management,Perl)