| 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
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table0%0/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$option and $option->{"_force_sh"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 44 | 0 | $shell->source_global_profile(1); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ( Rex::Config->get_source_profile ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | $shell->source_profile(1); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if ( $option && exists $option->{env} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$option and exists $option->{"env"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ($err) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
ref $option eq "HASH" and defined $out
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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||