lib/Rex/Helper/SSH2.pm

filter: Statementstmt 25.2% 24 / 95 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 6 / 8 Podpod 0.0% 0 / 2 totaltotal 20.6% 30 / 145 CC 22 SCAR 56.7
CC
22
Cov
18%
net_ssh2_exec
420.0
net_ssh2_exec_output
12.0
BEGIN
1.0
CRAP 291.1
« lib/Rex/Interface/Shell/Bash.pm ↑ Summary lib/Rex/Hardware/Host.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Helper::SSH2;
6  
7 86 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 86 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 86 our $VERSION = '1.16.1'; # VERSION
11  
12 86 require Exporter;
13 86 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 86 require Rex::Commands;
15 86 ▶use Time::HiRes qw(sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 86 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 86 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 86 @EXPORT = qw(net_ssh2_exec net_ssh2_exec_output net_ssh2_shell_exec);
21  
22 86 our $READ_STDERR = 1;
23 86 our $EXEC_AND_SLEEP = 0;
24  
25 0 ▶sub net_ssh2_exec {
Subroutine net_ssh2_exec: not called CC=20 SCAR=60.4
Pod undocumented
26 0 my ( $ssh, $cmd, $base, $option ) = @_;
27  
28 0 my $chan = $ssh->channel;
29  
30 # REQUIRE_TTY can be turned off by feature no_tty
31 0 ▶ if ( !Rex::Config->get_no_tty ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Rex::Config"->get_no_tty) 0 0 0
32 0 $chan->pty("xterm"); # set to xterm, due to problems with vt100.
33 # if vt100 sometimes the restart of services doesn't work and need a sleep .000001 after the command...
34 # strange bug...
35 0 $chan->pty_size( 4000, 80 );
36 }
37 0 $chan->blocking(1);
38  
39 0 $chan->exec($cmd);
40  
41 0 my $in;
42 0 my $in_err = "";
43  
44 0 ▶ my $rex_int_conf = Rex::Commands::get("rex_internals") || {};
Condition0%0/2
Rex::Commands::get('rex_internals') || {}
l!l
00
MC/DC0%0/1
Rex::Commands::get('rex_internals') || {}
Rex::Commands::get('rex_internals')
Truth table0%0/2
A
A= Rex::Commands::get('rex_internals')
Aresult
11
00
45 0 my $buffer_size = 1024;
46 0 ▶ if ( exists $rex_int_conf->{read_buffer_size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rex_int_conf->{"read_buffer_size"}) 0 0 0
47 0 $buffer_size = $rex_int_conf->{read_buffer_size};
48 }
49  
50 0 my @lines;
51 my $last_line;
52 0 my $current_line = "";
53 0 ▶ while ( my $len = $chan->read( my $buf, $buffer_size ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = $chan->read(my $buf, $buffer_size)) 0 0 0
54 0 $in .= $buf;
55 0 $current_line .= $buf;
56  
57 0 ▶ if ( $buf =~ m/\n/ms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ /\n/msu) 0 0 0
58 0 @lines = split( /\n/, $current_line );
59 0 ▶ unshift @lines, $last_line if ($last_line);
Branch: 1 branch
BranchTrueFalseTotal
if $last_line 0 0 0
60 0 $last_line = pop @lines;
61  
62 0 for my $line (@lines) {
63 0 $line =~ s/[\r\n]//gms;
64 0 $line .= "\n";
65 0 ▶ $base->execute_line_based_operation( $line, $option ) && goto END_READ;
Branch: 1 branch
BranchTrueFalseTotal
$base->execute_line_based_operation($line, $option) and goto END_READ 0 0 0
66 }
67  
68 0 $current_line = "";
69 }
70  
71 }
72  
73 0 my @lines_err;
74 0 my $last_line_err = "";
75 0 ▶ while ( my $len = $chan->read( my $buf_err, $buffer_size, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = $chan->read(my $buf_err, $buffer_size, 1)) 0 0 0
76 0 $in_err .= $buf_err;
77 0 @lines_err = split( /\n/, $buf_err );
78 0 ▶ unshift @lines_err, $last_line_err if ($last_line_err);
Branch: 1 branch
BranchTrueFalseTotal
if $last_line_err 0 0 0
79 0 $last_line_err = pop @lines_err;
80  
81 0 for my $line (@lines_err) {
82 0 $line =~ s/[\r\n]//gms;
83 0 $line .= "\n";
84 0 ▶ $base->execute_line_based_operation( $line, $option ) && goto END_READ;
Branch: 1 branch
BranchTrueFalseTotal
$base->execute_line_based_operation($line, $option) and goto END_READ 0 0 0
85 }
86 }
87  
88 #select undef, undef, undef, 0.002; # wait a little before closing the channel
89 #sleep 1;
90 END_READ:
91 0 $chan->send_eof;
92  
93 0 my $wait_c = 0;
94 0 ▶ my $wait_max = $rex_int_conf->{ssh2_channel_closewait_max} || 500;
Condition0%0/2
$rex_int_conf->{'ssh2_channel_closewait_max'} || 500
l!l
00
MC/DC0%0/1
$rex_int_conf->{'ssh2_channel_closewait_max'} || 500
$rex_int_conf->{'ssh2_channel_closewait_max'}
Truth table0%0/2
A
A= $rex_int_conf->{'ssh2_channel_closewait_max'}
Aresult
11
00
95 0 ▶ while ( !$chan->eof ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chan->eof) 0 0 0
96 0 Rex::Logger::debug("Waiting for eof on ssh channel.");
97 0 sleep 0.002; # wait a little for retry
98 0 $wait_c++;
99 0 ▶ if ( $wait_c >= $wait_max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wait_c >= $wait_max) 0 0 0
100  
101 # channel will be force closed.
102 0 Rex::Logger::debug(
103 "Rex::Helper::SSH2::net_ssh2_exec: force closing channel for command: $cmd"
104 );
105 0 last;
106 }
107 }
108  
109 0 $chan->wait_closed;
110 0 $? = $chan->exit_status;
111  
112 # if used with $chan->pty() we have to remove \r
113 0 ▶ if ( !Rex::Config->get_no_tty ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Rex::Config"->get_no_tty) 0 0 0
114 0 ▶ $in =~ s/\r//g if $in;
Branch: 1 branch
BranchTrueFalseTotal
if $in 0 0 0
115 0 ▶ $in_err =~ s/\r//g if $in_err;
Branch: 1 branch
BranchTrueFalseTotal
if $in_err 0 0 0
116 }
117  
118 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
119 0 return ( $in, $in_err );
120 }
121  
122 0 return $in;
123 }
124  
125 0 ▶sub net_ssh2_exec_output {
Subroutine net_ssh2_exec_output: not called CC=3 SCAR=24.8
Pod undocumented
126 0 my ( $ssh, $cmd, $callback ) = @_;
127  
128 0 my $chan = $ssh->channel;
129 0 $chan->blocking(1);
130  
131 0 $chan->exec($cmd);
132  
133 0 while (1) {
134 0 my $buf;
135 my $buf_err;
136 0 $chan->read( $buf, 15 );
137 0 $chan->read( $buf_err, 15 );
138  
139 0 ▶ if ($callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) { } 0 0 0
140 0 &$callback( $buf, $buf_err );
141 }
142 else {
143 0 print $buf;
144 0 print $buf_err;
145 }
146  
147 0 ▶ last unless $buf;
Branch: 1 branch
BranchTrueFalseTotal
unless $buf 0 0 0
148 }
149  
150 0 $chan->close;
151 0 $? = $chan->exit_status;
152  
153 }
154  
155 86 1;
« lib/Rex/Interface/Shell/Bash.pm ↑ Summary lib/Rex/Hardware/Host.pm »