Skip to main content

Get MySQL login and password

Posted by cbbs70a on Fri, 03/06/2009

This is a little script I wrote that will retrieve the MySQL login and password to be used in the dialplan. This way, that info does not have to be hard coded. Example included.

#!/usr/bin/perl

#
# get_mysql_params.agi - Get the MySQL login and passwd
# so they do not have to be hardcoded in the dialplan. Reads
# /etc/asterisk/res_mysql.conf. Needs PERL module Config::Std
# and Asterisk::AGI. Dialplan example:
# exten => s,1,AGI(get_mysql_params.agi)
# exten => s,2,MYSQL(Connect connid ${DBHOST} ${DBUSER} ${DBPASS} ${DBNAME})
# By FSD. fsd@voipbusiness.us
#

use strict;
use Config::Std;
use Config::Std { def_sep => '=' };
use Asterisk::AGI;

our $AGI = new Asterisk::AGI;

my $configfile = "/etc/asterisk/res_mysql.conf";
read_config "$configfile" => my %config;
my $dbhost = $config{general}{'dbhost'};
my $dbname = $config{general}{'dbname'};
my $dbuser = $config{general}{'dbuser'};
my $dbpass = $config{general}{'dbpass'};
my $dbport = $config{general}{'dbport'};

$AGI->set_variable('DBHOST',$dbhost);
$AGI->set_variable('DBNAME',$dbname);
$AGI->set_variable('DBUSER',$dbuser);
$AGI->set_variable('DBPASS',$dbpass);
$AGI->set_variable('DBPORT',$dbport);

exit(0);