perl 读取cookie

use  LWP::UserAgent;
use HTTP::Date qw(time2iso str2time time2iso time2isoz);
use Net::Ping;  
use Socket;
use Net::SMTP;
use LWP;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Headers;
use HTTP::Response;
use Encode;
use URI::Escape;
use URI::URL;
use File::Temp qw/tempfile/;
my $ua = LWP::UserAgent->new;
$ua->timeout(5);
$ua->env_proxy;
$ua->agent("Mozilla/8.0");
my $cookie_jar = HTTP::Cookies->new(
                                   file=>'lwp_cookies.txt',
                                   autosave=>1,
                                   ignore_discard=>1
                                   );
$ua->cookie_jar($cookie_jar);
my $login_url = 'http://192.168.32.83/zabbix/index.php';
my $res = $ua->post($login_url,{
	                  'autologin'=>'1',
                          'enter'=>'Sign in',
			  'name'=>'admin',
                          'password'=>'zabbix'
                               },
                           'User-Agent'=>'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0',
                           'Host'=>'192.168.32.83',
                           'Referer'=>'http://192.168.32.83/zabbix/'
                           );

            my $content = $res->content();
            print $res->status_line . "
";
            print $res->as_string();              
            print $res->content();
            
            open (LOG ,"<","/home/scan/lwp_cookies.txt");  
                    while (<LOG>) {  
                    chomp; 
                     $sid=$1   if ($_ =~ /.*zbx_sessionid=(.*?);.*/)
                                 };
              

             print $sid."
";;       
             
             $cookie_jar->scan(sub {
                  my ($k,$v)=@_[1,2];
                  if ($k eq 'zbx_sessionid'){
                         $sid=$v;
                           };
                      });
             print $sid."
";

[root@dr-mysql01 ~]# perl zz.pl 
302 Found
HTTP/1.1 302 Found
Connection: close
Date: Fri, 15 Jul 2016 02:04:10 GMT
Location: tr_status.php
Server: Apache/2.2.15 (CentOS)
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Client-Date: Fri, 15 Jul 2016 02:11:26 GMT
Client-Peer: 192.168.32.83:80
Client-Response-Num: 1
Set-Cookie: zbx_sessionid=e9365512c43faa607f76c9306f979cfc; expires=Mon, 15-Aug-2016 02:04:10 GMT
Set-Cookie: zbx_sessionid=99142dca7311038e6d335d32060b5ef3; expires=Mon, 15-Aug-2016 02:04:10 GMT
X-Powered-By: PHP/5.3.3

e9365512c43faa607f76c9306f979cfc
99142dca7311038e6d335d32060b5ef3
[root@dr-mysql01 ~]# cat lwp_cookies.txt
#LWP-Cookies-1.0
Set-Cookie3: zbx_sessionid=99142dca7311038e6d335d32060b5ef3; path="/zabbix"; domain=192.168.32.83; expires="2016-08-15 02:04:10Z"; version=0

原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6199592.html