我正在使用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的用户,这就是我没有添加该参数的原因。
非常感谢。
答案 0 :(得分:1)
您的代码中存在拼写错误。
pogram => '/usr/bin/rm',
应该是:
program => '/usr/bin/rm',
David W的调试建议绝对是现场......