| 1 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | Rex::Commands::Run - Execute a remote command | ||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | With this module you can run a command. | ||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | my $output = run 'ls -l'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | sudo 'id'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | =head1 CONFIGURATION AND ENVIRONMENT | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | Please note that Rex may set the C<PATH> environment variable when executing commands on the user's behalf to a different value compared to executing the same commands manually. The following are available to control the related behavior: | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | =item L<path|Rex::Commands#path> command | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | =item L<set_path|Rex::Config#set_path> configuration option | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | =item L<no_path_cleanup|Rex#no_path_cleanup> feature flag | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | =item L<source_profile|Rex#source_profile> feature flag | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | =item L<source_global_profile|Rex#source_global_profile> feature flag | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | =head1 EXPORTED FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | package Rex::Commands::Run; | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 86 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 86 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 86 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | #require Exporter; | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 86 | require Rex::Exporter; | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | 86 | ▶ | use Net::OpenSSH::ShellQuoter; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 86 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 86 | ▶ | use Rex; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 86 | ▶ | use Rex::Logger; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 86 | ▶ | use Rex::Helper::SSH2; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 86 | ▶ | use Rex::Helper::Run; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 86 | ▶ | use Rex::Helper::SSH2::Expect; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 86 | ▶ | use Rex::Config; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 86 | ▶ | use Rex::Interface::Exec; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 86 | ▶ | use Rex::Interface::Fs; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 86 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 86 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=9.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 86 | ▶ | if ( $^O !~ m/^MSWin/ ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | eval "use Expect"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # this fails sometimes on windows... | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | eval { Rex::Logger::debug("Running under windows, Expect not supported."); }; | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 86 | ▶ | use vars qw(@EXPORT); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 86 | ▶ | use base qw(Rex::Exporter); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 86 | @EXPORT = qw(run can_run sudo); | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | =head2 run($command [, $callback], %options) | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | This function will execute the given C<$command> and returns the output. In | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | scalar context it returns the raw output as is, and in list context it | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | returns the list of output lines. The exit value of the command is stored | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | in the C<$?> variable. | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | run 'uptime'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | my $output = run 'uptime'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | my @output_lines = run 'uptime'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | Please note when the C<L<tty|Rex#tty>> feature flag is enabled the | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | combined output containing both C<STDOUT> and C<STDERR> is returned | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | via C<STDOUT>. When using the C<L<no_tty|Rex#no_tty>> feature flag, or | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | the C<L<1.0|Rex#1.0>> feature bundle (or newer), then C<run()> returns | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | only the C<STDOUT> output of the command. | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | To access separate C<STDOUT> and C<STDERR> output, use a callback | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | subroutine, for example: | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | run 'uptime', sub { | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | my ( $stdout, $stderr ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | my $server = Rex::get_current_connection()->{server}; | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | say "[$server] $stdout\n"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | It also takes further options in a form of a hash. Supported options are: | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | =item cwd => $path | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | Sets the working directory of the executed command to C<$path>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | =item only_if => $condition_command | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | Executes the command only if C<$condition_command> returns success. | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | =item unless => $condition_command | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | Executes the command if C<$condition_command> returns failure. | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | =item only_notified => TRUE | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | Queues the command to be executed later upon notification. | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | =item env => { var1 => $value1, ..., varN => $valueN } | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | Sets environment variables for the given command. | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | =item timeout => value | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | Sets the timeout for the command to be run. | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | =item auto_die => TRUE | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | Die if the command returns with an exit code indicating failure. It can be set globally via the L<exec_autodie|Rex#exec_autodie> feature flag. | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | =item command => $command_to_run | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | If present, Rex will execute C<$command_to_run>, and treat the first argument as an identifier for the given C<run()> block (e.g. to be triggered with notify). | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | =item creates => $file_to_create | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | Tries to create C<$file_to_create> upon execution, and skips execution if the file already exists. | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | =item continuous_read => $callback | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | Calls C<$callback> subroutine reference for each line of the command's output, passing the line as an argument. | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | =item end_if_matched => qr{$pattern} | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | End execution early as soon as C<$pattern> is detected in the command's output. | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | Examples: | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||
| 154 | If you only want to run a command if another command succeeds or fails, use the C<only_if> or C<unless> options. | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | run 'some-command', | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | only_if => 'pgrep httpd'; # only run if httpd is running | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | run 'some-other-command', | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | unless => 'pgrep httpd'; # only run if httpd is _not_ running | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | If you want to set custom environment variables you can do it like this: | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | run 'my_command', | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | env => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | env_var_1 => 'the value for 1', | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | env_var_2 => 'the value for 2', | ||||||||||||||||||||||||||||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | If you want to end the command upon receiving a certain output: | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | run 'my_command', | ||||||||||||||||||||||||||||||||||||||||||||||
| 173 | end_if_matched => qr{$pattern}; | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | =head2 run($command, $arguments, %options) | ||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | This form will execute C<$command> with the given C<$arguments> pass as an array reference. | ||||||||||||||||||||||||||||||||||||||||||||||
| 178 | All arguments will be quoted by Rex with C<Net::OpenSSH::ShellQuoter-E<gt>quoter()> according to the managed host's shell. | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | run 'ls', [ '-l', '-t', '-r', '-a' ]; | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | run 'ls', [ '/tmp', '-l' ], auto_die => TRUE; | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | =head2 run($command_description, command => $command, %options) | ||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | If you only want to run a command in certain cases, you can queue the command | ||||||||||||||||||||||||||||||||||||||||||||||
| 186 | and notify it to trigger its execution. | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | run 'extract-something', | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | command => 'tar -C /foo -xzf /tmp/foo.tgz', | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | only_notified => TRUE; | ||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||
| 192 | # some code ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||
| 194 | notify 'run', 'extract-something'; # now the command gets executed | ||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||
| 196 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 86 | our $LAST_OUTPUT; # this variable stores the last output of a run. | |||||||||||||||||||||||||||||||||||||||||||||
| 199 | # so that it is possible to get for example the output of an apt-get update | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | # that is called through >> install "foo" << | ||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 13 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=42 SCAR=51.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 13 | my $cmd = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 13 | ▶ | if ( ref $cmd eq "ARRAY" ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | for my $_cmd ( @{$cmd} ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | &run( $_cmd, @_ ); | |||||||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 13 | my ( $code, $option ); | |||||||||||||||||||||||||||||||||||||||||||||
| 213 | 13 | ▶ | if ( ref $_[0] eq "CODE" ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | $code = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 13 | my ($args); | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | 13 | ▶ | if ( ref $_[0] eq "ARRAY" ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 2 | $args = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 13 | ▶ | if ( scalar @_ > 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 2 | $option = {@_}; | |||||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | $option->{auto_die} = Rex::Config->get_exec_autodie() | ||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 13 | ▶ | if !exists $option->{auto_die}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 13 | my $res_cmd = $cmd; | |||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 13 | ▶ | if ( exists $option->{only_notified} && $option->{only_notified} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $option->{"only_notified"} and $option->{"only_notified"}
MC/DC0%0/2
exists $option->{"only_notified"} and $option->{"only_notified"}
exists $option->{"only_notified"}
$option->{"only_notified"}
Truth table33%1/3
A and B
A= exists $option->{"only_notified"}B= $option->{"only_notified"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | Rex::Logger::debug( | |||||||||||||||||||||||||||||||||||||||||||||
| 233 | "This command runs only if notified. Passing by. ($cmd, $option->{command})" | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | my $notify = Rex::get_current_connection()->{notify}; | |||||||||||||||||||||||||||||||||||||||||||||
| 236 | $notify->add( | ||||||||||||||||||||||||||||||||||||||||||||||
| 237 | type => "run", | ||||||||||||||||||||||||||||||||||||||||||||||
| 238 | name => $cmd, | ||||||||||||||||||||||||||||||||||||||||||||||
| 239 | options => $option, | ||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | cb => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | my ($option) = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | Rex::Logger::debug( | |||||||||||||||||||||||||||||||||||||||||||||
| 243 | "Running notified command: $cmd ($option->{command})"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | run( $option->{command} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 13 | ▶ | if ( exists $option->{command} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | $cmd = $option->{command}; | |||||||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||
| 255 | Rex::get_current_connection()->{reporter} | ||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 13 | ->report_resource_start( type => "run", name => $res_cmd ); | |||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 13 | my $changed = 1; # default for run() is 1 | |||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 13 | ▶ | if ( exists $option->{creates} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | my $fs = Rex::Interface::Fs->create(); | |||||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | if ( $fs->is_file( $option->{creates} ) ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | Rex::Logger::debug( | |||||||||||||||||||||||||||||||||||||||||||||
| 264 | "File $option->{creates} already exists. Not executing $cmd."); | ||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | $changed = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 13 | ▶ | if ( exists $option->{only_if} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | run( $option->{only_if}, auto_die => 0 ); | |||||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | Rex::Logger::debug( | |||||||||||||||||||||||||||||||||||||||||||||
| 273 | "Don't executing $cmd because $option->{only_if} return $?."); | ||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | $changed = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | $? = 0; # reset $? | |||||||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 13 | ▶ | if ( exists $option->{unless} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | run( $option->{unless}, auto_die => 0 ); | |||||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | if ( $? == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | Rex::Logger::debug( | |||||||||||||||||||||||||||||||||||||||||||||
| 283 | "Don't executing $cmd because $option->{unless} return $?."); | ||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | $changed = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 13 | my $out_ret; | |||||||||||||||||||||||||||||||||||||||||||||
| 289 | 13 | my ( $out, $err ); | |||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 13 | ▶ | if ($changed) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 13 | my $path; | |||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 13 | ▶ | if ( !Rex::Config->get_no_path_cleanup() ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 13 | $path = join( ":", Rex::Config->get_path() ); | |||||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 13 | my $exec = Rex::Interface::Exec->create; | |||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 13 | ▶ | if ( $args && ref($args) eq "ARRAY" ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args and ref $args eq "ARRAY"
MC/DC50%1/2
$args and ref $args eq "ARRAY"
$args
ref $args eq "ARRAY"
Truth table66%2/3
A and B
A= $argsB= ref $args eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 2 | ▶ | my $shell = | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
Rex::is_local() and $Rex::Commands::Run::OSNAME eq "MSWin32"
MC/DC0%0/2
Rex::is_local() and $Rex::Commands::Run::OSNAME eq "MSWin32"
Rex::is_local()
$Rex::Commands::Run::OSNAME eq "MSWin32"
Truth table33%1/3
A and B
A= Rex::is_local()B= $Rex::Commands::Run::OSNAME eq "MSWin32"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 302 | Rex::is_local() && $OSNAME eq 'MSWin32' ? 'MSWin' : $exec->shell->name; | ||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 2 | my $quoter = Net::OpenSSH::ShellQuoter->quoter($shell); | |||||||||||||||||||||||||||||||||||||||||||||
| 305 | 2 | $cmd = "$cmd " . join( " ", map { $quoter->quote($_) } @{$args} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 13 | ▶ | if ( exists $option->{timeout} && $option->{timeout} > 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $option->{"timeout"} and $option->{"timeout"} > 0
MC/DC0%0/2
exists $option->{"timeout"} and $option->{"timeout"} > 0
exists $option->{"timeout"}
$option->{"timeout"} > 0
Truth table33%1/3
A and B
A= exists $option->{"timeout"}B= $option->{"timeout"} > 0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | local $SIG{ALRM} = sub { die("timeout"); }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | alarm $option->{timeout}; | |||||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | ( $out, $err ) = $exec->exec( $cmd, $path, $option ); | |||||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | alarm 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 314 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | ▶ | if ( $@ =~ m/^timeout at/ ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | Rex::Logger::info( "Timeout executing $cmd.", "error" ); | |||||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | $? = 300; | |||||||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 321 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 13 | ( $out, $err ) = $exec->exec( $cmd, $path, $option ); | |||||||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 13 | ▶ | chomp $out if $out; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 13 | ▶ | chomp $err if $err; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 13 | $LAST_OUTPUT = [ $out, $err ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 13 | ▶ | if ( !defined $out ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | $out = ""; | |||||||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 13 | ▶ | if ( !defined $err ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 12 | $err = ""; | |||||||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 13 | ▶ | if ( $? == 127 ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 1 | ▶ | Rex::Logger::info( "$cmd: Command not found.", "error" ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 340 | if ( Rex::Config->get_verbose_run ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 342 | ▶ | elsif ( $? != 0 && $? != 300 ) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$? != 0 and $? != 300
MC/DC50%1/2
$? != 0 and $? != 300
$? != 0
$? != 300
Truth table66%2/3
A and B
A= $? != 0B= $? != 300
| |||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 7 | ▶ | Rex::Logger::info( "Error executing $cmd: Return code: $?", "warn" ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 344 | if ( Rex::Config->get_verbose_run ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 346 | elsif ( $? == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 5 | ▶ | Rex::Logger::info("Successfully executed $cmd.") | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 348 | if ( Rex::Config->get_verbose_run ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 13 | ▶ | if ($code) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | $out_ret = &$code( $out, $err ); | |||||||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||
| 355 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 13 | $out_ret = $out; | |||||||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||
| 359 | Rex::get_current_connection()->{reporter}->report( | ||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 13 | changed => 1, | |||||||||||||||||||||||||||||||||||||||||||||
| 361 | message => "Command ($cmd) executed. Return code: $?" | ||||||||||||||||||||||||||||||||||||||||||||||
| 362 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 364 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | Rex::get_current_connection()->{reporter}->report( changed => 0, ); | |||||||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||
| 368 | Rex::get_current_connection()->{reporter} | ||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 13 | ->report_resource_end( type => "run", name => $res_cmd ); | |||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 13 | ▶ | if ( exists $option->{auto_die} && $option->{auto_die} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $option->{"auto_die"} and $option->{"auto_die"}
MC/DC50%1/2
exists $option->{"auto_die"} and $option->{"auto_die"}
exists $option->{"auto_die"}
$option->{"auto_die"}
Truth table66%2/3
A and B
A= exists $option->{"auto_die"}B= $option->{"auto_die"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 3 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 2 | die("Error executing: $cmd.\nSTDOUT:\n$out\nSTDERR:\n$err"); | |||||||||||||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 11 | ▶ | if ( wantarray && defined $out_ret ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
wantarray and defined $out_ret
MC/DC0%0/2
wantarray and defined $out_ret
wantarray
defined $out_ret
Truth table33%1/3
A and B
A= wantarrayB= defined $out_ret
| |||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | return split( /\r?\n/, $out_ret ); | |||||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 11 | return $out_ret; | |||||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||
| 384 | =head2 can_run($command) | ||||||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||||||
| 386 | This function checks if a command is available in the path. It accepts a list of commands, and returns the full path to the first command found. | ||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||
| 388 | task 'uptime', sub { | ||||||||||||||||||||||||||||||||||||||||||||||
| 389 | if ( my $cmd = can_run( 'uptime', 'downtime' ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 390 | say run $cmd; | ||||||||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 392 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||
| 394 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 437 | ▶ | sub can_run { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
can_run: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 437 | my @commands = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 398 | 437 | my $exec = Rex::Interface::Exec->create; | |||||||||||||||||||||||||||||||||||||||||||||
| 399 | 437 | $exec->can_run( [@commands] ); # use a new anon ref, so that we don't have drawbacks if some lower layers will manipulate things. | |||||||||||||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||||
| 402 | =head2 sudo | ||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||
| 404 | Run a single command, a code block, or all commands with C<sudo>. You need perl to be available on the remote systems to use C<sudo>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||
| 406 | Depending on your remote sudo configuration, you may need to define a sudo password with I<sudo_password> first: | ||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||
| 408 | sudo_password 'my_sudo_password'; # hardcoding | ||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||
| 410 | Or alternatively, since Rexfile is plain perl, you can read the password from terminal at the start: | ||||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||
| 412 | use Term::ReadKey; | ||||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||||
| 414 | print 'I need sudo password: '; | ||||||||||||||||||||||||||||||||||||||||||||||
| 415 | ReadMode('noecho'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 416 | sudo_password ReadLine(0); | ||||||||||||||||||||||||||||||||||||||||||||||
| 417 | ReadMode('restore'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||
| 419 | Similarly, it is also possible to read it from a secret file, database, etc. | ||||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||
| 421 | You can turn sudo on globally with: | ||||||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||||||
| 423 | sudo TRUE; # run _everything_ with sudo | ||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||
| 425 | To run only a specific command with sudo, use : | ||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||
| 427 | say sudo 'id'; # passing a remote command directly | ||||||||||||||||||||||||||||||||||||||||||||||
| 428 | say sudo { command => 'id' }; # passing anonymous hashref | ||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||
| 430 | say sudo { command => 'id', user => 'different' }; # run a single command with sudo as different user | ||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||
| 432 | To run multiple commands with C<sudo>, either use an anonymous code reference directly: | ||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||
| 434 | sudo sub { | ||||||||||||||||||||||||||||||||||||||||||||||
| 435 | service 'nginx' => 'restart'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 436 | say run 'id'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 437 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||||
| 439 | or pass it via C<command> (optionally along a different user): | ||||||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||||||
| 441 | sudo { | ||||||||||||||||||||||||||||||||||||||||||||||
| 442 | command => sub { | ||||||||||||||||||||||||||||||||||||||||||||||
| 443 | say run 'id'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 444 | say run 'pwd', cwd => '/home/different'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 445 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 446 | user => 'different', | ||||||||||||||||||||||||||||||||||||||||||||||
| 447 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||
| 449 | B<Note> that some users receive the error C<sudo: sorry, you must have a tty | ||||||||||||||||||||||||||||||||||||||||||||||
| 450 | to run sudo>. In this case you have to disable C<requiretty> for this user. | ||||||||||||||||||||||||||||||||||||||||||||||
| 451 | You can do this in your sudoers file with the following code: | ||||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||
| 453 | Defaults:$username !requiretty | ||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||
| 455 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||||||
| 457 | 0 | ▶ | sub sudo { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sudo: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 0 | my ($cmd) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 0 | my $options; | |||||||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | ▶ | if ( ref $cmd eq "HASH" ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 0 | $options = $cmd; | |||||||||||||||||||||||||||||||||||||||||||||
| 463 | 0 | $cmd = $options->{command}; | |||||||||||||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | ▶ | if ( $cmd eq "on" || $cmd eq "-on" || $cmd eq "1" ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/6
$cmd eq "on" or $cmd eq "-on" or $cmd eq 1
$cmd eq "on" or $cmd eq "-on"
MC/DC0%0/3
$cmd eq "on" or $cmd eq "-on" or $cmd eq 1
$cmd eq "on"
$cmd eq "-on"
$cmd eq 1
Truth table0%0/4
A or B or C
A= $cmd eq "on"B= $cmd eq "-on"C= $cmd eq 1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 0 | Rex::Logger::debug("Turning sudo globally on"); | |||||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | Rex::global_sudo(1); | |||||||||||||||||||||||||||||||||||||||||||||
| 469 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 471 | elsif ( $cmd eq "0" ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | Rex::Logger::debug("Turning sudo globally off"); | |||||||||||||||||||||||||||||||||||||||||||||
| 473 | 0 | Rex::global_sudo(0); | |||||||||||||||||||||||||||||||||||||||||||||
| 474 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | Rex::get_current_connection_object()->push_use_sudo(1); | |||||||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | Rex::get_current_connection_object()->push_sudo_options( %{$options} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 0 | my $ret; | |||||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||||||
| 482 | # if sudo is used with a code block | ||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | ▶ | if ( ref($cmd) eq "CODE" ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 0 | $ret = &$cmd(); | |||||||||||||||||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 486 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 0 | $ret = i_run( $cmd, fail_ok => 1 ); | |||||||||||||||||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 0 | Rex::get_current_connection_object()->pop_use_sudo(); | |||||||||||||||||||||||||||||||||||||||||||||
| 491 | 0 | Rex::get_current_connection_object()->pop_sudo_options(); | |||||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 0 | return $ret; | |||||||||||||||||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||||||
| 496 | 86 | 1; | |||||||||||||||||||||||||||||||||||||||||||||