Skip to content

Instantly share code, notes, and snippets.

@sattellite
Last active August 29, 2015 14:04
Show Gist options
  • Save sattellite/98f76ac57c87d35fd141 to your computer and use it in GitHub Desktop.
Save sattellite/98f76ac57c87d35fd141 to your computer and use it in GitHub Desktop.
Script for create configuration archives for Eltex TAU-8.IP
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use File::Copy 'move';
use File::Copy::Recursive 'dircopy';
$\="\n";
my $temp = '/usr/local/share/eltex/temporary/';
my $template = '/usr/local/share/eltex/template/';
my $def_opts_start = "
config 'config' 'general'
option use_fxs_profile '0'
config 'config' 'sip'
option 'enablesip' '1'
option 'useproxy' '1'
option 'outbound' '0'
option 'dial_timeout' '10'
option 'registration' '1'
option 'authentication' '1'
option 'ringback' '0'
option 'expires' '1800'
option 'proxyip' '192.168.0.3'
option 'registrarip' '192.168.0.3'
config 'config' 'qos'
option 'udpportmin' '23000'
option 'udpportmax' '26000'
option 'rtp_tos' '0'
option 'sig_tos' '0'
option 'reserved_ip' '192.168.253.1'
";
my $def_opts_end = "
config 'config' 'codecs'
option 'g711a' '0'
option 'g711u' '1'
option 'g729a' '-1'
option 'g729b' '-1'
option 'g729x' '-1'
option 'g723' '-1'
option 'g711pte' '20'
option 'dtmftransfer' 'rfc2833'
option 'flashtransfer' 'rfc2833'
option 'faxtransfer1' '0'
option 'faxtransfer2' '3'
option 'faxtransfer3' '3'
option 'modem' '0'
option 'payload' '101'
option 'silencedetector' '1'
option 'echocanceler' '1'
option 'comfortnoise' '1'
option 'rtcp' '0'
option 'faxdirection' '0'
config 'dialplan' 'type'
option 'dialplan_type' 'regexp'
config 'dialplan' 'regexp'
option 'expression' 'S10, L30 ( )'
";
# Создать директорию для хранения распакованной конфигурации
mkdir $temp unless (-d $temp);
# Выборка всех устройств
my $db = DBI->connect("DBI:mysql:database=billing;host=hostname",
'readonlyuser',
'sup3rpassw0rd'
) or die $DBI::errstr;
my $devices = $db->selectall_arrayref("SELECT mac FROM table1 WHERE type = 'eltextau8';");
# Основной цикл, в котором генерируются конфиги для каждого найденного устройства
for (@{$devices}) {
my $nas = {
id => $_->[0],
mac => convert_mac($_->[1])
};
my $ports = $db->selectall_hashref("SELECT number, port, DECODE(password, 'secretkey') AS password FROM table1, table2, table3 WHERE nas_id = $$nas{id};", 'port');
generate_config($nas,$ports);
generate_file($nas,$ports);
}
sub generate_file {
# Генерация конфигурационного архива
my ($nas, $ports) = @_;
my $command = '/usr/local/bin/gtar czf '.$temp.$nas->{mac}.'.tgz -C '.$temp.$nas->{mac}.' tmp';
#my $command = 'tar czf '.$temp.$nas->{mac}.'.tgz -C '.$temp.$nas->{mac}.' tmp';
system($command);
my $st = move($temp.$nas->{mac}.'.tgz', '/var/tftp/'.$nas->{mac}.'.cfg');
}
sub generate_config {
# Генерация конфигурации портов
my ($nas, $ports) = @_;
create_dir($nas->{mac});
my $pbx = $def_opts_start;
for (my $p = 1; $p <= 8; $p++) {
$pbx .= fxs_template($p,$ports->{$p}->{number}, $ports->{$p}->{password});
}
$pbx .= $def_opts_end;
open my $fh, '>', $temp.$nas->{mac}."/tmp/etc/config/pbx";
print $fh $pbx;
close $fh;
}
sub create_dir {
# Создать директорию по переданному mac адресу, если она еще не существует
my $dir = shift;
unless (-d $temp.$dir) {
mkdir $temp.$dir;
# Скопировать директорию-темплейт
my $copy_status = dircopy("$template", "$temp$dir");
}
}
sub convert_mac {
# Конвертирование mac адреса в читаемый устройством вид
my $mac = shift;
$mac =~ s/:/./g;
return uc($mac);
}
sub fxs_template {
# Генерирование конфига на указанный порт устройства
my ($port, $num, $pass) = @_;
my $status = 0;
unless (defined $num) {
$num = "00".$port;
$status = 1;
$pass = '';
}
return <<TMPL
config 'config' 'fxs$port'
option 'profile' 'Default'
option 'phone' '$num'
option 'username' '$num'
option disabled '$status'
option 'minonhooktime' '500'
option 'gainr' '-70'
option 'gaint' '0'
option 'calltransfer' '0'
option 'callwaiting' '0'
option 'hotline' '0'
option 'stop_dial' '0'
option 'ct_busy' '0'
option 'ct_noanswer' '0'
option 'minflash' '200'
option 'minpulse' '100'
option 'interdigit' '200'
option 'auth_name' '$num'
option 'sipport' '5060'
option 'sip_profile_id' '0'
option 'auth_pass' '$pass'
option use_alt_number '0'
option payphone 'off'
TMPL
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment