1

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.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Zagorax
  • 11,440
  • 8
  • 44
  • 56
  • 1
    Are you saying there's no STDOUT or STDERR output from the command? One thing you can try is [Data::Dumper](http://perldoc.perl.org/Data/Dumper.html) on the `$run` hash to see if there's anything in it at all. I would also do a dump of `$su` to see if there's anything in there. – David W. Jun 07 '12 at 22:49
  • Thank you for this suggestion. By now, I gave up and I'm using system call to use sudo, I needed it as soon as possible. I'll check better later. – Zagorax Jun 08 '12 at 17:56

1 Answers1

1

There is a typo in your code.

pogram => '/usr/bin/rm',

should be:

program => '/usr/bin/rm',

David W's suggestions for debugging are absolutely spot-on...

Soz
  • 957
  • 1
  • 5
  • 9