line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# (c) Jan Gehring |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Rex::Interface::Exec::Base; |
6
|
|
|
|
|
|
|
|
7
|
58
|
|
|
58
|
|
884
|
use v5.12.5; |
|
58
|
|
|
|
|
232
|
|
8
|
58
|
|
|
58
|
|
361
|
use warnings; |
|
58
|
|
|
|
|
170
|
|
|
58
|
|
|
|
|
1516
|
|
9
|
58
|
|
|
58
|
|
385
|
use Carp; |
|
58
|
|
|
|
|
156
|
|
|
58
|
|
|
|
|
4257
|
|
10
|
58
|
|
|
58
|
|
472
|
use Rex::Helper::Run; |
|
58
|
|
|
|
|
189
|
|
|
58
|
|
|
|
|
7486
|
|
11
|
58
|
|
|
58
|
|
610
|
use Rex::Commands::Fs; |
|
58
|
|
|
|
|
1035
|
|
|
58
|
|
|
|
|
759
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '1.14.3'; # VERSION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
0
|
|
|
0
|
0
|
0
|
my $that = shift; |
17
|
0
|
|
0
|
|
|
0
|
my $proto = ref($that) || $that; |
18
|
0
|
|
|
|
|
0
|
my $self = {@_}; |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
0
|
bless( $self, $proto ); |
21
|
|
|
|
|
|
|
|
22
|
0
|
|
|
|
|
0
|
return $self; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
0
|
|
|
0
|
0
|
0
|
sub exec { die("Must be implemented by Interface Class"); } |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub _continuous_read { |
28
|
5995
|
|
|
5995
|
|
11300
|
my ( $self, $line, $option ) = @_; |
29
|
5995
|
|
50
|
|
|
21423
|
my $cb = $option->{continuous_read} || undef; |
30
|
|
|
|
|
|
|
|
31
|
5995
|
50
|
33
|
|
|
14301
|
if ( defined($cb) && ref($cb) eq 'CODE' ) { |
32
|
0
|
|
|
|
|
0
|
&$cb($line); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _end_if_matched { |
37
|
5995
|
|
|
5995
|
|
10712
|
my ( $self, $line, $option ) = @_; |
38
|
5995
|
|
50
|
|
|
18163
|
my $regex = $option->{end_if_matched} || undef; |
39
|
|
|
|
|
|
|
|
40
|
5995
|
0
|
33
|
|
|
12726
|
if ( defined($regex) && ref($regex) eq 'Regexp' && $line =~ m/$regex/ ) { |
|
|
|
33
|
|
|
|
|
41
|
0
|
|
|
|
|
0
|
return 1; |
42
|
|
|
|
|
|
|
} |
43
|
5995
|
|
|
|
|
154478
|
return; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub execute_line_based_operation { |
47
|
5995
|
|
|
5995
|
0
|
17457
|
my ( $self, $line, $option ) = @_; |
48
|
|
|
|
|
|
|
|
49
|
5995
|
|
|
|
|
15993
|
$self->_continuous_read( $line, $option ); |
50
|
5995
|
|
|
|
|
11912
|
return $self->_end_if_matched( $line, $option ); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub can_run { |
54
|
555
|
|
|
555
|
0
|
1840
|
my ( $self, $commands_to_check, $check_with_command ) = @_; |
55
|
|
|
|
|
|
|
|
56
|
555
|
|
50
|
|
|
2531
|
$check_with_command ||= "which"; |
57
|
|
|
|
|
|
|
|
58
|
555
|
|
|
|
|
1976
|
my $exec = Rex::Interface::Exec->create; |
59
|
555
|
|
|
|
|
2993
|
my $cache = Rex::get_cache(); |
60
|
|
|
|
|
|
|
|
61
|
555
|
|
|
|
|
1112
|
for my $command ( @{$commands_to_check} ) { |
|
555
|
|
|
|
|
2694
|
|
62
|
|
|
|
|
|
|
|
63
|
556
|
|
|
|
|
5122
|
my $cache_key_name = $cache->gen_key_name("can_run.cmd/$command"); |
64
|
556
|
50
|
|
|
|
3274
|
if ( $cache->valid($cache_key_name) ) { |
65
|
0
|
|
|
|
|
0
|
return $cache->get($cache_key_name); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
556
|
|
|
|
|
5062
|
my $output = Rex::Helper::Run::i_run "$check_with_command $command", |
69
|
|
|
|
|
|
|
fail_ok => 1; |
70
|
|
|
|
|
|
|
|
71
|
556
|
100
|
|
|
|
7248
|
next if ( $? != 0 ); |
72
|
|
|
|
|
|
|
|
73
|
212
|
50
|
|
|
|
3976
|
next if ( !is_file($output) ); |
74
|
|
|
|
|
|
|
|
75
|
212
|
|
|
|
|
3229
|
$cache->set( $cache_key_name, $output ); |
76
|
|
|
|
|
|
|
|
77
|
212
|
|
|
|
|
6346
|
return $output; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
343
|
|
|
|
|
13797
|
return undef; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub direct_exec { |
84
|
0
|
|
|
0
|
0
|
|
my ( $self, $exec, $option ) = @_; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
Rex::Commands::profiler()->start("direct_exec: $exec"); |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
my $class_name = ref $self; |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
Rex::Logger::debug("$class_name/executing: $exec"); |
91
|
0
|
|
|
|
|
|
my ( $out, $err ) = $self->_exec( $exec, $option ); |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
Rex::Commands::profiler()->end("direct_exec: $exec"); |
94
|
|
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
|
Rex::Logger::debug($out) if ($out); |
96
|
0
|
0
|
|
|
|
|
if ($err) { |
97
|
0
|
|
|
|
|
|
Rex::Logger::debug("========= ERR ============"); |
98
|
0
|
|
|
|
|
|
Rex::Logger::debug($err); |
99
|
0
|
|
|
|
|
|
Rex::Logger::debug("========= ERR ============"); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
0
|
0
|
|
|
|
|
if (wantarray) { return ( $out, $err ); } |
|
0
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
return $out; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub _exec { |
108
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
109
|
0
|
|
|
|
|
|
my $class_name = ref $self; |
110
|
0
|
|
|
|
|
|
die("_exec method must be overwritten by class ($class_name)."); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub shell { |
114
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
Rex::Logger::debug("Detecting shell..."); |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
my $cache = Rex::get_cache(); |
119
|
0
|
0
|
|
|
|
|
if ( $cache->valid("shell") ) { |
120
|
0
|
|
|
|
|
|
Rex::Logger::debug( "Found shell in cache: " . $cache->get("shell") ); |
121
|
0
|
|
|
|
|
|
return Rex::Interface::Shell->create( $cache->get("shell") ); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
my %shells = Rex::Interface::Shell->get_shell_provider; |
125
|
0
|
|
|
|
|
|
for my $shell ( keys %shells ) { |
126
|
0
|
|
|
|
|
|
Rex::Logger::debug( "Searching for shell: " . $shell ); |
127
|
0
|
|
|
|
|
|
$shells{$shell}->require; |
128
|
0
|
0
|
|
|
|
|
if ( $shells{$shell}->detect($self) ) { |
129
|
0
|
|
|
|
|
|
Rex::Logger::debug( "Found shell and using: " . $shell ); |
130
|
0
|
|
|
|
|
|
$cache->set( "shell", $shell ); |
131
|
0
|
|
|
|
|
|
return Rex::Interface::Shell->create($shell); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
|
return Rex::Interface::Shell->create("sh"); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
1; |