File Coverage

lib/Rex/Interface/Shell/Bash.pm
Criterion Covered Total %
statement 17 66 25.7
branch 0 28 0.0
condition 0 18 0.0
subroutine 6 11 54.5
pod 0 5 0.0
total 23 128 17.9


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Interface::Shell::Bash;
6              
7 1     1   14 use v5.12.5;
  1         6  
8 1     1   6 use warnings;
  1         2  
  1         41  
9              
10             our $VERSION = '1.14.3'; # VERSION
11              
12 1     1   11 use Rex::Interface::Shell::Base;
  1         3  
  1         12  
13 1     1   38 use base qw(Rex::Interface::Shell::Base);
  1         2  
  1         93  
14 1     1   9 use Data::Dumper;
  1         2  
  1         53  
15 1     1   6 use Net::OpenSSH::ShellQuoter;
  1         3  
  1         14  
16              
17             sub new {
18 0     0 0   my $class = shift;
19 0   0       my $proto = ref($class) || $class;
20 0           my $self = $proto->SUPER::new(@_);
21              
22 0           bless( $self, $class );
23              
24 0           return $self;
25             }
26              
27             sub path {
28 0     0 0   my ( $self, $path ) = @_;
29 0           $self->{path} = $path;
30             }
31              
32             sub source_global_profile {
33 0     0 0   my ( $self, $parse ) = @_;
34 0           $self->{source_global_profile} = $parse;
35             }
36              
37             sub source_profile {
38 0     0 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             sub exec {
56 0     0 0   my ( $self, $cmd, $option ) = @_;
57              
58 0 0         if ( exists $option->{path} ) {
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 0         if ( exists $option->{cwd} ) {
74 0           $complete_cmd = "cd $option->{cwd} && $complete_cmd";
75             }
76              
77 0 0 0       if ( $self->{path} && !exists $self->{__env__}->{PATH} ) {
78 0           $complete_cmd = "PATH=$self->{path}; export PATH; $complete_cmd ";
79             }
80              
81 0 0 0       if ( $self->{locale} && !exists $option->{no_locales} ) {
82 0           $complete_cmd = "LC_ALL=$self->{locale} ; export LC_ALL; $complete_cmd ";
83             }
84              
85 0 0         if ( $self->{source_profile} ) {
86 0           $complete_cmd =
87             "[ -r ~/.profile ] && . ~/.profile >/dev/null 2>&1 ; $complete_cmd";
88             }
89              
90 0 0         if ( $self->{source_global_profile} ) {
91 0           $complete_cmd = ". /etc/profile >/dev/null 2>&1 ; $complete_cmd";
92             }
93              
94 0 0         if ( exists $self->{__env__} ) {
95 0 0         if ( ref $self->{__env__} eq "HASH" ) {
96 0           for my $key ( keys %{ $self->{__env__} } ) {
  0            
97 0           my $val = $self->{__env__}->{$key};
98 0           $val =~ s/"/\\"/gms;
99 0 0 0       if ( exists $self->{__sudo_env__} && $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 0         if ( Rex::Config->get_sleep_hack ) {
115 0           $complete_cmd .= " ; f=\$? ; sleep .00000001 ; exit \$f";
116             }
117              
118 0 0 0       if ( exists $option->{preprocess_command}
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 0         if ( exists $option->{format_cmd} ) {
126 0           $complete_cmd =~ s/\{\{CMD\}\}/$cmd/;
127             }
128              
129 0 0         if ( $self->{__inner_shell__} ) {
130 0           my $quoter = Net::OpenSSH::ShellQuoter->quoter("sh");
131 0           $complete_cmd = "sh -c " . $quoter->quote($complete_cmd);
132             }
133              
134 0 0 0       if ( exists $option->{prepend_command} && $option->{prepend_command} ) {
135 0           $complete_cmd = $option->{prepend_command} . " $complete_cmd";
136             }
137              
138 0           return $complete_cmd;
139             }
140              
141             1;