Upgrade 3CX to v18 and get it hosted free!

Connect DialPlan to Remote Mysql Database

Author image

This is a very simple yet effective script that uses the mysql client to connect remotely to a mysql database. The script can be initiated from the DialPlan using System Command

Example of DialPlan Line <extentions.conf>

exten => s,1,System(‘mmysql.sh “INSERT INTO asteriskcdrdb.calls (phone,act,runtime) VALUES(~CALLERID(Number)~,~Dialed~,~12:00AM~)”‘)

Example of the .sh Script to be written <mmysql.sh>

hld=`echo ${1}|sed “s/\~/\’/g”`
mysql -h xxx.xxx.xxx.xxx -uasteriskuser -pasteriskpassword -D ‘asteriskcdrdb’ -e “$hld”

Note that all iterations of quote [‘] are shown as tilda [~] The reason for this is because of the way the
System function handles quotes will not allow you to put them into your SQL statement.
So if you notice the first line of the .sh Script has a conversion back to the real [‘] from [~]

The xxx.xxx.xxx.xxx can be replaced with the ip address or host of the remote mysql database.
I would also suggest for security that you create a seperate user in your remote mysql user list
that can only connect from the host you are putting this on.

The variable hld that uses the sed command works like this

`echo ${1}|sed “s/\~/\’/g”`

–>`echo ${1} (echo’s the first command line parameter to the sed command)
–> | (pipes the command line parameter to the other side of the line)
–> sed “s/\~/\’/g”` ( s means separate files. / is a separator in the command
\~ represents the ~ symbol. / another separator. \’ represents the ‘
/ another separator. g stands for global meaning the whole string
Note: Charaters must be escaped with \ to be swapped correctly)

This can be done for any character with a simple swap in the SQL and an extra line in the .sh statement to do the same thing.

Note: the mmysql.sh script must be put in a place where your system knows the path too such
as /bin or /usr/bin. if you are not sure then type echo $PATH at the command prompt

Thanks
you can email me at schapman1974@gmail.com if you have any questions.

9388

See also


Article Reviews

Write a Review

Your email address will not be published. Required fields are marked *

Required Field. Minimum 5 characters.

Required Field. Minimum 5 characters, maximum 50.

Required field.There is an error with this field.

Required Field.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

There are no reviews for this article. Be the first one to write a review.
Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.