Adding an extension programmatically

Hi all,

I’m wanting to add an extension to the asterisk database programmatically, and I am also wanting to set the announcement message for the voicmail to be a system recording (basically I’ll be dynamically adding extensions and all will have the same announcement message).

Has anyone done something like this before? If so (or even if not) can anyone point me in the right direction for details on which tables I need to add records to and also how to set the announcement message to be a system recording?

The extensions are only going to be used for voicemail, and will not be used to dial out or for a live person to be answering.

I have found some of the tables where I need to modify to add the extension, but I just want to make sure I don’t leave anything important out. And I haven’t yet figured out how to set the announcement message up either.

Thanks in advance for your help!


Dave Phillips


You can install the Bulk Extensions from module management. That can create/edit/delete extensions as often as you need. We automate it with a Perl script that generates the CSV file from an external SQL database, then posts the CSV file to the web page.

The voicemail greetings are just sound files, so you could replace them with symlinks to the system recording you choose. That could be done by the same script that automates running Bulk Extensions. Anyway, I hope this gives you some ideas to get started.

Thanks, Seth

Okay, here’s the thing and I probably should have included this in the previous post:

  1. I’m using PBXInAFlash distribution
  2. I’m not able to modify the asterisk server itself at this time (due to my lack of understanding of the whole thing) however, I AM able to modify the mySQL asterisk database utilizing a language that I AM familiar with.
  3. I’m looking for a solution where I can just insert the necessary rows into the asterisk DB and/or FTP the necessary file (unavail.wav) into a specific folder on the asterisk server, as I’m doing all this REMOTELY from another server.

Is this even possible?




Sorry to say, but the voicemail records are not in the SQL database. That makes things a little more difficult for everyone. Here’s what you need to do:

  1. Install Bulk Extensions. (can be done with FreePBX web admin and FTP, search the FreePBX site if you need instructions for installing a module.)

  2. Examine the template.csv file that comes with Bulk Extensions. Your SQL experience should help you generate a new CSV file with the needed columns.

  3. Use the Bulk Extensions web page to load your CSV file with a web client like Firefox, or a command line client like wget or curl. All your new voicemail accounts should be created. (You can also delete accounts with Bulk Extensions. See the template.csv for samples.)

  4. Make sure that the voicemail directories are created at /var/spool/asterisk/voicemail/default/[ext]/. If any don’t exist, leave a voicemail message for that extension (calls can also be automated with call files, which can be created remotely and copied over by FTP), and the directories will automatically be created. For each new extension, FTP your standard greeting file to that directory. Depending on the codecs you have enabled, you may need .gsm, .wav, and/or .WAV sound files for your greeting message(s).

Does that sound workable? Please post back how things go.

Thanks, Seth


Thanks alot for your help. Okay, here’s an update:

  1. I finally figured out how to get the module installe.d Thanks - learning a lot! :slight_smile:

  2. So the module is installed, and I can see the template.csv file. And yes, I can generate a new CSV file with no problem. So, each time I need to add an extension, I will generate a new CSV file because I want it to be added immediately.

  3. So now I have the CSV file. Now I’m not sure what to do from here. I did some searching and found the following page under /var/www/html/admin/modules/bulkextensions/page.bulkextensions.php

I’m assuming this is the page to execute, and that I should be able to run by http://servername/admin/modules/bulkextensions/page.bulkextensions.php

However, I get a ‘forbidden’ error when I try to access that. Not sure what to do about that.

Anyway, once I get that running, I’m assuming I can POST the template.csv file to that page from my other server. Assuming that works, then that part of the process will be fine and the extension will get added.

  1. What I’m planning for this is to connect via FTP to the asterisk server and create the necessary folder and ftp the necessary file into that directory. I will get to this part after I get #3 working right since it’s a moot point if I can’t make it work.

Any thoughts on how I can get past #3 issue?




I’m glad you got this far. The FreePBX admin interface accesses files below http://servername/admin/ through config.php. The following Perl script should point you in the right direction:


use strict;
use WWW::Mechanize;

my $mech = WWW::Mechanize->new(autocheck => 1);

# default FreePBX user name and password, replace as needed
$mech->credentials( 'admin' => 'admin');



# replace arrivals.csv with the name (and path) of your csv file
$mech->field('csvFile' => 'arrivals.csv');

Thanks, Seth

sasargen, that’s helpful. Would you happen to have a corresponding PHP script that does something similar? I create a two line csv file on another server every time I need to create an extension (through an automated process), and I want to upload that to build_extensions.

Maybe I just need to figure out why the PERL script won’t run. When I try to execute the perl script I get the following:

Can’t locate WWW/ in @INC (@INC contains: /usr/local/lib/perl5/5.8.8/x86_64-linux /usr/local/lib/perl5/5.8.8 /usr/local/lib/perl5/site_perl/5.8.8/x86_64-linux /usr/local/lib/perl5/site_perl/5.8.8 /usr/local/lib/perl5/site_perl .) at line 5.
BEGIN failed–compilation aborted at line 5.

OK, it looks like I need to load the Perl WWW::Mechanize module. Let me try that…

sasargen (or anyone else) I’m hoping you can help with one thing…

I loaded WWW::Mechanize onto my server and I’m using the perl script above (modified for my credentials and file location.

I know the script is being called, I know the authentication is working (because if I change the credentials I get an error), I know the csvFile is being found (because if I change the file and path I get an error), and I know the reload is working (because I see the orange bar appear in FreePBX if I’m in the GUI at the same time, and the reload step takes a long time.

But, the script isn’t updating the extension. I verified the form and field names and if I load the csv file manually it works. There are three buttons on the Bulk Extensions form. Could it be that the “$mech->click();” line isn’t clicking the right button? Could the form have changed since 2008 when this was written. I’m not that familiar with how the click() command knows which button to click.

Please let me know if you have any thoughts on why that line of the script may not be working. Thank!

The www::mechanize approach above will no longer work, because the bulk extensions page relies on javascript (if you turn javascript off in your browser you can no longer load in extensions) and www::mechanize perl module does not support javascript.

I trying to use the page.bulkextensions.php from my php script for add, edit or del extension, sending the parameters by post, but I don’t know what ‘includes’ files I need, I have found some of them: ex:


but the sql() function I can’t find where is. Could some body illuminate??