Skip to content

Instantly share code, notes, and snippets.

@ferki
Last active February 27, 2023 22:53
Show Gist options
  • Select an option

  • Save ferki/18a869f4b3bb97eda58114ebac00a2bd to your computer and use it in GitHub Desktop.

Select an option

Save ferki/18a869f4b3bb97eda58114ebac00a2bd to your computer and use it in GitHub Desktop.
rex sudo pass
use strict;
use warnings;
use Rex -feature => [ '1.4', 'exec_autodie' ];
sudo_password get_password('path/to/sudo/password/in/pass');
user 'myuser';
task 'a' => sub {
say run 'whoami'; # should be myuser
get_sudo();
say run 'whoami'; # should be root
};
sub get_sudo {
sudo TRUE;
my $sudo_password = Rex::Config::get_sudo_password();
return if $sudo_password;
print 'I need sudo password: ';
ReadMode('noecho');
sudo_password ReadLine(0);
ReadMode('restore');
say 'Thanks!';
}
sub get_password {
my $query = shift;
my $password;
open PASS, '-|', "pass show $query";
while ( $password = <PASS> ) {
chomp $password;
last;
}
close PASS;
return $password;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment