I'm using the Perl module Sudo.pm from CPAN.
Here is the code I'm using:
# In this two variable I'll store the object that runs sudo commands and
# the exit status of the commands
my ($su, $run);
# This functions accept an object returned from sudo->run and will check
# if there were errors running the code.
sub check_status {
# Retrieving the object
my $run = shift;
if (exists($run->{error})) {
print "Failed.\n";
}
else {
print "Done.\n";
}
}
# This first call erase all RSA related files
$su = Sudo->new(
{
sudo => '/usr/bin/sudo',
username => 'root',
pogram => '/usr/bin/rm',
program_args => '-f /tmp/cvmfs_test.key /tmp/cvmfs_test.csr /tmp/cvmfs_test.crt /tmp/whitelist.test.* /tmp/cvmfs_master.key /tmp/cvmfs_master.pub'
}
);
print 'Erasing RSA keys... ';
$run = $su->sudo_run();
check_status($run);
# This instance will erase configuration files created in /etc/cvmfs/config.d
$su = Sudo->new(
{
sudo => '/usr/bin/sudo',
username => 'root',
program => '/usr/bin/rm',
program_args => '-f /etc/cvmfs/config.d/127.0.0.1.conf'
}
);
print 'Erasing configuration files in /etc/cvmfs/config.d... ';
$run = $su->sudo_run();
check_status($run);
# This instance will erase /tmp/cvmfs.faulty
$su = Sudo->new(
{
sudo => '/usr/bin/sudo',
username => 'root',
program => '/usr/bin/rm',
program_args => '-f /tmp/cvmfs.faulty'
}
);
print 'Erasing /tmp/cvmfs.faulty... ';
$run = $su->sudo_run();
print $run->{stdout};
check_status($run);
# This instance will erase all previous extracted repository
$su = Sudo->new(
{
sudo => '/usr/bin/sudo',
username => 'root',
program => '/usr/bin/rm',
program_args => '-fr /tmp/server'
}
);
print 'Erasing /tmp/server directory... ';
$run = $su->sudo_run();
print $run->{stdout};
check_status($run);
# This instance will run 'restarting_services.sh'
$su = Sudo->new(
{
sudo => '/usr/bin/sudo',
username => 'root',
program => 'sh',
program_args => "$Bin/restarting_services.sh"
}
);
print 'Restarting services... ';
$run = $su->sudo_run();
check_status($run);
Is there someone who can explain me why only some of this instance works? To be precise, the first, the second and the fifth instance doesn't work, while the third and the fourth does.
I'm not able to get any output on STDOUT or STDERR and the check_status() function always answer 'Done', for all of them. But this is only because when the command doesn't work, the object is not set at all.
It seems to me I'm using the same syntax for all of them. Of course, I'm using it with an user that is able to run sudo without password, that's why I didn't added that parameter.
Thank you very much.