Rapid Phone Network Deployment on Asterisk + Cisco

    It so happened that in a short time there was a need 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 telephone exchange, and the telephone exchange 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. Equipment delivery was scheduled for the day preceding the day of relocation, which meant that there would be very little time to deploy telephony.

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

    So the existing configuration:
    • 60 users with their computers.
    • 60 Cisco SPA 502G Phones
    • 4 Cisco SPA500S Panels
    • 5 Siemens C610A bases with a pair of tubes for each.
    • One server on FreeBSD.
    • A pair of switches Cisco SF300-24P


    After searching and reading a lot of documents related to autoprovision on the network, we decided to take advantage of the asterisk ability to perform automatic device configuration on our own. Everything would be fine, but as it turned out, auto-tuning through the sql database in asterisk does not allow monitoring the status of the extension on doppanels. Those. hint cannot be configured (at least in August 2012 it was not supported). Having a pair of phones available, we decided to write our autoprovision.

    We started by setting up the server and switches. We created 2 VLANs with numbers 204 and 214. The first for LAN, the second for IP telephony. Because phones have a built-in configurable switch, this was the best solution from our point of view.
    Set 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 dhcpd.conf settings
    # 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)));
      }
    }
    

    Excellent. Addresses are heard, phones are booted, it's time to gash the base config.
    Raise thttpd and put it in the root of XMLDefault.cnf.xml
    7654321Nophones.mydomain.local172.16.214.1172.16.214.1172.16.214.10172.16.214.1YesYesNoNo204http://172.16.214.1/XMLDefault.cnf.xmlhttp://172.16.214.1/cfg/cfg.cgi?SN=$SN&MAC=$MANoYesNoNoNoNoNoNoYes24hrday/monthCompanyGMT+04:00( $SWVER ne 7.5.2b )? http://172.16.214.1/sw/spa50x-30x-7-5-2b.bin

    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, attached to the serial number of the device and its poppy address. After the config is fully initialized, the phone will transfer the computer port to 204 VLAN, providing it with a local network.
    Let's move on to how 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"
    


    Using the sqlite3 database specified in the configuration file, create the following structure:
    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 workplaces 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 in the database and enable the quick connection of new phones.

    Now a little witchcraft.
    Asterisk runs under asterisk: asterisk, thttpd runs 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 your phone settings.
    We create the 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,";
        close IN;
    }
    my $hostname = "office-".$dbdata->{ext};
    print << "[end]";
    
    $hostnameYesYes172.16.214.1$dbdata->{fullname}$dbdata->{ext}$dbdata->{secret}(7[0-9][0-9]xxxxS0|*8|*xx|xxxx|0xxxxxxxxxxxx.)$dbdata->{ext}$dbdata->{fullname}AsteriskMy Companyhttp://172.16.213.1/directory.cgi
    $additional
    
    [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 MAC request of the device and its serial number, if the link is in the database, then the phone is given a config with login / password and parameters for connecting to the server. If there is no connection, the phone is added to the database, it is assigned a “service number”, in this case from 1000 to 2000, and the phone is given a config. If in the folder " cfg " put a file named additional.xml, then the phone will add this file to the config. We use this feature to download panel settings to your phone. For instance:
    *8fnc=sd+cp+blf;sub=2010@172.16.213.1;nme=2010fnc=sd+cp+blf;sub=2012@172.16.213.1;nme=2012fnc=sd+cp+blf;sub=2014@172.16.213.1;nme=2014fnc=sd+cp+blf;sub=2100@172.16.213.1;nme=2100fnc=sd+cp+blf;sub=2110@172.16.213.1;nme=2110fnc=sd+cp+blf;sub=2111@172.16.213.1;nme=2111fnc=sd+cp+blf;sub=2112@172.16.213.1;nme=2112fnc=sd+cp+blf;sub=2120@172.16.213.1;nme=2120fnc=sd+cp+blf;sub=2121@172.16.213.1;nme=2121

    or to change the port settings for transferring it to another VLAN
    YesYesNoNo300


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

    If everything is fine with us, then we proceed to the most interesting part, namely the automation and asterisk settings. Everyone writes on what is convenient for him, but since we have the opportunity to steer sqlite3 through the console, I decided to write reinforced concrete scripts on the shell. At first glance scary, but in the end it turned out very compact. The main part is occupied by error handling for interacting 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 enabled 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 extensions.conf config, add
    #include "/var/db/asterisk/asterisk_ext_dialplan.conf"

    In the sip.conf config add
    #include "/var/db/asterisk/asterisk_ext_accounts.conf"

    In order to execute the commands directly from asterisk, create a group 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 base for diagnostics
    #!/bin/sh
    set -e
    . /usr/local/etc/astprov.subr
    $sqlitecmd 'select * from provision'
    

    rebuildlist.sh dynamic configuration update script 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 a phone number with 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 script to swap phones. let's say ext 1023 needs to be swapped from 2035. It is very convenient when employees are transplanted. Phones remain in place, and numbers change places.
    #!/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 posted. In principle, you can use scripts with your hands through
    su -m asterisk , but the best option is to integrate this "feature" into asterisk.
    Because If all new unregistered phones fall into the guest context "[unreg]", 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 makes this set of letters and numbers.
    1. Change of number.
    You can change the existing registered work number to another. The phone number you want to change is requested. After changing the number, the dynamic configuration list is recreated, reload is done on the server, and the phone automatically reboots.
    2. Swap phones. The
    call is made from the phone to which you want to change with another phone. When replacing phones inside the database, the information given by the configuration script changes. Login phone password does not change, only information on callerid and ext changes.
    After changing the numbers, the dynamic configuration list is recreated, reload is done on the server, and the phones automatically reboot.
    3. Re-create the phone list.
    Dynamic configuration files are recreated. It is convenient to use when a lot of unregistered phones in auto configuration are standing and waiting for their turn in maintenance, but can not register on the server. It is executed from any phone. At runtime, a list of dynamic configuration is recreated, reload is done on the server.
    4. Reboot the device
    Reboots the device from which the call was made. Automatically does reload on the server.
    5. Updating the config on the server.
    Automatically does reload on the server.

    That's all. With the correct transfer of scripts to the server, raising telephony becomes an easy task. Initially, you can simply create a dialplan and add it to the database. Further actions are completely transparent and understandable to any person who is 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

    Also popular now: