📜 ⬆️ ⬇️

Rapid deployment of the telephone network to Asterisk + Cisco

It so happened that in a short time it became necessary to transport 70 people with analog phones from one business center to another. The situation was aggravated by the fact that in the new office the owner did not have analog ports in the PBX, and the PBX in the old one belonged to the telephone company. I had to quickly implement IP telephony with the transfer of all analog city lines to Asterisk. The delivery of equipment was scheduled for the day preceding the day of the move, which meant that there would be very little time to deploy telephony.

What came of it under the cut.
A lot of material, so do not be alarmed.

So the available configuration:


Searching and reading a lot of documents related to autoprovision on the network, we decided to use the asterisk opportunity to auto-configure devices on our own. All anything, but as it turned out auto-tuning through the sql database in asterisk does not allow monitoring the state of the extension on the doppanel. Those. hint cannot be configured (at least in August 2012 it was not supported). Having a couple of phones available, we decided to write our own autoprovision.
')
We started with setting up the server and the switches. Created 2 VLANs with numbers 204 and 214. The first for the local network, the second for IP telephony. Since phones have a built-in configurable switch, it was the best solution from our point of view.
Put the ports on the switches to which users connect to the trunk and change the native vlan.
interface fastethernet1 switchport trunk allowed vlan add 204 switchport trunk native vlan 214 exit 
We connected the server to the switch in the trunk port and changed the settings of dhcpd.conf
 # Phones Subnet Vlan 214 subnet 172.16.214.0 netmask 255.255.255.0 { range 172.16.214.10 172.16.214.250; option routers 172.16.214.1; option tftp-server-name "http://172.16.214.1/XMLDefault.cnf.xml"; option domain-name "phones.mydomain.local"; option domain-name-servers 172.16.214.1; option broadcast-address 172.16.214.255; ddns-updates on; ddns-domainname "phones.mydomain.local"; ddns-rev-domainname "in-addr.arpa"; } # Computers Subnet Vlan 204 subnet 172.16.6.0 netmask 255.255.255.0 { range 172.16.6.12 172.16.6.240; option broadcast-address 172.16.6.255; option domain-name-servers 172.16.6.1; option domain-name "mydomain.local"; option routers 172.16.6.1; if option host-name = "" { option host-name = concat ("dev-", binary-to-ascii( 10, 8, "", substring( reverse( 1, leased-address), 0, 1))); ddns-hostname = concat ("dev-", binary-to-ascii( 10, 8, "", substring( reverse( 1, leased-address), 0, 1))); } } 

Fine. Addresses are distributed, the phones are bugged, it's time to gash the basic config.
Raise thttpd and put it in the root XMLDefault.cnf.xml
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <flat-profile xmlns="http://www.sipura.net/xsd/SPA50x-30x-SIP" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sipura.net/xsd/SPA50x-30x-SIP http://www.sipura.net/xsd/SPA50x-30x-SIP/SPA50x-30x-SIP-7-5-2.xsd"> <Admin_Passwd ua="na">7654321</Admin_Passwd> <SPCP_Auto-detect ua="na">No</SPCP_Auto-detect> <Domain ua="rw">phones.mydomain.local</Domain> <Primary_DNS ua="rw">172.16.214.1</Primary_DNS> <Syslog_Server ua="na">172.16.214.1</Syslog_Server> <Debug_Server ua="na">172.16.214.1</Debug_Server> <Debug_Level ua="na">0</Debug_Level> <Primary_NTP_Server ua="na">172.16.214.1</Primary_NTP_Server> <Enable_VLAN ua="rw">Yes</Enable_VLAN> <Enable_PC_Port_VLAN_Tagging ua="na">Yes</Enable_PC_Port_VLAN_Tagging> <Enable_CDP ua="na">No</Enable_CDP> <Enable_LLDP-MED ua="na">No</Enable_LLDP-MED> <PC_Port_VLAN_ID ua="na">204</PC_Port_VLAN_ID> <Profile_Rule ua="na">http://172.16.214.1/XMLDefault.cnf.xml</Profile_Rule> <Profile_Rule_B ua="na">http://172.16.214.1/cfg/cfg.cgi?SN=$SN&MAC=$MA</Profile_Rule_B> <Key_System_Auto_Discovery ua="na">No</Key_System_Auto_Discovery> <G722_Enable_1_ ua="na">Yes</G722_Enable_1_> <L16_Enable_1_ ua="na">No</L16_Enable_1_> <G726-16_Enable_1_ ua="na">No</G726-16_Enable_1_> <G726-24_Enable_1_ ua="na">No</G726-24_Enable_1_> <G726-32_Enable_1_ ua="na">No</G726-32_Enable_1_> <G726-40_Enable_1_ ua="na">No</G726-40_Enable_1_> <Enable_IP_Dialing_1_ ua="na">No</Enable_IP_Dialing_1_> <Use_Remote_Pref_Codec_1_ ua="na">Yes</Use_Remote_Pref_Codec_1_> <Time_Format ua="rw">24hr</Time_Format> <Date_Format ua="rw">day/month</Date_Format> <Text_Logo ua="na">Company</Text_Logo> <Time_Zone ua="na">GMT+04:00</Time_Zone> <Upgrade_Rule ua="na">( $SWVER ne 7.5.2b )? http://172.16.214.1/sw/spa50x-30x-7-5-2b.bin</Upgrade_Rule> </flat-profile> 

Thus, after the phone takes the autoconfiguration parameters from dhcp and takes the basic config from the server, it will do the second iteration after the configuration file associated with the device’s serial number and its mac address. After full initialization of the config, the phone will transfer the computer port to 204 VLAN, ensuring it works with the local network.
Let's go to how the phones get their configuration file.
Create an auxiliary file with the settings /usr/local/etc/astprov.conf
 sqlite="/usr/local/bin/sqlite3" ast_provisiondb="/var/db/asterisk/asterisk_provision.sqlite3" ast_ext_dialplan="/var/db/asterisk/asterisk_ext_dialplan.conf" ast_ext_accounts="/var/db/asterisk/asterisk_ext_accounts.conf" logger_tag="astprov" include="/etc/rc.conf" 


Let's create the sqlite3 base of the following structure specified in the configuration file:
 PRAGMA foreign_keys=OFF; BEGIN TRANSACTION; CREATE TABLE `provision` ( `macaddress` varchar(12) NOT NULL, `serial` varchar(12) NOT NULL, `secret` varchar(32) NOT NULL, `ext` int(11) NOT NULL, `fullname` varchar(64) NOT NULL, `callerid` varchar(64) NOT NULL, `callgroup` varchar(32) NOT NULL default '1', `pickupgroup` varchar(32) NOT NULL default '1', `context` varchar(32) NOT NULL, `subscribecontext` varchar(32) NOT NULL default '1', `ip` varchar(15) NOT NULL); COMMIT; 


For the subsequent quick setup of workstations we create a file with phones
 3027|Buhgalter 3097|Igor 3018|Sergey 3016|Oleg 3091|Vladimir 3014|Ekaterina 3012|Andrey 3015|Maxim 


and feed it to the script as a parameter
 #!/bin/sh set -x . /usr/local/etc/astprov.subr context='local_pool' callgroup=1 pickupgroup=1 subscribecontext=1 ip="none" cat $1 | while IFS= read -r line; do randomstr=`< /dev/urandom tr -dc A-Za-z0-9 | head -c10` randompas=`< /dev/urandom tr -dc A-Za-z0-9 | head -c10` ext=$(echo $line | cut -d '|' -f1 ) fullname=$(echo $line | cut -d '|' -f2 ) # fullname <<< $(IFS=";"; echo $line) insertline="insert into provision values ('$randomstr','$randompas','$randompas',$ext,'$fullname','$fullname <$ext>',$callgroup,$pickupgroup,'$context',$subscribecontext,'$ip')" # echo $insertline $sqlitecmd "$insertline" done 

the script will automatically fill the base and give the ability to quickly connect new phones.

Now a little witchcraft.
Asterisk works under the asterisk: asterisk account, thttpd works under www: www. Therefore we create the astprov group where we add asterisk and www.
 # chown -R asterisk.astprov /var/db/asterisk # chmod 0775 /var/db/asterisk # chmod 0664 /var/db/asterisk/* 


