1

我正在使用来自 CPAN的 Perl 模块Sudo.pm。

这是我正在使用的代码:

# 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);

有没有人可以向我解释为什么只有其中一些实例有效?准确的说,第一个、第二个和第五个实例不起作用,而第三个和第四个实例起作用。

我无法在 STDOUT 或 STDERR 上获得任何输出,并且 check_status() 函数对于所有这些都始终回答“完成”。但这只是因为当命令不起作用时,根本没有设置对象。

在我看来,我对所有这些都使用相同的语法。当然,我将它与能够在没有密码的情况下运行 sudo 的用户一起使用,这就是我没有添加该参数的原因。

非常感谢。

4

1 回答 1

1

您的代码中有错字。

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

应该:

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

David W 的调试建议绝对是正确的……

于 2012-06-07T23:19:31.620 回答