The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Rex::Commands::Run - Execute a remote command

DESCRIPTION

With this module you can run a command.

SYNOPSIS

 my $output = run "ls -l";
 sudo "id";

EXPORTED FUNCTIONS

run($command [, $callback])

This function will execute the given command and returns the output.

 task "uptime", "server01", sub {
    say run "uptime";
    run "uptime", sub {
       my ($stdout, $stderr) = @_;
       my $server = Rex::get_current_connection()->{server};
       say "[$server] $stdout\n";
    };
 };
can_run($command)

This function checks if a command is in the path or is available.

 task "uptime", sub {
    if(can_run "uptime") {
       say run "uptime";
    }
 };
sudo

Run a command with sudo. Define the password for sudo with sudo_password.

You can use this function to run one command with sudo privileges or to turn on sudo globaly.

 user "unprivuser";
 sudo_password "f00b4r";
 sudo -on;   # turn sudo globaly on
     
 task prepare => sub {
    install "apache2";
    file "/etc/ntp.conf",
       source => "files/etc/ntp.conf",
       owner  => "root",
       mode   => 640;
 };

Or, if you don't turning sudo globaly on.

 task prepare => sub {
    file "/tmp/foo.txt",
       content => "this file was written without sudo privileges\n";
        
    # everything in this section will be executed with sudo privileges
    sudo sub {
       install "apache2";
       file "/tmp/foo2.txt",
          content => "this file was written with sudo privileges\n";
    };
 };

Run only one command within sudo.

 task "eth1-down", sub {
   sudo "ifconfig eth1 down";
 };