Advertisement
DRVTiny

example_net_curl_easy_post_ssl_cookies

Nov 24th, 2017
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.90 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use 5.16.1;
  3. use Net::Curl::Easy qw(:constants);
  4. use Data::Dumper;
  5.  
  6. my $ua=Net::Curl::Easy->new;
  7. my $curlOpts={
  8.   CURLOPT_COOKIEFILE()   => '',
  9.   CURLOPT_SSLCERT()     => '/opt/zabbix/aux/checks/x509/support_company_ru_new_02062017.cer',
  10.   CURLOPT_SSLKEY()      => '/opt/zabbix/aux/checks/x509/support_company_ru_new_02062017.key',
  11.   CURLOPT_CAPATH()      => '/opt/zabbix/aux/checks/x509',
  12.   CURLOPT_SSL_VERIFYPEER()     => 1,
  13.   CURLOPT_SSL_VERIFYHOST()     => 2,
  14.   CURLOPT_URL()         => 'https://support.company.ru',
  15.   CURLOPT_FOLLOWLOCATION()      => 1,
  16.   CURLOPT_HTTPHEADER()          => {'Accept-Language'=>['ru-RU,ru', {'q'=>'0.8,en-US'}, {'q'=>'0.6,en'}, {'q'=>0.4}]},
  17.   CURLOPT_WRITEDATA()           => \my $body,
  18.   CURLOPT_HEADERDATA()          => \my $headers,
  19.   CURLOPT_VERBOSE()             => 1,  
  20. };
  21.  
  22. sub rcrs_struct_2str {
  23.   my ($el, $lvl)=@_;
  24.   return $el unless ref($el) and ref($el) =~ /^ARRAY|HASH$/;
  25.   ($lvl++)
  26.   ?
  27.     join(';' =>
  28.       ref($el) eq 'ARRAY'
  29.         ? ( map { ref($_) ? rcrs_struct_2str($_,$lvl) : $_ } @{$el} )
  30.         : ( map { $_->[0] . '=' . (ref($_->[1]) ? '('.rcrs_struct_2str($_->[1], $lvl).')' : $_->[1]) } map [each $el], 1..keys $el )
  31.     )
  32.   :
  33.     [
  34.       ref($el) eq 'ARRAY'
  35.         ? ( map { ref($_) ? rcrs_struct_2str($_,$lvl) : $_ } @{$el} )
  36.         : ( map { $_->[0]  . ': ' . (ref($_->[1]) ? rcrs_struct_2str($_->[1], $lvl) : $_->[1])} map [each $el], 1..keys $el )
  37.     ]
  38. }
  39.  
  40. ref($_->[1])=~/^(ARRAY|HASH)$/ ? $ua->pushopt($_->[0] => do { my $s=rcrs_struct_2str($_->[1]); print Dumper [$s]; $s }) : $ua->setopt(@{$_}) for map [each $curlOpts], 1..keys $curlOpts;
  41.  
  42. $ua->perform();  
  43.  
  44. say ">> ".$ua->getinfo( CURLINFO_RESPONSE_CODE )." <<";
  45. say "( ".($ua->getinfo( CURLINFO_NAMELOOKUP_TIME )*1000)." ms. )";
  46. say Dumper [$ua->getinfo(CURLINFO_COOKIELIST())];
  47.  
  48. my ($authToken) = $body=~m%input\s+[^>]+name="authenticity_token"[^>]+value="([^"]+)"%;
  49. my $post={
  50.                'username'=>'UserTest',
  51.                'password'=>'12345678',
  52.                'login'=>'Login+»',
  53.                'utf8'=>'✓',
  54.                'authenticity_token'=>$authToken,
  55. };
  56. say "AUTH=$authToken";
  57. $ua->setopt( CURLOPT_POST, 1 );
  58.  
  59. # NOT SO: $ua->setopt( CURLOPT_POSTFIELDS, my $postContent=join('&' => map $_->[0].'='.$_->[1], map [each $post], 1..keys $post) );
  60. #  OR SO: $ua->setopt( CURLOPT_POSTFIELDS, my $postContent=$ua->escape(join('&' => map $_->[0].'='.$_->[1], map [each $post], 1..keys $post) ) );
  61. # ONLY SO:
  62. $ua->setopt( CURLOPT_POSTFIELDS =>
  63.  my $postContent=join('&' => map join('=', map $ua->escape($_), each $post), 1..keys $post)
  64. );
  65.  
  66. say "POST=$postContent";
  67. $ua->setopt( CURLOPT_POSTFIELDSIZE, length $postContent );
  68. $ua->setopt( CURLOPT_URL, 'https://support.company.ru/login' );
  69.  
  70. ($body,$headers)=('','');
  71. $ua->perform();
  72.  
  73. {
  74.  open my $fh, '>', '/tmp/body.txt';
  75.  print $fh $body;
  76.  close $fh;
  77. }
  78. say 'headers=', $headers;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement