Asterisk tips renumber extens

Adding a new rule in the middle of extensions.conf can be painful if you need to renumber the priorites of things below it.

I wrote a little perl script to renumbers the priorities.

It looks at rules in the Extension Patterns in sets. a set consists a number of lines of the form
extern => extension,priority,Command(parameters)

Anything other then a comment line or another exten line will end the set . If the extension changes that also ends the set.
The script leaves the priority of the first rule in the set alone, and increments the priority for each of the remaining rules in the set.

so:

exten => 202,1,Dial(${PHONE001},30,rt)
exten => 202,2,Voicemail(u202)
exten => 202,3,Hangup
exten => 202,102,Voicemail(u202)
exten => 202,103,Hangup

becomes:

exten => 202,1,Dial(${PHONE001},30,rt)
exten => 202,2,Voicemail(u202)
exten => 202,3,Hangup
exten => 202,4,Voicemail(u202)
exten => 202,5,Hangup

So here's the script:

#! /usr/bin/perl

my $state = 1; # 1 for not in a run; 2 for in a run
my $next;

while ( <> )
{
        # if this line is an exten rule
        if ( /^(\s*exten\s+=>)(\s+\w+,)(\d+)(,.*)$/ )
        {
                # if we are in a set of rules
                # just give this rule the next number
                if ( $state == 2 && $prefix eq $2 )
                {
                        print "$1$2$next$4\n";
                        $next++;
                }
                # If we aren't in a set or
                # we were in a set but the
                # prefix changed (if we are in a set and the
                # prefix matches, we'd hit the rule above)
                # start a new set
                elsif ( $state == 1 || $state == 2 )
                {
                        # we are in a new state
                        $prefix = $2;
                        $next = $3;
                        $next++;
                        print;
                        $state = 2;
                }
        }
        elsif ( /^\s*;/ )
        {
                # just a comment don't change state
                print;
        }
        else
        {
                # if we see anything else, assume the end of the set
                # was reached, and put us back into the "not in
                # a run" state.
                print;
                $state = 1;
        }
}

Adding a new rule in the middle of extensions.conf can be painful if you need to renumber the priorites of things below it.

I wrote a little perl script to renumbers the priorities.

It looks at rules in the Extension Patterns in sets. a set consists a number of lines of the form
extern => extension,priority,Command(parameters)

Anything other then a comment line or another exten line will end the set . If the extension changes that also ends the set.
The script leaves the priority of the first rule in the set alone, and increments the priority for each of the remaining rules in the set.

so:

exten => 202,1,Dial(${PHONE001},30,rt)
exten => 202,2,Voicemail(u202)
exten => 202,3,Hangup
exten => 202,102,Voicemail(u202)
exten => 202,103,Hangup

becomes:

exten => 202,1,Dial(${PHONE001},30,rt)
exten => 202,2,Voicemail(u202)
exten => 202,3,Hangup
exten => 202,4,Voicemail(u202)
exten => 202,5,Hangup

So here's the script:

#! /usr/bin/perl

my $state = 1; # 1 for not in a run; 2 for in a run
my $next;

while ( <> )
{
        # if this line is an exten rule
        if ( /^(\s*exten\s+=>)(\s+\w+,)(\d+)(,.*)$/ )
        {
                # if we are in a set of rules
                # just give this rule the next number
                if ( $state == 2 && $prefix eq $2 )
                {
                        print "$1$2$next$4\n";
                        $next++;
                }
                # If we aren't in a set or
                # we were in a set but the
                # prefix changed (if we are in a set and the
                # prefix matches, we'd hit the rule above)
                # start a new set
                elsif ( $state == 1 || $state == 2 )
                {
                        # we are in a new state
                        $prefix = $2;
                        $next = $3;
                        $next++;
                        print;
                        $state = 2;
                }
        }
        elsif ( /^\s*;/ )
        {
                # just a comment don't change state
                print;
        }
        else
        {
                # if we see anything else, assume the end of the set
                # was reached, and put us back into the "not in
                # a run" state.
                print;
                $state = 1;
        }
}

Created by: gray, Last modification: Thu 04 of Nov, 2010 (05:46 UTC) by admin
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+