我正在试着弄清楚为什么通过命令行执行我的perl脚本效果很好,但是当我尝试通过php执行它时,它运行脚本,但在某些点失败。
OS: CentOS release 6.6 (Final)
PHP Version: 5.3.3
Perl Version: 5.10.1
lighttpd Version: 1.4.39
web server type: lighttpd
webpage location: /var/www/htdocs/
scripts location: /var/www/htdocs/scripts/
jquery location: /var/www/htdocs/js/
JQUERY调用php脚本:
$.ajax({
url: "scripts/check_lsps.php",
type: "POST",
data: 'id='+device_id,
dataType: 'text',
success: function(data)
{
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown)
{
alert(textStatus, errorThrown);
}
});
PHP脚本:
<?php
$device_id = $_POST['id'];
$output = shell_exec('/usr/bin/perl /var/www/htdocs/scripts/lsp_script.pl ' . $device_id);
echo ($output);
?>
Perl脚本失败区域:
sub main {
my %lsp_list = ();
#look for any unreachable hosts in local database
ping_devices();
}
sub ping_devices {
print "Checking local databse for unreachable hosts:\n";
my ($result) = database_queries('localhost','juniper_lsps','','',qq(select device_id, device_name from juniper_lsps.devices));
if (scalar(@$result) > 0) {
for my $host (@$result) {
my $p = Net::Ping->new("icmp"); **<-SCRIPT FAILS HERE**
unless ($p->ping($host->{device_name}, 1)) {
print "Deleting all data related to $host->{device_name}, device is unreachable\n";
database_queries('localhost','juniper_lsps','','',qq(delete from juniper_lsps.devices where device_name = '$host->{device_name}'));
database_queries('localhost','juniper_lsps','','',qq(delete from juniper_lsps.lsps where device_id = '$host->{device_id}'));
database_queries('localhost','juniper_lsps','','',qq(delete from juniper_lsps.lsp_paths where device_id = '$host->{device_id}'));
print "Deleting of data completed.\n";
}
print "Completed checking local database for unreachable hosts\n";
}
}
else {
print "There are currently no downed lsps recorded.\n";
print "Completed checking local database for unreachable hosts\n";
}
}
当我通过单击网页上的按钮执行脚本时,我能够返回@ $ result的结果(ping_devices函数中的perl脚本),但脚本在到达Net :: Ping时失败部分剧本和死亡。
它通过命令行工作得非常好,但是在通过php执行它时会很短暂。
也许是我在php设置中缺少的东西,但我完全没有线索。
如果需要,我可以提供更多详细信息。
编辑:对此感到抱歉。
当php脚本试图运行Net :: Ping(“icmp”)和所需的root权限时,结果是一个权限问题。
-sh-4.1$ php check_lsps.php
icmp ping requires root privilege at /var/www/htdocs/scripts/lsp_script.pl line 54.
Array
(
[0] => Checking local databse for unreachable hosts:
)
通过将php脚本中的原始exec()命令编辑为:
来解决此问题exec('sudo /usr/bin/perl /var/www/htdocs/scripts/lsp_script.pl ' . $device_id, $output);
答案 0 :(得分:0)
当php脚本试图运行Net :: Ping(“icmp”)和所需的root权限时,结果是一个权限问题。
-sh-4.1$ php check_lsps.php
icmp ping requires root privilege at /var/www/htdocs/scripts/lsp_script.pl line 54.
Array
(
[0] => Checking local databse for unreachable hosts:
)
通过将php脚本中的原始exec()命令编辑为:
来解决此问题exec('sudo /usr/bin/perl /var/www/htdocs/scripts/lsp_script.pl ' . $device_id, $output);
- jmg0880