[英]perl script does not fully run when executed via php
我想弄清楚为什么要通过命令行执行我的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);
当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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.