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