lib/Rex/Interface/Exec/LibSSH.pm

filter: Statementstmt 42.0% 29 / 69 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 75.0% 9 / 12 Podpod 0.0% 0 / 2 totaltotal 30.8% 38 / 123 CC 16 SCAR 39.3
CC
16
Cov
29%
exec
110.0
_exec
42.0
new
6.0
CRAP 50.9
« blib/lib/Rex/Interface/Connection/LibSSH.pm ↑ Summary lib/Rex/Interface/File/LibSSH.pm »
1 # ABSTRACT: Rex command execution via Net::LibSSH exec channels
2  
3 package Rex::Interface::Exec::LibSSH;
4 1 our $VERSION = '0.005';
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Rex;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Rex::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Rex::Commands;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Rex::Interface::Shell;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Rex::Interface::Exec::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use base qw(Rex::Interface::Exec::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
17 0 my ( $that, %args ) = @_;
18 0 ▶ my $proto = ref($that) || $that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
19 0 return bless {%args}, $proto;
20 }
21  
22 # 3-arg signature is the one Rex::Commands::Run::run actually invokes:
23 # $exec->exec($cmd, $path, $option)
24 # The 2-arg form on Base.pm leaves $path glued onto $option, which means
25 # the env hash never reaches us. Mimic Rex::Interface::Exec::SSH here so
26 # environment variables are routed through a shell wrapper.
27 0 ▶sub exec {
Subroutine exec: not called CC=10 SCAR=47.0
Pod undocumented
28 0 my ( $self, $cmd, $path, $option ) = @_;
29  
30 0 Rex::Logger::debug("LibSSH/preparing: $cmd");
31  
32 0 my $shell;
33 0 ▶ if ( $option && $option->{_force_sh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option and $option->{'_force_sh'}) { } 0 0 0
Condition0%0/3
$option and $option->{"_force_sh"}
!ll&&!rl&&r
000
MC/DC0%0/2
$option and $option->{"_force_sh"}
$option $option->{"_force_sh"}
Truth table0%0/3
A and B
A= $optionB= $option->{"_force_sh"}
ABresult
0X0
100
111
34 0 $shell = Rex::Interface::Shell->create("Sh");
35 }
36 else {
37 0 $shell = $self->shell;
38 }
39  
40 0 $shell->set_locale("C");
41 0 $shell->path($path);
42  
43 0 ▶ if ( Rex::Config->get_source_global_profile ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Rex::Config"->get_source_global_profile) 0 0 0
44 0 $shell->source_global_profile(1);
45 }
46 0 ▶ if ( Rex::Config->get_source_profile ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Rex::Config"->get_source_profile) 0 0 0
47 0 $shell->source_profile(1);
48 }
49  
50 0 ▶ if ( $option && exists $option->{env} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option and exists $option->{"env"}) 0 0 0
Condition0%0/3
$option and exists $option->{"env"}
!ll&&!rl&&r
000
MC/DC0%0/2
$option and exists $option->{"env"}
$option exists $option->{"env"}
Truth table0%0/3
A and B
A= $optionB= exists $option->{"env"}
ABresult
0X0
100
111
51 0 $shell->set_environment( $option->{env} );
52 }
53  
54 0 my $wrapped = $shell->exec( $cmd, $option );
55 0 Rex::Logger::debug("LibSSH/executing: $wrapped");
56  
57 0 my ( $out, $err ) = $self->_exec( $wrapped, $option );
58  
59 0 ▶ Rex::Logger::debug($out) if $out;
Branch: 1 branch
BranchTrueFalseTotal
if $out 0 0 0
60 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
61 0 Rex::Logger::debug("========= ERR ============");
62 0 Rex::Logger::debug($err);
63 0 Rex::Logger::debug("========= ERR ============");
64 }
65  
66 0 ▶ return wantarray ? ( $out, $err ) : $out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
67 }
68  
69 0 ▶sub _exec {
Subroutine _exec: not called CC=6 SCAR=37.4
70 0 my ( $self, $cmd, $option ) = @_;
71  
72 0 ▶ my $ssh = Rex::is_ssh()
Branch: 1 branch
BranchTrueFalseTotal
my $ssh = Rex::is_ssh() or die "LibSSH exec: no active SSH connection" 0 0 0
73 or die "LibSSH exec: no active SSH connection";
74  
75 0 Rex::Logger::debug("LibSSH exec: $cmd");
76  
77 0 ▶ my $ch = $ssh->channel
Branch: 1 branch
BranchTrueFalseTotal
my $ch = $ssh->channel or die "LibSSH exec: failed to open channel" 0 0 0
78 or die "LibSSH exec: failed to open channel";
79  
80 0 $ch->exec($cmd);
81  
82 0 my ( $out, $err ) = ( '', '' );
83  
84 # libssh processes all SSH protocol messages (including stderr window
85 # adjustments) while blocking on the stdout read, so this sequential
86 # approach is safe against deadlocks.
87 0 $out = $ch->read;
88 0 $err = $ch->read( -1, 1 );
89  
90 0 my $exit = $ch->exit_status;
91 0 $ch->close;
92  
93 0 $? = $exit << 8;
94  
95 0 ▶ if ( ref $option eq 'HASH' && defined $out ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $option eq "HASH" and defined $out) 0 0 0
Condition0%0/3
ref $option eq "HASH" and defined $out
!ll&&!rl&&r
000
MC/DC0%0/2
ref $option eq "HASH" and defined $out
ref $option eq "HASH" defined $out
Truth table0%0/3
A and B
A= ref $option eq "HASH"B= defined $out
ABresult
0X0
100
111
96 0 $self->_continuous_read( $_, $option ) for split /\n/, $out;
97 }
98  
99 0 return ( $out, $err );
100 }
101  
102 1 1;
103  
104 __END__
« blib/lib/Rex/Interface/Connection/LibSSH.pm ↑ Summary lib/Rex/Interface/File/LibSSH.pm »