Now you need to personalize the phone settings.
Create a script /usr/local/www/data/cfg/cfg.cgi , I love perl, therefore the configurator on pearl.
 #!/usr/bin/perl use strict; #use Data::Dumper; use DBI; use vars qw/%sv %form %cookie %rq $sth $dbh $config/; use FileHandle; use Sys::Syslog qw(:standard); my $configfilename="/usr/local/etc/astprov.conf"; $config=_read_config_file($configfilename); &systeminit; &printhead; #$form{SN}='CBT1602095Z'; #$form{MAC}='649ef37761c2'; #$sv{ip}="172.16.214.10"; openlog("astprov", 'cons,pid'); exit(1) if (not defined $form{SN} or not defined $form{MAC}); &baseconnect; &get_info; &baseclose; closelog(); exit(1); sub get_info { $form{SN} =~ s/[^0-9A-Za-z]//g; $form{MAC} =~ s/[^0-9A-Za-z]//g; my $dbdata; if($dbdata = &request_phone_info) { &print_xml($dbdata); } else { if(keys %{$dbdata} < 1) { &insert_new_phone; } $dbdata = &request_phone_info; &print_xml($dbdata); } #print Dumper $dbdata; } sub print_xml { my $dbdata=shift; my $additional; if(-f "/usr/local/www/data/cfg/additional".lc($form{MAC}).".xml") { open IN,"</usr/local/www/data/cfg/additional".lc($form{MAC}).".xml"; undef $/; $additional = <IN>; close IN; } my $hostname = "office-".$dbdata->{ext}; print << "[end]"; <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <flat-profile xmlns="http://www.sipura.net/xsd/SPA50x-30x-SIP" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sipura.net/xsd/SPA50x-30x-SIP http://www.sipura.net/xsd/SPA50x-30x-SIP/SPA50x-30x-SIP-7-5-2.xsd"> <HostName ua="rw">$hostname</HostName> <Phone-UI-readonly ua="na">Yes</Phone-UI-readonly> <Phone-UI-user-mode ua="na">Yes</Phone-UI-user-mode> <Proxy_1_ ua="na">172.16.214.1</Proxy_1_> <Display_Name_1_ ua="na">$dbdata->{fullname}</Display_Name_1_> <User_ID_1_ ua="na">$dbdata->{ext}</User_ID_1_> <Password_1_ ua="na">$dbdata->{secret}</Password_1_> <Dial_Plan_1_ ua="na">(7[0-9][0-9]xxxxS0|*8|*xx|xxxx|0xxxxxxxxxxxx.)</Dial_Plan_1_> <Station_Name ua="na">$dbdata->{ext}</Station_Name> <Station_Display_Name ua="na">$dbdata->{fullname}</Station_Display_Name> <Server_Type ua="na">Asterisk</Server_Type> <XML_Directory_Service_Name ua="na">My Company</XML_Directory_Service_Name> <XML_Directory_Service_URL ua="na">http://172.16.213.1/directory.cgi</XML_Directory_Service_URL> $additional </flat-profile> [end] } sub request_phone_info { my $cmd = "select ext,callerid,fullname,secret from provision where macaddress='".lc($form{MAC})."' and serial='".lc($form{SN})."'"; my $dbdata = $dbh->selectrow_hashref($cmd); if($dbh->err) { print "cmd = ",$cmd,"\n"; print "err = ",$dbh->err,"\n"; print "errstr = ",$dbh->errstr,"\n"; print "state = ",$dbh->state,"\n"; return undef; } return $dbdata; } sub insert_new_phone { my $maxnum = $dbh->selectrow_array("SELECT MAX(ext) FROM provision where ext < 2000") || "1000"; $maxnum++; $dbh->do("INSERT INTO provision (ip,macaddress,serial,secret,ext,fullname,callerid,context) VALUES ('".join ("','",$sv{ip},lc($form{MAC}),lc($form{SN}),lc($form{SN}),$maxnum,'Unregistered','Unregistered <'.$maxnum.'>','unreg')."')"); syslog('info|local7',"New host added MAC:$form{MAC} SN:$form{SN}"); if($dbh->err) { print "err = ",$dbh->err,"\n"; print "errstr = ",$dbh->errstr,"\n"; print "state = ",$dbh->state,"\n"; exit(1); } } sub systeminit { $sv{"ip"} = $ENV{"REMOTE_ADDR"}; $sv{"userhost"} = $ENV{"REMOTE_HOST"}; $sv{"url"} = $ENV{"HTTP_HOST"}; $sv{"doc"} = $ENV{"DOCUMENT_ROOT"}; $sv{"ref"} = $ENV{"HTTP_REFERER"}; # ---------------------------------------------------------------------------------------- my $request_url = $ENV{"REQUEST_URI"}; $request_url =~ s/%(..)/pack("c",hex($1))/ge; $request_url =~ s/[^A-Za-z0-9\-\_\+\=\:\.\,\/\@]//g; $request_url =~ s/([\-\_\+\=\.\:\,\/\@\s]){2,}/$1/g; $request_url =~ s/\+/\&/g; $request_url =~ s/^\/+//g; my $count = 0; while ($request_url =~ /^([\w\-\=\_\&\.\,\:\@\s]+)\//) { $rq{$count} = $1; $request_url =~ s/$rq{$count}\///; $count++; } $request_url =~ s/^\s+$//g; chomp $request_url; $rq{$count} = $request_url if (length($request_url) > 0); # ---------------------------------------------------------------------------------------- if ( defined($ENV{"HTTP_COOKIE"}) && length($ENV{"HTTP_COOKIE"}) > 0 ) { my @cookies = split(/;/,$ENV{"HTTP_COOKIE"}); foreach (@cookies) { my ($name,$value) = split(/=/,$_); $cookie{$name} = $value; } } # ---------------------------------------------------------------------------------------- if ((defined($ENV{"QUERY_STRING"}) && length($ENV{"QUERY_STRING"}) != 0) || (defined($ENV{"CONTENT_LENGTH"}) && $ENV{"CONTENT_LENGTH"} != 0)) { my $data = undef; my @data = undef; if ($ENV{"REQUEST_METHOD"} eq "GET") { $data = $ENV{"QUERY_STRING"}; } else { read(STDIN,$data,$ENV{"CONTENT_LENGTH"}); } @data = split(/&/,$data); foreach (@data) { $_ =~ s/\+/ /g; my ($name, $value) = split(/=/,$_,2); $name =~ s/%(..)/pack("c",hex($1))/ge; $name =~ tr/[^A-Za-z0-9\-\_\$\+\=\~\.\,]//; $value =~ s/%(..)/pack("c",hex($1))/ge; $form{$name} .= "\0" if (defined($form{$name})); $form{$name} .= $value; } } } sub baseconnect { my $dbname = eval $config->{ast_provisiondb}; $dbh = DBI->connect("dbi:SQLite:dbname=$dbname","",""); $sth = $dbh->table_info('%', '%', 'provision'); my $result = $sth->fetchall_hashref('TABLE_NAME'); if(!defined($result->{'provision'})) { $dbh->do(" CREATE TABLE `provision` ( `macaddress` varchar(12) NOT NULL, `serial` varchar(12) NOT NULL, `secret` varchar(32) NOT NULL, `ext` int(11) NOT NULL, `fullname` varchar(64) NOT NULL, `callerid` varchar(64) NOT NULL, `callgroup` varchar(32) NOT NULL default '1', `pickupgroup` varchar(32) NOT NULL default '1', `context` varchar(32) NOT NULL, `subscribecontext` varchar(32) NOT NULL default 'internal_phones', `ip` varchar(15) NOT NULL) "); } } sub baseclose { if ($dbh->{Active} && defined($sth)) { $sth->finish }; $dbh->disconnect; } sub printhead { print "Content-Type: text/plain; charset=UTF-8;\r\n\r\n"; } sub _read_config_file { my $file = shift or return; my $conf = {}; my $FH = new FileHandle; $FH->open("$file") or ( warn(loc(q[Could not open config file '%1': %2],$file,$!)), return {} ); while(<$FH>) { next if /\s*#/; next unless /\S/; chomp; s/^\s*//; s/\s*$//; my ($param,$val) = split /\s*=\s*/; ### add these to the config hash ### $conf->{ lc $param } = $val; } close $FH; return $conf; } 


The script receives from the request of the MAC device and its serial number, if the bundle is in the database, then the phone is given a config with the login / password and the parameters for connecting to the server. If there is no bundle, then the phone is added to the database, a “service number” is assigned to it, in this case from 1000 to 2000 and the config is given to the phone. If you put a file named additional <MAC> .xml in the " cfg " folder, then this file will be added to the config file. We use this feature to load the panel settings into the phone. For example:
 <Attendant_Console_Call_Pickup_Code ua="na">*8</Attendant_Console_Call_Pickup_Code> <Unit_1_Key_1 ua="na">fnc=sd+cp+blf;sub=2010@172.16.213.1;nme=2010</Unit_1_Key_1> <Unit_1_Key_2 ua="na">fnc=sd+cp+blf;sub=2012@172.16.213.1;nme=2012</Unit_1_Key_2> <Unit_1_Key_3 ua="na">fnc=sd+cp+blf;sub=2014@172.16.213.1;nme=2014</Unit_1_Key_3> <Unit_1_Key_4 ua="na">fnc=sd+cp+blf;sub=2100@172.16.213.1;nme=2100</Unit_1_Key_4> <Unit_1_Key_5 ua="na">fnc=sd+cp+blf;sub=2110@172.16.213.1;nme=2110</Unit_1_Key_5> <Unit_1_Key_6 ua="na">fnc=sd+cp+blf;sub=2111@172.16.213.1;nme=2111</Unit_1_Key_6> <Unit_1_Key_7 ua="na">fnc=sd+cp+blf;sub=2112@172.16.213.1;nme=2112</Unit_1_Key_7> <Unit_1_Key_8 ua="na">fnc=sd+cp+blf;sub=2120@172.16.213.1;nme=2120</Unit_1_Key_8> <Unit_1_Key_9 ua="na">fnc=sd+cp+blf;sub=2121@172.16.213.1;nme=2121</Unit_1_Key_9> 

