简体   繁体   English

如何使用php执行shell脚本?

[英]How to execute shell script using php?

I try to monitor the call in Asterisk.我尝试监视 Asterisk 中的呼叫。

If I want to listen event I must start service with this command and I store bash file in there /etc/rc.d/init.d/asterisk_ipu_gui.bash如果我想监听事件,我必须用这个命令启动服务,并将 bash 文件存储在那里 /etc/rc.d/init.d/asterisk_ipu_gui.bash

service asterisk_ipu_gui start

And the I login Asterisk CLI, and check the status with these command我登录 Asterisk CLI,并使用这些命令检查状态

asterisk -vvvr
manager show connected

Ex show in picture: Ex 显示在图片中:

在此处输入图片说明

If It has IP address 127.0.0.0 ---> start service success.如果它有 IP 地址 127.0.0.0 ---> 启动服务成功。

This is manual, now I want to use PHP execute that shell script.这是手动的,现在我想用 PHP 执行那个 shell 脚本。 How can I make this?我怎样才能做到这一点?

I write my code like this, but it not affect.我这样写我的代码,但它不影响。

<?php
 $output1 = shell_exec('service asterisk_ipu_gui start');
 $output2 = shell_exec('asterisk -vvvr');
 $output3 = shell_exec('manager show connected');
echo "<pre>$output3</pre>";
?>

Instead of having php execute each shell command, try putting all commands in a shell script and then execute that script with php.与其让 php 执行每个 shell 命令,不如尝试将所有命令放在一个 shell 脚本中,然后用 php 执行该脚本。

you can have commands in asterisk.sh你可以在asterisk.sh中有命令

#!/bin/bash
service asterisk_ipu_gui start
asterisk -vvvr
manager show connected

now execute that with php:现在用php执行:

<?php 
$output = shell_exec('/home/user/scripts/asterisk.sh'); 
echo "$output"; 
?>

You should put echo after every command你应该在每个命令之后放回声

<?php
$output1 = shell_exec('service asterisk_ipu_gui start');
echo "<pre>$output1</pre>";
$output2 = shell_exec('asterisk -vvvr');
echo "<pre>$output2</pre>";
$output3 = shell_exec('manager show connected');
echo "<pre>$output3</pre>";
?>

Also look into manual , espessially into User Contributed Notes还要查看手册,尤其是用户贡献的注释

Try this.尝试这个。 It issues the command in the same command as the login as I mentioned in my comment above:正如我在上面的评论中提到的,它在与登录相同的命令中发出命令:

 $output1 = shell_exec('service asterisk_ipu_gui start');
 $output2 = shell_exec('asterisk -vvvr "manager show connected"');

$output2 should hold your expected results now. $output2现在应该保存您的预期结果。

Also, try to add your www-data user to asterisk group, this may be due to permissions and ownership issues.另外,尝试将您的 www-data 用户添加到 asterisk 组,这可能是由于权限和所有权问题。 If you can do it locally with 127.0.0.1, that means your local user is allowed to do it and it works, but when php tried via www-data, it fails.如果您可以使用 127.0.0.1 在本地执行此操作,则意味着您的本地用户可以执行此操作并且它可以工作,但是当 php 通过 www-data 尝试时,它会失败。

If your php has root permission to access asterisk then it should work如果您的 php 具有访问星号的 root 权限,那么它应该可以工作

Also instead of也代替

 $output2 = shell_exec('asterisk -vvvr');
 $output3 = shell_exec('manager show connected');

try尝试

$output2 = shell_exec('asterisk -rx "manager show connected"');

The simplest way to control Asterisk from an external shell从外部 shell 控制 Asterisk 的最简单方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM