Rex::Commands::Run - Online Linux Manual PageSection : 3
Updated : 2021-03-06
Source : perl v5.32.1
Note : User Contributed Perl Documentation

NAMERex::Commands::Run − Execute a remote command

DESCRIPTIONWith this module you can run a command.

SYNOPSIS​ my $output = run "ls −l"; ​ sudo "id";

EXPORTED FUNCTIONS

run($command [, $callback], %options)

run($command, $arguments, %options)This form will execute $command with the given $arguments. ​$arguments must be an array reference. The arguments will be quoted. ​ run "ls", ["−l", "−t", "−r", "−a"]; ​ run "ls", ["/tmp", "−l"], auto_die => TRUE;

run($command_description, command => $command, %options)This function will execute the given command and returns the output. In scalar context it returns the raw output as is, and in list context it returns the list of output lines. The exit value of the command is stored in the $? variable. ​ task "uptime", "server01", sub { ​ say run "uptime"; ​ run "uptime", sub { ​ my ($stdout, $stderr) = @_; ​ my $server = Rex::get_current_connection()>{server}; ​ say "[$server] $stdout\n"; ​ }; ​ }; Supported options are: ​ cwd => $path ​ sets the working directory of the executed command to $path ​ only_if => $condition_command ​ executes the command only if $condition_command completes successfully ​ unless => $condition_command ​ executes the command unless $condition_command completes successfully ​ only_notified => TRUE ​ queues the command, to be executed upon notification (see below) ​ env => { var1 => $value1, ..., varN => $valueN } ​ sets environment variables in the environment of the command ​ timeout => value ​ sets the timeout for the command to be run ​ auto_die => TRUE ​ die if the command returns with a non−zero exit code ​ it can be set globally via the exec_autodie feature flag ​ command => $command_to_run ​ if set, run tries to execute the specified command and the first argument ​ becomes an identifier for the run block (e.g. to be triggered with notify) ​ creates => $file_to_create ​ tries to create $file_to_create upon execution ​ skips execution if the file already exists ​ continuous_read => $callback ​ calls $callback subroutine reference for each line of the command's output, ​ passing the line as an argument Examples: If you only want to run a command in special cases, you can queue the command and notify it when you want to run it. ​ task "prepare", sub { ​ run "extract−something", ​ command => "tar −C /foo −xzf /tmp/foo.tgz", ​ only_notified => TRUE; ​ ​ # some code ... ​ ​ notify "run", "extract−something"; # now the command gets executed ​ }; If you only want to run a command if another command succeeds or fails, you can use ​only_if or unless option. ​ run "some−command", ​ only_if => "ps −ef | grep −q httpd"; # only run if httpd is running ​ ​ run "some−other−command", ​ unless => "ps −ef | grep −q httpd"; # only run if httpd is not running If you want to set custom environment variables you can do it like this: ​ run "my_command", ​ ​ env => { ​ env_var_1 => "the value for 1", ​ env_var_2 => "the value for 2", ​ }; If you want to end the command upon receiving a certain output:
 run my_command,
   end_if_matched => qr/PATTERN/;

can_run($command)This function checks if a command is in the path or is available. You can specify multiple commands, the first command found will be returned. ​ task "uptime", sub { ​ if( my $cmd = can_run("uptime", "downtime") ) { ​ say run $cmd; ​ }};

sudoRun a single command, a code block, or all commands with sudo. You need perl to be available on the remote systems to use sudo. Depending on your remote sudo configuration, you may need to define a sudo password with sudo_password first: ​ sudo_password 'my_sudo_password'; # hardcoding Or alternatively, since Rexfile is plain perl, you can read the password from terminal at the start: ​ use Term::ReadKey; ​ ​ print 'I need sudo password: '; ​ ReadMode('noecho'); ​ sudo_password ReadLine(0); ​ ReadMode('restore'); Similarly, it is also possible to read it from a secret file, database, etc. You can turn sudo on globally with: ​ sudo TRUE; # run _everything_ with sudo To run only a specific command with sudo, use : ​ say sudo 'id'; # passing a remote command directly ​ say sudo { command => 'id' }; # passing anonymous hashref ​ ​ say sudo { command => 'id', user => 'different' }; # run a single command with sudo as different user To run multiple commands with sudo, either use an anonymous code reference directly: ​ sudo sub { ​ service 'nginx' => 'restart'; ​ say run 'id'; ​ }; or pass it via command (optionally along a different user): ​ sudo { ​ command => sub { ​ say run 'id'; ​ say run 'pwd', cwd => '/home/different'; ​ }, ​ user => 'different', ​ }; Note that some users receive the error sudo: sorry, you must have a tty to run sudo. In this case you have to disable requiretty for this user. You can do this in your sudoers file with the following code: ​ Defaults:$username !requiretty
0
Johanes Gumabo
Data Size   :   19,691 byte
man-Rex::Commands::Run.3pmBuild   :   2024-12-29, 07:25   :  
Visitor Screen   :   x
Visitor Counter ( page / site )   :   3 / 257,474
Visitor ID   :     :  
Visitor IP   :   18.224.32.243   :  
Visitor Provider   :   AMAZON-02   :  
Provider Position ( lat x lon )   :   39.962500 x -83.006100   :   x
Provider Accuracy Radius ( km )   :   1000   :  
Provider City   :   Columbus   :  
Provider Province   :   Ohio ,   :   ,
Provider Country   :   United States   :  
Provider Continent   :   North America   :  
Visitor Recorder   :   Version   :  
Visitor Recorder   :   Library   :  
Online Linux Manual Page   :   Version   :   Online Linux Manual Page - Fedora.40 - march=x86-64 - mtune=generic - 24.12.29
Online Linux Manual Page   :   Library   :   lib_c - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Online Linux Manual Page   :   Library   :   lib_m - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Data Base   :   Version   :   Online Linux Manual Page Database - 24.04.13 - march=x86-64 - mtune=generic - fedora-38
Data Base   :   Library   :   lib_c - 23.02.07 - march=x86-64 - mtune=generic - fedora.36

Very long time ago, I have the best tutor, Wenzel Svojanovsky . If someone knows the email address of Wenzel Svojanovsky , please send an email to johanes_gumabo@yahoo.co.id .
If error, please print screen and send to johanes_gumabo@yahoo.co.id
Under development. Support me via PayPal.

ERROR : Need New Coding :         (parse_manual_page_|252|Rex::Commands::Run.3pm|36/37|el══─{─══.|.el══─{─══. ds -- \|\(em\| )         (htmlprn|149|Rex::Commands::Run.3pm|36/37|.el══─{─══. ds --  —  |.el══─{─══. ds -- \|\(em\| )         (parse_manual_page_|252|Rex::Commands::Run.3pm|43|br══─}─══|'br══─}─══ )         (htmlprn|149|Rex::Commands::Run.3pm|43|'br══─}─══ |'br══─}─══ )