| 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
| |||||||||||||
| 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') || {}
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')
| |||||||||||||
| 45 | 0 | my $buffer_size = 1024; | |||||||||||
| 46 | 0 | ▶ | if ( exists $rex_int_conf->{read_buffer_size} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 54 | 0 | $in .= $buf; | |||||||||||
| 55 | 0 | $current_line .= $buf; | |||||||||||
| 56 | |||||||||||||
| 57 | 0 | ▶ | if ( $buf =~ m/\n/ms ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | 0 | @lines = split( /\n/, $current_line ); | |||||||||||
| 59 | 0 | ▶ | unshift @lines, $last_line if ($last_line); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
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'}
| |||||||||||||
| 95 | 0 | ▶ | while ( !$chan->eof ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 114 | 0 | ▶ | $in =~ s/\r//g if $in; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 115 | 0 | ▶ | $in_err =~ s/\r//g if $in_err; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | 0 | ▶ | if (wantarray) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 148 | } | ||||||||||||
| 149 | |||||||||||||
| 150 | 0 | $chan->close; | |||||||||||
| 151 | 0 | $? = $chan->exit_status; | |||||||||||
| 152 | |||||||||||||
| 153 | } | ||||||||||||
| 154 | |||||||||||||
| 155 | 86 | 1; | |||||||||||