Automatic generation of Dial Patterns for Asterisk from DEF codes of mobile operators
In the office we use GSM gateways for outgoing calls to cellular. But the task was to limit the range of numbers in outboud routes asterisk so that users did not call anywhere. Namely, allow outgoing communication only to the cellular networks of your region.
Further, you can read how to automate the upgrade of Dial Patterns of outgoing calls to Asterisk using a public list of DEF codes of mobile operators.
The Asterisk 1.8.6.0 server running on the FreePBX 2.10.1.1 distribution kit was subjected to automation .
So, for starters, we needed to get somewhere a list of DEF codes of mobile operators that could be analyzed.
It turned out that the Rossvyaz site has this information, and updates it periodically.
Thus, using this link, you can get the latest updated list of DEF codes of mobile operators in html format.
Since I would like to push the Dial Patterns update into crontab later, it was decided to parse this html-ku on bash.
Well, since bash is not to say that it is convenient for word processing, the main translation function in Dial Patterns is written in awk.
So, after loading the html processing with all sorts of grep and sed commands, we got a csv file, with a separator;, of the form:
First column: DEF code
Second and third columns: range of allocated numbers (from and to)
Fourth column: total number of allocated numbers
Fifth column: Operator
Sixth column: Region
This kind of view afterwards had to be converted to Dial Patterns format , which in general and was the main task that was done using the awk language. The output of the script was a list:
Well, so that you don’t have to press anything yourself, after the Dial Patterns are formed, they are loaded into the FreePBX database, and the configuration application in FreePBX is initiated via curl. ( !!! Attention !! you need to know the id of the route to which the templates will be applied, well, the data recorded in the database tables is purely individual. Therefore, pay attention to what you really need)
Here is the script itself, which performs all the manipulation data, which You can then write to the scheduler on the server:
PS: the script and the function does not claim to be ideal or universal. But if you wish, you can remake it to your needs very easily.
Further, you can read how to automate the upgrade of Dial Patterns of outgoing calls to Asterisk using a public list of DEF codes of mobile operators.
The Asterisk 1.8.6.0 server running on the FreePBX 2.10.1.1 distribution kit was subjected to automation .
So, for starters, we needed to get somewhere a list of DEF codes of mobile operators that could be analyzed.
It turned out that the Rossvyaz site has this information, and updates it periodically.
Thus, using this link, you can get the latest updated list of DEF codes of mobile operators in html format.
Since I would like to push the Dial Patterns update into crontab later, it was decided to parse this html-ku on bash.
Well, since bash is not to say that it is convenient for word processing, the main translation function in Dial Patterns is written in awk.
So, after loading the html processing with all sorts of grep and sed commands, we got a csv file, with a separator;, of the form:
913;0000000;0199999;200000;Мобильные ТелеСистемы;Новосибирская область
913;0600000;0699999;100000;Мобильные ТелеСистемы;Новосибирская область
913;2000000;2099999;100000;Мобильные ТелеСистемы;Новосибирская область
913;3700000;3999999;300000;Мобильные ТелеСистемы;Новосибирская область
First column: DEF code
Second and third columns: range of allocated numbers (from and to)
Fourth column: total number of allocated numbers
Fifth column: Operator
Sixth column: Region
This kind of view afterwards had to be converted to Dial Patterns format , which in general and was the main task that was done using the awk language. The output of the script was a list:
901458XXXX
901459XXXX
903049XXXX
903076XXXX
90390[0-6]XXXX
90393XXXXX
90399[7-9]XXXX
90509[4-5]XXXX
Well, so that you don’t have to press anything yourself, after the Dial Patterns are formed, they are loaded into the FreePBX database, and the configuration application in FreePBX is initiated via curl. ( !!! Attention !! you need to know the id of the route to which the templates will be applied, well, the data recorded in the database tables is purely individual. Therefore, pay attention to what you really need)
Here is the script itself, which performs all the manipulation data, which You can then write to the scheduler on the server:
#!/bin/bash
#файл DEF-кодов
DOWNFILE='http://www.rossvyaz.ru/docs/num/DEF-9x.html';
#рабочая папка
TMPDIR='./';
#файл, где сохраним csv формат кодов
FILENAME='codes';
#какой регион будем выделять
REGION='Новосибирская область';
#id outbound route в FreePBX mysql БД
ROUTE_ID=4
FREEPBX_LOGIN='admin'
FREEPBX_PASS='pass'
FREEPBX_ADRESS='192.168.1.15'
#качаем и преобразуем в формат csv
wget -c -q -O - $DOWNFILE | grep "^" | sed -e 's/<\/td>//g' -e 's///g' -e 's/<\/tr>//g' -e 's/[\t]//g' -e 's/^//g' -e 's//;/g' | iconv -c -f WINDOWS-1251 -t UTF8 | grep "$REGION" > $TMPDIR/$FILENAME
#проверяем не скачали ли пустышку
check=`cat $TMPDIR/$FILENAME`
if [ "$check" == "" ]; then
exit 0
fi
#скрипт на awk генерации Dial Patterns
awk_code='
#функция определения диапазона
function ret_diap(from,to)
{
if ((to-from)==0) return from;
else if ((to-from)==9) return "X";
else return "["from"-"to"]";
}
#основная функция
{
DEF=$1;
razm=1;
delete out_str;
for (i=1; i <= length($3);i++)
{
if ((substr($3,i,1)-substr($2,i,1))==0)
{
for (r=1; r <= razm;r++)
{
out_str[r]=out_str[r] substr($3,i,1);
}
}
else
{
if ((substr($3,i,1)-substr($2,i,1))==9)
{
for (r=1; r <= razm;r++)
{
out_str[r]=out_str[r]"X";
}
}
else
{
if (substr($3,i,1)-substr($2,i,1)>=1 && substr($3,(i+1),1)-substr($2,(i+1),1)!=9)
{
count=1;
init_str=out_str[1];
for (j=substr($2,(i),1); j < substr($3,(i),1);j++)
{
if (count==1)
{
out_str[count]=init_str j ret_diap(substr($2,(i+1),1),9);
}
else
{
out_str[count]=init_str ret_diap(j,(substr($3,(i),1)-1)) "X";
j=(substr($3,(i),1)-1);
}
count++;
if (razm patterns
#удаляем старые паттерны
sql="DELETE FROM outbound_route_patterns WHERE route_id=$ROUTE_ID"
echo $sql
mysql -Dasterisk -e "$sql"
#формируем новые паттерны
sql="INSERT INTO outbound_route_patterns (route_id,match_pattern_pass,match_pattern_prefix) VALUES "
n=1
for i in `cat patterns_mts`
do
if [ $n -eq 1 ]; then sql="$sql ($ROUTE_ID,'$i','')"
else sql="$sql, ($ROUTE_ID,'$i','')"
fi
let n=n+1
done
echo $sql
mysql -Dasterisk -e "$sql"
#авторизуемся на freepbx
curl -c cookies -d 'username=$FREEPBX_LOGIN&password=$FREEPBX_PASS&submit=Login' http://$FREEPBX_ADRESS/admin/config.php > /dev/null
#перезагружаем конфигурацию
curl -b cookies http://FREEPBX_ADRESS/admin/config.php?handler=reload > /dev/null
PS: the script and the function does not claim to be ideal or universal. But if you wish, you can remake it to your needs very easily.