or to change the port settings for transferring it to another VLAN
 <Enable_VLAN ua="rw">Yes</Enable_VLAN> <Enable_PC_Port_VLAN_Tagging ua="na">Yes</Enable_PC_Port_VLAN_Tagging> <Enable_CDP ua="na">No</Enable_CDP> <Enable_LLDP-MED ua="na">No</Enable_LLDP-MED> <PC_Port_VLAN_ID ua="na">300</PC_Port_VLAN_ID> 


We check whether phones are loaded correctly and whether the config is given correctly.
You can do it by hand, looking at thttpd.log and copying the request into the browser.

If everything is fine with us, then proceed to the most interesting part, namely, the automation and settings of the asterisk. Everyone writes on what is convenient for him, but if we have the opportunity to steer sqlite3 through the console, I decided to write reinforced concrete shell scripts. At first glance, scary, but in the end it turned out very compact. The main part is occupied by error handling for interactive with asterisk. All actions performed in the interactive control mode from asterisk are logged in the system log. In almost all scripts, the stop mode is turned on when an internal error occurs. If there is no path in the script name below, then it lies in / usr / local / etc / asterisk / scripts / .

In the config extensions.conf add
#include "/var/db/asterisk/asterisk_ext_dialplan.conf"

Add to sip.conf config
#include "/var/db/asterisk/asterisk_ext_accounts.conf"

In order to execute commands directly from asterisk, create a group of scripts:
/usr/local/etc/astprov.subr - the main script containing all the procedures for working with the database.
 #!/bin/sh . /usr/local/etc/astprov.conf #set -x getvalue() { result= _request=$1 _ext=$2 selectcmd='select '${_request}' from provision where ext='${_ext}';' result=`$sqlitecmd "$selectcmd"` if [ "$result" ]; then echo $result return 0 else syslog "Error: SQL request cannot be made (request ${_request},extention ${_ext})" return 1 fi } checkvalue() { result= _request=$1 _ext=$2 selectcmd='select '${_request}' from provision where ext='${_ext}';' result=`$sqlitecmd "$selectcmd"` echo $result return 0 } setvalue() { result= _what=$1 _new=$2 _where=$3 _old=$4 updatecmd="update provision set ${_what}='${_new}' where ${_where}='${_old}';" $sqlitecmd "$updatecmd" if [ $? -ne 0 ]; then syslog "Error: SQL update cannot be made (set ${_what}=${_new} where ${_where}=${_old})" return 1 else return 0 fi } get_script_name() { result= result=`echo $0 | rev | cut -d/ -f1 | rev` echo $result } syslog() { result= _message=$@ logger -t $logger_tag `get_script_name`": ${_message}" } sqlitecmdcoln="$sqlite -column $ast_provisiondb" sqlitecmdline="$sqlite -line $ast_provisiondb" sqlitecmd="$sqlite $ast_provisiondb" 


showall.sh dump database for diagnostics
 #!/bin/sh set -e . /usr/local/etc/astprov.subr $sqlitecmd 'select * from provision' 

rebuildlist.sh script update dynamic configuration for asterisk.
 #!/bin/sh set -e . /usr/local/etc/astprov.subr if [ -f $ast_ext_accounts -a ! -w $ast_ext_accounts ]; then syslog 'Cannot write to astprov extension database' exit 1 fi if [ -f $ast_ext_accounts ]; then mv $ast_ext_accounts $ast_ext_accounts.backup fi if [ -f $ast_ext_dialplan -a ! -w $ast_ext_dialplan ]; then syslog 'Cannot write to astprov extension dialplan' exit 1 fi if [ -f $ast_ext_dialplan ]; then mv $ast_ext_dialplan $ast_ext_dialplan.backup fi echo '[dynamic_internal_numbers]' >> $ast_ext_dialplan selectcmd='select ext from provision order by ext;' for ext in `$sqlitecmdcoln "$selectcmd"`; do echo 'exten => '$ext',1,dumpchan()' >> $ast_ext_dialplan echo 'same => n,Dial(SIP/'$ext',60,Tt)' >> $ast_ext_dialplan echo 'same => hint,SIP/'$ext >> $ast_ext_dialplan echo 'same => n,Hangup()' >> $ast_ext_dialplan echo ''>> $ast_ext_dialplan selectcmd='select callerid,fullname,macaddress,secret,context,callgroup,pickupgroup,subscribecontext,ip from provision where ext='$ext';' echo '['$ext'](all)' >> $ast_ext_accounts $sqlitecmdline "$selectcmd" | sed -E 's/ = /=/g' | while IFS= read -r line; do echo $line >> $ast_ext_accounts done echo >> $ast_ext_accounts done syslog 'Asterisk dynamic list succesfully updated' 

changenumber.sh script to replace the phone number to another.
 #!/bin/sh #set -x . /usr/local/etc/astprov.subr oldname=`getvalue 'ext' $1` if [ "$?" -ne "0" ]; then echo -n GETERR exit fi numberexist=`checkvalue 'ext' $2` if [ -z "$numberexist" ]; then setvalue 'ext' $2 'ext' $1 if [ "$?" -ne "0" ]; then echo -n SETERR exit fi setvalue 'callerid' "$oldname <$2>" 'ext' $2 if [ "$?" -ne "0" ]; then echo -n SETERR exit fi syslog "Number $1 renamed to $2" echo -n OK else syslog "Number $1 cannot be renamed to $2. Number $2 already exist" echo -n NUMEXIST exit # return 1 fi 

togglenumbers.sh is a script for swapping phones. let's say you need to swap ext 1023 from 2035. It is very convenient when employees change hands. Phones remain in place, and numbers are swapped.
 #!/bin/sh #set -x . /usr/local/etc/astprov.subr macaddress_from=`getvalue 'macaddress' $1` if [ "$?" -ne "0" ]; then echo -n GETERR ; exit ; fi macaddress_to=`getvalue 'macaddress' $2` if [ "$?" -ne "0" ]; then echo -n GETERR; exit; fi secret_from=`getvalue 'secret' $1` if [ "$?" -ne "0" ]; then echo -n GETERR; exit; fi secret_to=`getvalue 'secret' $2` if [ "$?" -ne "0" ]; then echo -n GETERR; exit; fi serial_from=`getvalue 'serial' $1` if [ "$?" -ne "0" ]; then echo -n GETERR; exit; fi serial_to=`getvalue 'serial' $2` if [ "$?" -ne "0" ]; then echo -n GETERR; exit; fi setvalue macaddress $macaddress_from'_'$macaddress_to macaddress $macaddress_from if [ "$?" -ne "0" ]; then echo -n SETERR; exit; fi setvalue macaddress $macaddress_to'_'$macaddress_from macaddress $macaddress_to if [ "$?" -ne "0" ]; then echo -n SETERR; exit; fi setvalue secret $secret_to macaddress $macaddress_from'_'$macaddress_to if [ "$?" -ne "0" ]; then echo -n SETERR; exit; fi setvalue secret $secret_from macaddress $macaddress_to'_'$macaddress_from if [ "$?" -ne "0" ]; then echo -n SETERR; exit; fi setvalue serial $serial_to macaddress $macaddress_from'_'$macaddress_to if [ "$?" -ne "0" ]; then echo -n SETERR; exit; fi setvalue serial $serial_from macaddress $macaddress_to'_'$macaddress_from if [ "$?" -ne "0" ]; then echo -n SETERR; exit; fi setvalue macaddress $macaddress_to macaddress $macaddress_from'_'$macaddress_to if [ "$?" -ne "0" ]; then echo -n SETERR; exit; fi setvalue macaddress $macaddress_from macaddress $macaddress_to'_'$macaddress_from if [ "$?" -ne "0" ]; then echo -n SETERR; exit; fi syslog "Phones $1($serial_from:$macaddress_from) and $2($serial_to:$macaddress_to) are reversed" echo -n OK 


