Skip to content

Instantly share code, notes, and snippets.

@krimdomu
Created January 16, 2013 19:25
Show Gist options
  • Save krimdomu/4549997 to your computer and use it in GitHub Desktop.
Save krimdomu/4549997 to your computer and use it in GitHub Desktop.
Prompt for a password
use Term::ReadKey;
user "root";
# get the CLI parameters
my %args = Rex::Args->getopts;
# only ask for a password if the user don't want to list tasks.
if(! exists $args{T}) {
password read_password();
}
task "test", "insttest-debian664", sub {
say run "hostname";
};
sub read_password {
print "Password please: ";
ReadMode "noecho"; # don't echo anything
my $password = <STDIN>;
chomp $password;
ReadMode 0; # reset terminal so it echo again
return $password;
}
@krimdomu
Copy link
Author

for the sudo password one can do

if(! exists $args{T}) {
    sudo_password read_password();
}

@krimdomu
Copy link
Author

to change the sudo_password inside a task, you can do the following:

task "mytask", sub {
  connection->server->{auth}->{sudo_password} = read_password();
  sudo sub {
    say run "id";
  };
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment