How to get data in asterisk from bash script?


I want to import data (numbers, strings) from bash script to asterisk.
results from System(script) or AGI(script) are 0 or -1 only
CLI> !cat /home/asterisk/test
show file on screen but i don’t know how to use it in dialplan
to store the result in variable.

does anyone have some hints for me ?

thanks in advance

Have you met this requirements for AGI scripts

*  must be executable
* must be located in /var/lib/asterisk/agi-bin
* must be specified in the dialplan complete with an extension 

taken from

Yes. i know haw to make bash script runable.

Does anybody know about existence of aplication (like Dial or Meetme)
that can ask database (postgresql,mysql) in SQL format for value and result
can be assigned to asterisk variable ?
that would be perfect for me.


have you looked at the application MYSQL ??

although to be quite honest, AGI has always been the way i get values from a db to assign to variables.

i don’t know how to use AGI for that… yet.
specially runnig AGI(bash.script) and that
script should set my_variable=5 in asterisk

The asterisk first sends some variables to the agi script.
The agi script needs to read them from the stdin.

The commands to asterisk are send through stdout.
The results of each command are immediately returned and
should be read through the stdin.

There is a agi script in bash for your reference.

here’s a simple AGI i wrote to determine call blocking for a clients system(there’s lot’s more, this is just one of the AGI scripts) :

[code]#!/usr/bin/perl -w

Call Blocking AGI

Baconbuttie 2006 tony [at] baconbuttie [dot] co [dot] uk

You’re free to use this code, and free to give at to others.

You can’t claim you own it though, nor remove this copyright block.

use DBI;
use Asterisk::AGI;

my $debug = 1; # Debug level, lower to reduce logging
my $dnid; # Called number
my $caller; # Caller for this call
my $priority; # Next priority
my $result; # DB result

my $AGI = new Asterisk::AGI;
my %input = $AGI->ReadParse();

$dnid = $input{“dnid”};
$caller = $input{“callerid”};
$priority = $input{‘priority’};

if ($debug >= 2) {
foreach $key (keys %input) {
debug("$key = ".$input{$key},3);

my $dbh = DBI->connect (“dbi:mysql:host=localhost:database=asterisk”,“user”,“password”) or die “Can’t connect to database: $DBI::errstr”;
my $statement = “SELECT * FROM blocklist WHERE caller IN (0,’”.$caller."’) AND callnumber = ‘".$dnid."’ AND patmatch = 0" or die "Couldn’t prepare statement: " . $dbh->errstr;
if ($debug >= 2) {debug("CallBlock Statement = ".$statement,3);}
$result = $dbh->selectall_arrayref($statement);
unless ($result) {

check for errors after every single database call

print “dbh->selectall_arrayref($statement) failed!\n”;
print “DBI::err=[$DBI::err]\n”;
print “DBI::errstr=[$DBI::errstr]\n”;

@resultSet = @{$result};
if ( $#resultSet == -1 ) {

check for pattern match entry

my $statement = "SELECT * FROM blocklist WHERE caller IN (0,\'".$caller."\') AND callnumber = \'".substr($dnid,0,4)."\' AND patmatch = 1" or die "Couldn't prepare statement: " . $dbh->errstr;
if ($debug >= 2) {debug("CallBlock Statement = ".$statement,3);}
$result = $dbh->selectall_arrayref($statement);
unless ($result) {
# check for errors after every single database call
print "dbh->selectall_arrayref($statement) failed!\n";
print "DBI::err=[$DBI::err]\n";
print "DBI::errstr=[$DBI::errstr]\n";

@resultSet = @{$result};
if ( $#resultSet == -1 ) {
debug("No CallBlock found",3);
$priority = $priority + 1;
debug("Setting priority = ".$priority,3);
exit 0;
 else {
debug("Valid Patternmatch CallBlock found",3);
$priority = $priority + 101;
debug("Setting priority = ".$priority,3);
exit 0;

else {
debug(“Valid CallBlock found”,3);
$priority = $priority + 101;
debug("Setting priority = ".$priority,3);
exit 0;


sub debug
my $string = shift;
my $level = shift || 3;
if ($debug) {
$AGI->verbose($string, $level);

exit 0;

all fairly simple. and you can use $AGI->set_variable('varname', varvalue); to set variable values.

does that help ?

i seen that link alredy

set up callerid on my own bash script
but can’t set up diffrent variables.
i have to educate myself more.

Use the set agi command SET VARIABLE

This can set the channel variables just like in extension.
Is this what you want?

very thanks


i should have no problem now.

One thing about AGI I am not very sure:

There is no way to return to the dialplan from the AGI.
Once the control is given to the AGI it doesn’t return.
Is there any way this issue can be solved?
Or is it a issue anyway?

But sometimes I do think we need the AGI to return to the
dialplan … …???

how about “exit 0;” (depending on your preferred language of course)

never been a issue with me yet.

Thats great, I was running into hangup in agi which caused
the termination.

i have some results, thanks for Your help.

exten => 1,1,answer
exten => 1,2,AGI(/home/asterisk/|${CALLERIDNUM})
exten => 1,3,noop(--value1=${test}-value2=${test2}-)
exten => 1,4,noop(${CALLERIDNUM} called=${a${CALLERIDNUM}} times)
exten => 1,5,hangup script

echo "SET VARIABLE test 50"
echo "SET VARIABLE test2 150"
echo "SELECT count(*) from cdr where src='$od'" > /home/asterisk/zapytanie.sql
zmienna=`su -c "psql asterisk < /home/asterisk/zapytanie.sql" postgres | head -n3 | tail -n1 | tr -d " "`
echo "SET VARIABLE a$od $zmienna"

CLI log

exten => 1,2,AGI(/home/asterisk/|${CALLERIDNUM})

The callerid is also passed to agi script. You needn’t pass it as an arg.

At script startup time Asterisk sends various pieces of information to your
script. Each item is sent on a line terminated with a newline and the end
of the list is indicated by an empty line.
One of the information callerid that is set in agi_callerid.