i think this is more a Linux issue than an Asterisk issu, but here we go.
I have an AGI script which connects to a mysql database, gets a certain value and runs a shell command.
Ok, when I run the shell script out of asterisk, it works pretty fine. But when Asterisk calls this script, it doesn’t connect to the database. Asterisk is running as ‘root’, so I can’t see where is the problem. I will post the code here:
#!/bin/bash
Declaracao de variaveis
EXTEN=$1 #Numero de telefone recebido do Asterisk
ID=$2 #Uniqueid
LOG="/tmp/log"
Comando para extrair do banco do Id do agente que atendeu a chamada
MSQL=$(mysql -u root --password=“mypass” -e “select qagent from dbname.dbtable where uniqueid=$2 and event=4”)
Enviar pop up de acordo com o array relacionado ao ID do agente:
#LOG
echo "
Data: $(date +%d/%m/%Y)
Horario: $(date +%H:%M:%S)
Numero do cliente: $EXTEN
Uniqueid da chamada: $ID
Id do agente:$AGT
**************************************" >> $LOG
echo “$EXTEN |nc -w 1 ${agente[$AGT]} 10629”
end
So, as I said before, Asterisk is running as root, but I still think it is a permission problem. But I can’t see where.
I wrote the same script in PHP, and I still have problems with the connection.
Thanks!
When you run a shell command interactively, the shell tries to prefix command names that don’t contain a “/” with each of the strings contained in the $PATH environment variable. I have a suspicion that, when Asterisk starts an AGI script, the PATH environment variable is empty, in which case, you must either set it in the script, or must include the full path name in all command names.
@david55
I included both mysql (/usr/sbin/mysql) and the script (/var/lib/asterisk/agi-bin/shell.sh) in the $PATH and it is not working yet … Here’s the output of the $PATH:
Despite of the error line (510 Invalid command), it works fine - when I don’t use the mysql script this works fine.
The command, if the shell script worked fine should be like: 45063119 << nc -w 1 192.168.x.x 10629
@navaismo
I tried with PHP and it’s not working too.
The mysql query it’s ok. When I run this script out from Asterisk enviorment it works pretty fine.
I have a log, take a look:
When I run from Asterisk:
1 << recebido
2
3 **************************************
4 Data: 25/08/2011
5 Horario: 09:36:15
6 Numero do cliente: 45063119
7 Uniqueid da chamada: 1314275772.839
8 Id do agente:
9 **************************************
Line 1 should have the query result (a number) and it’s empty. Line 6 its the fist variable sent by Asterisk and the line 7 is the second variable. So Asterisk is sending the variables correctly.
When I run the commando from Linux: ./shell.sh 45063119 1314275772.839, the output is ok:
***Command output
asterisk:/var/lib/asterisk/agi-bin# ./shell.sh 45063119 1314275772.839
45063119 |nc -w 1 192.168.2.26 10629 --> this is the command I need to run
*** LOG FILE
52 << recebido
**************************************
Data: 25/08/2011
Horario: 09:40:10
Numero do cliente: 45063119
Uniqueid da chamada: 1314275772.839
Id do agente:52
**************************************
MM but when the query is performed what its the result? can you echo that?
try with this:
[code]#!/usr/bin/php -q
<?php
set_time_limit(30); // Habilitamos el control de PHPAGI y reporte de errores
require('phpagi/phpagi.php');
error_reporting(E_ALL);
$host="localhost"; // Datos de conexion a la BD
$user="root";
$pass="mypass";
$db="dbname";
$exten=$argv[1];
$id=$argv[2];
$agi = new AGI(); // Creamos el canal AGI y contestamos
$agi->answer();
$link = mysql_connect($host,$user,$pass) or die(mysql_error()); //Conexion a la BD
mysql_select_db($db, $link);
$MSQL=mysql_query("SELECT qagent FROM dbtable WHERE uniqueid=$id AND event=4");
$agi->Verbose($MSQL); cual es el resultado del query?
$agi->Hangup(); cuelga
?>
[/code]
Requieres PHPAGI and only sends to the asterisk cli the result of the query