Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use 5.16.1;
- use Net::Curl::Easy qw(:constants);
- use Data::Dumper;
- my $ua=Net::Curl::Easy->new;
- my $curlOpts={
- CURLOPT_COOKIEFILE() => '',
- CURLOPT_SSLCERT() => '/opt/zabbix/aux/checks/x509/support_company_ru_new_02062017.cer',
- CURLOPT_SSLKEY() => '/opt/zabbix/aux/checks/x509/support_company_ru_new_02062017.key',
- CURLOPT_CAPATH() => '/opt/zabbix/aux/checks/x509',
- CURLOPT_SSL_VERIFYPEER() => 1,
- CURLOPT_SSL_VERIFYHOST() => 2,
- CURLOPT_URL() => 'https://support.company.ru',
- CURLOPT_FOLLOWLOCATION() => 1,
- CURLOPT_HTTPHEADER() => {'Accept-Language'=>['ru-RU,ru', {'q'=>'0.8,en-US'}, {'q'=>'0.6,en'}, {'q'=>0.4}]},
- CURLOPT_WRITEDATA() => \my $body,
- CURLOPT_HEADERDATA() => \my $headers,
- CURLOPT_VERBOSE() => 1,
- };
- sub rcrs_struct_2str {
- my ($el, $lvl)=@_;
- return $el unless ref($el) and ref($el) =~ /^ARRAY|HASH$/;
- ($lvl++)
- ?
- join(';' =>
- ref($el) eq 'ARRAY'
- ? ( map { ref($_) ? rcrs_struct_2str($_,$lvl) : $_ } @{$el} )
- : ( map { $_->[0] . '=' . (ref($_->[1]) ? '('.rcrs_struct_2str($_->[1], $lvl).')' : $_->[1]) } map [each $el], 1..keys $el )
- )
- :
- [
- ref($el) eq 'ARRAY'
- ? ( map { ref($_) ? rcrs_struct_2str($_,$lvl) : $_ } @{$el} )
- : ( map { $_->[0] . ': ' . (ref($_->[1]) ? rcrs_struct_2str($_->[1], $lvl) : $_->[1])} map [each $el], 1..keys $el )
- ]
- }
- 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;
- $ua->perform();
- say ">> ".$ua->getinfo( CURLINFO_RESPONSE_CODE )." <<";
- say "( ".($ua->getinfo( CURLINFO_NAMELOOKUP_TIME )*1000)." ms. )";
- say Dumper [$ua->getinfo(CURLINFO_COOKIELIST())];
- my ($authToken) = $body=~m%input\s+[^>]+name="authenticity_token"[^>]+value="([^"]+)"%;
- my $post={
- 'username'=>'UserTest',
- 'password'=>'12345678',
- 'login'=>'Login+»',
- 'utf8'=>'✓',
- 'authenticity_token'=>$authToken,
- };
- say "AUTH=$authToken";
- $ua->setopt( CURLOPT_POST, 1 );
- # NOT SO: $ua->setopt( CURLOPT_POSTFIELDS, my $postContent=join('&' => map $_->[0].'='.$_->[1], map [each $post], 1..keys $post) );
- # OR SO: $ua->setopt( CURLOPT_POSTFIELDS, my $postContent=$ua->escape(join('&' => map $_->[0].'='.$_->[1], map [each $post], 1..keys $post) ) );
- # ONLY SO:
- $ua->setopt( CURLOPT_POSTFIELDS =>
- my $postContent=join('&' => map join('=', map $ua->escape($_), each $post), 1..keys $post)
- );
- say "POST=$postContent";
- $ua->setopt( CURLOPT_POSTFIELDSIZE, length $postContent );
- $ua->setopt( CURLOPT_URL, 'https://support.company.ru/login' );
- ($body,$headers)=('','');
- $ua->perform();
- {
- open my $fh, '>', '/tmp/body.txt';
- print $fh $body;
- close $fh;
- }
- say 'headers=', $headers;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement