| 1 | # | ||||||||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Rex::Interface::Shell::Bash; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Rex::Interface::Shell::Base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use base qw(Rex::Interface::Shell::Base); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Net::OpenSSH::ShellQuoter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 19 | 2 | ▶ | my $proto = ref($class) || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 20 | 2 | my $self = $proto->SUPER::new(@_); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | bless( $self, $class ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2 | return $self; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub path { | ||||||||||||||||||||||||||
|
Subroutine
path: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 0 | my ( $self, $path ) = @_; | |||||||||||||||||||||||||||
| 29 | 0 | $self->{path} = $path; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub source_global_profile { | ||||||||||||||||||||||||||
|
Subroutine
source_global_profile: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 0 | my ( $self, $parse ) = @_; | |||||||||||||||||||||||||||
| 34 | 0 | $self->{source_global_profile} = $parse; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub source_profile { | ||||||||||||||||||||||||||
|
Subroutine
source_profile: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 0 | my ( $self, $parse ) = @_; | |||||||||||||||||||||||||||
| 39 | 0 | $self->{source_profile} = $parse; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # sub set_env { | ||||||||||||||||||||||||||||
| 43 | # my ( $self, $env ) = @_; | ||||||||||||||||||||||||||||
| 44 | # my $cmd = undef; | ||||||||||||||||||||||||||||
| 45 | # | ||||||||||||||||||||||||||||
| 46 | # die("Error: env must be a hash") | ||||||||||||||||||||||||||||
| 47 | # if ( ref $env ne "HASH" ); | ||||||||||||||||||||||||||||
| 48 | # | ||||||||||||||||||||||||||||
| 49 | # while ( my ( $k, $v ) = each($env) ) { | ||||||||||||||||||||||||||||
| 50 | # $cmd .= "export $k=$v; "; | ||||||||||||||||||||||||||||
| 51 | # } | ||||||||||||||||||||||||||||
| 52 | # $self->{env} = $cmd; | ||||||||||||||||||||||||||||
| 53 | # } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub exec { | ||||||||||||||||||||||||||
|
Subroutine
exec: not called CC=21 SCAR=61.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 0 | my ( $self, $cmd, $option ) = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ( exists $option->{path} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | $self->path( $option->{path} ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | Rex::Logger::debug("Shell/Bash: Got options:"); | |||||||||||||||||||||||||||
| 63 | 0 | Rex::Logger::debug( Dumper($option) ); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | my $complete_cmd = $cmd; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # if we need to execute without an sh command, | ||||||||||||||||||||||||||||
| 68 | # use the format_cmd key | ||||||||||||||||||||||||||||
| 69 | # if ( exists $option->{no_sh} ) { | ||||||||||||||||||||||||||||
| 70 | # $complete_cmd = $option->{format_cmd}; | ||||||||||||||||||||||||||||
| 71 | # } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ( exists $option->{cwd} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | $complete_cmd = "cd $option->{cwd} && $complete_cmd"; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ( $self->{path} && !exists $self->{__env__}->{PATH} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"path"} and not exists $self->{"__env__"}{"PATH"}
MC/DC0%0/2
$self->{"path"} and not exists $self->{"__env__"}{"PATH"}
$self->{"path"}
not exists $self->{"__env__"}{"PATH"}
Truth table0%0/3
A and not(B)
A= $self->{"path"}B= not exists $self->{"__env__"}{"PATH"}
| |||||||||||||||||||||||||||||
| 78 | 0 | $complete_cmd = "PATH=$self->{path}; export PATH; $complete_cmd "; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ( $self->{locale} && !exists $option->{no_locales} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"locale"} and not exists $option->{"no_locales"}
MC/DC0%0/2
$self->{"locale"} and not exists $option->{"no_locales"}
$self->{"locale"}
not exists $option->{"no_locales"}
Truth table0%0/3
A and not(B)
A= $self->{"locale"}B= not exists $option->{"no_locales"}
| |||||||||||||||||||||||||||||
| 82 | 0 | $complete_cmd = "LC_ALL=$self->{locale} ; export LC_ALL; $complete_cmd "; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ( $self->{source_profile} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | $complete_cmd = | |||||||||||||||||||||||||||
| 87 | "[ -r ~/.profile ] && . ~/.profile >/dev/null 2>&1 ; $complete_cmd"; | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ( $self->{source_global_profile} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | $complete_cmd = ". /etc/profile >/dev/null 2>&1 ; $complete_cmd"; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ( exists $self->{__env__} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ( ref $self->{__env__} eq "HASH" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | for my $key ( keys %{ $self->{__env__} } ) { | |||||||||||||||||||||||||||
| 97 | 0 | my $val = $self->{__env__}->{$key}; | |||||||||||||||||||||||||||
| 98 | 0 | $val =~ s/"/\\"/gms; | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ( exists $self->{__sudo_env__} && $self->{__sudo_env__} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $self->{"__sudo_env__"} and $self->{"__sudo_env__"}
MC/DC0%0/2
exists $self->{"__sudo_env__"} and $self->{"__sudo_env__"}
exists $self->{"__sudo_env__"}
$self->{"__sudo_env__"}
Truth table0%0/3
A and B
A= exists $self->{"__sudo_env__"}B= $self->{"__sudo_env__"}
| |||||||||||||||||||||||||||||
| 100 | 0 | $complete_cmd = " $key=\"$val\" $complete_cmd "; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | else { | ||||||||||||||||||||||||||||
| 103 | 0 | $complete_cmd = " export $key=\"$val\" ; $complete_cmd "; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | else { | ||||||||||||||||||||||||||||
| 108 | 0 | $complete_cmd = $self->{__env__} . " $complete_cmd "; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # this is due to a strange behaviour with Net::SSH2 / libssh2 | ||||||||||||||||||||||||||||
| 113 | # it may occur when you run rex inside a kvm virtualized host connecting to another virtualized vm on the same hardware | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if ( Rex::Config->get_sleep_hack ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | $complete_cmd .= " ; f=\$? ; sleep .00000001 ; exit \$f"; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if ( exists $option->{preprocess_command} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $option->{"preprocess_command"} and ref $option->{"preprocess_command"} eq "CODE"
MC/DC0%0/2
exists $option->{"preprocess_command"} and ref $option->{"preprocess_command"} eq "CODE"
exists $option->{"preprocess_command"}
ref $option->{"preprocess_command"} eq "CODE"
Truth table0%0/3
A and B
A= exists $option->{"preprocess_command"}B= ref $option->{"preprocess_command"} eq "CODE"
| |||||||||||||||||||||||||||||
| 119 | && ref $option->{preprocess_command} eq "CODE" ) | ||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||
| 121 | 0 | $complete_cmd = $option->{preprocess_command}->($complete_cmd); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # rewrite the command again | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ( exists $option->{format_cmd} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | $complete_cmd =~ s/\{\{CMD\}\}/$cmd/; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ( $self->{__inner_shell__} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | my $quoter = Net::OpenSSH::ShellQuoter->quoter("sh"); | |||||||||||||||||||||||||||
| 131 | 0 | $complete_cmd = "sh -c " . $quoter->quote($complete_cmd); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if ( exists $option->{prepend_command} && $option->{prepend_command} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $option->{"prepend_command"} and $option->{"prepend_command"}
MC/DC0%0/2
exists $option->{"prepend_command"} and $option->{"prepend_command"}
exists $option->{"prepend_command"}
$option->{"prepend_command"}
Truth table0%0/3
A and B
A= exists $option->{"prepend_command"}B= $option->{"prepend_command"}
| |||||||||||||||||||||||||||||
| 135 | 0 | $complete_cmd = $option->{prepend_command} . " $complete_cmd"; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | return $complete_cmd; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 3 | 1; | |||||||||||||||||||||||||||