[Asterisk-Users] Convert extensions.conf INTO MySQL script
Matthew Boehm
mboehm at cytelcom.com
Fri Jun 10 15:37:23 MST 2005
Chris Coulthurst wrote:
> I swear I read somewhere on one of the MANY pages that there is a script out
> there that can read the extensions.conf file and create the MySQL DB records
> on the fly. Anyone know where I look for such a thing?
>
>
> Sure speeds up migration!
>
> Chris Coulthurst
> chris at shuksan.com
That will be $10. Please pull up to the next window.
#!/usr/bin/perl -w
// originally downloaded from http://www.bkw.org/load.txt
//
// coding by pfn
//
use DBI;
use strict;
use POSIX;
if (@ARGV != 1) {
print STDERR "Usage: load_res_config <ast_config_file>\n";
exit 1;
}
open(CONFIG_FILE, "<$ARGV[0]") || die $!;
my @lines;
my $cat_metric = -1; # incremented to 0 on first hit
my $var_metric = -1;
my $category;
while (<CONFIG_FILE>) {
my $line = $_;
chop($line);
my($var_name, $var_val);
next if ($line =~ /^\s*;/); # comment line skip
if ($line =~ /^\s*\[(.*?)\]/) {
$category = $1;
$var_metric = -1;
$cat_metric++;
} elsif ($line =~ /^\s*(\w+)\s*=>\s*(.+)\s*;?.*$/ ||
$line =~ /^\s*(\w+)\s*=\s*(.+)\s*;?.*$/) {
$var_metric++;
$var_name = $1;
$var_val = $2;
} else {
next; # no match, skip
}
if ($var_metric >= 0) {
my %hash = ('cat_metric' => $cat_metric,
'var_metric' => $var_metric,
'category' => $category,
'var_name' => $var_name,
'var_val' => $var_val);
push(@lines, \%hash);
}
}
close(CONFIG_FILE);
my $dbh;
$dbh = DBI->connect("dbi:mysql:dbname=asterisk", "root", "asdf") || die
$DBI::errstr;
foreach my $row (@lines) {
print
"$row->{'cat_metric'}\t$row->{'category'}\t$row->{'var_metric'}\t$row->{'var_name'}\t$row->{'var_val'}\n";
my $sth = $dbh->prepare("INSERT into ast_config (filename,
cat_metric, var_metric, category, var_name, var_val) values (?, ?, ?, ?,
?, ?)");
$sth->bind_param(1, $ARGV[0]);
$sth->bind_param(2, $row->{'cat_metric'});
$sth->bind_param(3, $row->{'var_metric'});
$sth->bind_param(4, $row->{'category'});
$sth->bind_param(5, $row->{'var_name'});
$sth->bind_param(6, $row->{'var_val'});
$sth->execute();
warn $sth->errstr if $sth->errstr;
}
$dbh->disconnect;
More information about the asterisk-users
mailing list