[Asterisk-Users] realtime app data formatting

snacktime snacktime at gmail.com
Sat May 21 21:17:29 MST 2005


Here is a quick script that will parse extensions.conf, any files
included via #include, and print out the sql commands to put them into
mysql.

I'll add on routines to do the same for sip, iax, and voicemail when I
get the chance.

Chris


-----------------------------------------------------------------------------
#!/usr/bin/perl

# Copyright (c) 2005 Chris Ochs

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

use IO::File;
$path = "/etc/asterisk";
my $database = "asterisk";
my $table = "extensions";
my $context;
$file = $ARGV[0];

if(!-e "$path/$file" || $file eq '') {
  print "File does not exist\n";
  exit;
}

print "USE $database;\n";

&parse_config($file);


sub parse_config {
  my ($file) = @_;
  my $fh = IO::File->new("$path/$file");
  while ($line = <$fh>) {
     chomp $line;
     if ($line =~/^\[(.*)\]$/) {
        $context = $1;
     }elsif ($line =~/(^[\s]{0,9}#include[\s]{0,9}.*$)/) {
        my $file_include = $1;
        $file_include =~s/#include[\s]{0,9}//g;
        $file_include =~s/\s//g;
        &parse_config($file_include);
     }elsif ($line
=~/(^[\s]{0,9}exten[\s]{0,9}=>[\s]{0,9})([_.XN0-9a-zA-Z]{0,20}),([_.XN0-9a-zA-Z]{0,20}),(.*)/)
{
        my (undef,$exten,$priority,$action) = ($1,$2,$3,$4);
        my ($app,$appdata);
        if ($action =~/(^[a-zA-Z]{0,32})\((.*)\)/) {
           ($app,$appdata) = ($1,$2);
        }elsif($action =~/(^[a-zA-Z]{0,32}),(.*)/) {
           ($app,$appdata) = ($1,$2);
        }else{
           $app = $action;
           $appdata = '';
        }
        $appdata =~s/,/\|/g;
        print "INSERT INTO extensions
(context,exten,priority,app,appdata) VALUES ('$context', '$exten',
'$priority', '$app', '$appdata');\n";
     }
  }
  $fh->close;
}



More information about the asterisk-users mailing list