简体   繁体   中英

How to execute shell script using php?

I try to monitor the call in 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

service asterisk_ipu_gui start

And the I login Asterisk CLI, and check the status with these command

asterisk -vvvr
manager show connected

Ex show in picture:

在此处输入图片说明

If It has IP address 127.0.0.0 ---> start service success.

This is manual, now I want to use PHP execute that shell script. 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.

you can have commands in asterisk.sh

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

now execute that with 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.

Also, try to add your www-data user to asterisk group, this may be due to permissions and ownership issues. 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.

If your php has root permission to access asterisk then it should work

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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