Scripts are laid out. In principle, you can use scripts and hands through
su -m asterisk , but the best option is to integrate this feature into asterisk.
Since Since all new unregistered phones fall into the guest context "[unreg]", then you need to create a description of this context in asterisk.

 ;goto service menu exten => 3999,1,Goto(unreg,_X.,1) [unreg] exten => _X.,1,Answer() ;same => n,Authenticate(040478) same => n,NoOp(Entering Service Menu) same => n,Playback(ivr/prov-welcome-to-service-menu) same => n(menu),Background(ivr/change-number&ivr/swap-numbers&ivr/rebuild-list&ivr/reboot-phone&ivr/reload-server) same => n,Read(CHOICE,,1,,1,10) ; 1 - Change Number ; 2 - Swap Numbers ; 3 - Rebuild extensions list ; 4 - Reload phone ; 5 - Reload server ; Rename Number exten => 1,1,Read(RENAMETO,ivr/enter-dest-number,4,,1,5) same => n,GotoIf($["${RENAMETO}" = ""]?menu) same => n,Playback(ivr/entered-number) same => n,SayDigits(${RENAMETO}) same => n,Read(APPROVE,ivr/press-1-for-accept,1,,1,5) same => n,GotoIf($["${APPROVE}" != "1"]?menu) same => n,Set(CHGNM=${SHELL(/usr/local/etc/asterisk/scripts/changenumber.sh ${CALLERID(num)} ${RENAMETO})}) same => n,NoOp(${CHGNM}) same => n,GotoIf($["${CHGNM}" = "GETERR"]?geterr) same => n,GotoIf($["${CHGNM}" = "SETERR"]?seterr) same => n,GotoIf($["${CHGNM}" = "NUMEXIST"]?numexist) same => n,GotoIf($["${CHGNM}" = "OK"]?ok) same => n(ok),Playback(ivr/prov-saved&privacy-thankyou) same => n,System(/usr/local/etc/asterisk/scripts/rebuildlist.sh) same => n,GotoIF($["${SYSTEMSTATUS}" != "SUCCESS"]?error) same => n,Playback(ivr/rebuild-ok) same => n,System(/usr/local/sbin/asterisk -rx "sip notify cisco-check-cfg ${CALLERID(num)}") same => n,GotoIF($["${SYSTEMSTATUS}" != "SUCCESS"]?error) same => n,Playback(ivr/send-phone-reboot-ok) same => n,System(/usr/local/sbin/asterisk -rx "reload") same => n,GotoIF($["${SYSTEMSTATUS}" != "SUCCESS"]?error) same => n,Playback(ivr/server-reload-ok) same => n,Hangup() same => n(geterr),Playback(ivr/script-get-error) same => n,Hangup() same => n(seterr),Playback(ivr/script-set-error) same => n,Hangup() same => n(numexist),Playback(ivr/prov-exist) same => n,Hangup() same => n(menu),Goto(_X.,menu) same => n,Hangup() same => n(error),Playback(ivr/script-error) same => n,Hangup() ; Reverse numbers exten => 2,1,Read(SWAPTO,ivr/enter-dest-number,4,,1,5) same => n,GotoIf($["${SWAPTO}" = ""]?menu) same => n,Playback(ivr/entered-number) same => n,SayDigits(${SWAPTO}) same => n,Read(APPROVE,ivr/press-1-for-accept,1,,1,5) same => n,GotoIf($["${APPROVE}" != "1"]?menu) same => n,Set(CHGNM=${SHELL(/usr/local/etc/asterisk/scripts/togglenumbers.sh ${CALLERID(num)} ${SWAPTO})}) same => n,NoOp(${CHGNM}) same => n,GotoIf($["${CHGNM}" = "GETERR"]?geterr) same => n,GotoIf($["${CHGNM}" = "SETERR"]?seterr) same => n,GotoIf($["${CHGNM}" = "OK"]?ok) same => n(ok),Playback(ivr/prov-saved&privacy-thankyou) same => n,System(/usr/local/etc/asterisk/scripts/rebuildlist.sh) same => n,GotoIF($["${SYSTEMSTATUS}" != "SUCCESS"]?error) same => n,Playback(ivr/rebuild-ok) same => n,System(/usr/local/sbin/asterisk -rx "sip notify cisco-check-cfg ${CALLERID(num)} ${SWAPTO}") same => n,GotoIF($["${SYSTEMSTATUS}" != "SUCCESS"]?error) same => n,Playback(ivr/send-phone-reboot-ok) same => n,System(/usr/local/sbin/asterisk -rx "reload") same => n,GotoIF($["${SYSTEMSTATUS}" != "SUCCESS"]?error) same => n,Playback(ivr/server-reload-ok) same => n,Hangup() same => n(geterr),Playback(ivr/script-get-error) same => n,Hangup() same => n(seterr),Playback(ivr/script-set-error) same => n,Hangup() same => n(error),Playback(ivr/script-error) same => n,Hangup() same => n(menu),Goto(_X.,menu) same => n,Hangup() exten => 3,1,System(/usr/local/etc/asterisk/scripts/rebuildlist.sh) same => n,NoOp(${SYSTEMSTATUS}) same => n,GotoIF($["${SYSTEMSTATUS}" != "SUCCESS"]?error) same => n,Playback(ivr/rebuild-ok) same => n,System(/usr/local/sbin/asterisk -rx "reload") same => n,GotoIF($["${SYSTEMSTATUS}" != "SUCCESS"]?error) same => n,Playback(ivr/server-reload-ok) same => n,Goto(_X.,menu) same => n(error),Playback(ivr/script-error) same => n,Goto(_X.,menu) exten => 4,1,System(/usr/local/sbin/asterisk -rx "sip notify cisco-check-cfg ${CALLERID(num)}") same => n,GotoIF($["${SYSTEMSTATUS}" != "SUCCESS"]?error) same => n,Playback(ivr/send-phone-reboot-ok) same => n,System(/usr/local/sbin/asterisk -rx "reload") same => n,GotoIF($["${SYSTEMSTATUS}" != "SUCCESS"]?error) same => n,Playback(ivr/server-reload-ok) same => n,Hangup() same => n(error),Playback(ivr/script-error) same => n,Goto(_X.,menu) same => n,Hangup() exten => 5,1,System(/usr/local/sbin/asterisk -rx "reload") same => n,GotoIF($["${SYSTEMSTATUS}" != "SUCCESS"]?error) same => n,Playback(ivr/server-reload-ok) same => n,Goto(_X.,menu) same => n(error),Playback(ivr/script-error) same => n,Goto(_X.,menu) same => n,Hangup() 


Actually what does this set of letters and numbers.
1. Change the number.
You can change the existing registered work number to another. Requested phone number for which you want to change. After changing the number, the list of dynamic configuration is recreated, the server is reload and the phone automatically restarts.
2. Swap phones
The call is made from the phone to which you want to change with another phone. When replacing phones within the database, the information given by the configuration script changes. The phone’s login password does not move, only the information about callerid and ext changes.
After changing the numbers, the list of dynamic configuration is re-created, the server is reloaded and the phones are automatically reloaded.
3. Recreate the list of phones.
Recreates dynamic configuration files. It is convenient to use when many unregistered phones in the autoconfiguration stand and wait for their turn in service, but cannot register on the server. It is executed from any phone. At runtime, the list of dynamic configuration is re-created, a reload is made on the server.
4. Reboot the machine
Reboots the device from which the call was made. Automatically makes reload on the server.
5. Updating the config on the server.
Automatically makes reload on the server.

In general, everything. With the correct transfer of scripts to the server, raising telephony becomes an easy task. Initially, you can simply create a dialplan and pour it into the database. Further actions are completely transparent and understandable to anyone, not even familiar with the administration of asterisk. There will be questions - write. The scheme is quite portable to any other database that you like more.

Aborche 2013

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


All Articles