📜 ⬆️ ⬇️

Automatic generation of Dial Patterns for Asterisk from DEF codes of cellular operators

In the office we use GSM gateways for outgoing calls to cellular. But there was a task to limit the range of numbers in outboud routes asterisk, so that users do not call anywhere. Namely, to allow outgoing communication only to the cellular of their region.

Then you can read how to automate the Dial Up update of outgoing calls to Asterisk using a publicly available list of DEF codes of cellular operators.

Automation has been the Asterisk server 1.8.6.0 , running on the distribution FreePBX 2.10.1.1 .

So, for starters, we needed to get somewhere a list of DEF codes of cellular operators that could be analyzed.
It turned out that the Rossvyaz website possesses such information, and periodically updates it.
Thus, using this link you can get the latest updated list of DEF codes of cellular operators in html format.
')
Since I would like to push the Dial Patterns update into the crontab, it was decided to parse this html file on bash.
Well, since bash does not say that it is convenient for word processing, the main translation function in Dial Patterns is written in awk.

So, after loading the processing of html with all sorts of grep and sed commands, we got a csv file, with a separator;, like:
913;0000000;0199999;200000; ;  913;0600000;0699999;100000; ;  913;2000000;2099999;100000; ;  913;3700000;3999999;300000; ;  

First column: DEF code
The second and third columns: the range of numbers allocated (from and to)
Fourth column: total number of allocated numbers
Fifth column: Operator
Sixth column: Region

This kind of view after that was required to be converted into Dial Patterns format , which in general was the main task, which was done using the awk language. At the exit of the script was a list:
 901458XXXX 901459XXXX 903049XXXX 903076XXXX 90390[0-6]XXXX 90393XXXXX 90399[7-9]XXXX 90509[4-5]XXXX 

Well, in order not to press anything at all, after the formation of Dial Patterns, they are loaded into the FreePBX database, and the application of the configuration in FreePBX is initiated through curl. ( !!! Warning !! you need to know the id of the route to which the templates will be applied, well, the data itself is written to the database tables individually. Therefore, pay attention to what exactly you need)

This is actually the script itself, which performs all the data manipulations, which can then be written 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 "^<tr>" | sed -e 's/<\/td>//g' -e 's/<tr>//g' -e 's/<\/tr>//g' -e 's/[\t]//g' -e 's/^<td>//g' -e 's/<td>/;/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<count) razm=count; } out_str[count]=init_str j ret_diap(0,substr($3,(i+1),1)); i++; } else { for (r=1; r <= razm;r++) { out_str[r]=out_str[r]"["substr($2,i,1)"-"substr($3,i,1)"]"; } } } } } for (r in out_str) { print 8DEF out_str[r]; } }' #  awk,   - Dial Patterns cat codes | awk -F ';' "$awk_code" > 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: script and function does not claim to be ideal and universal. But if you wish, you can remake to fit your needs very easily.

Source: https://habr.com/ru/post/151401/


All Articles