Upgrade 3CX to v18 and get it hosted free!

Asterisk tips renumber extens

Author image

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;
        }
}


Article Reviews

Write a Review

Your email address will not be published. Required fields are marked *

Required Field. Minimum 5 characters.

Required Field. Minimum 5 characters, maximum 50.

Required field.There is an error with this field.

Required Field.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

There are no reviews for this article. Be the first one to write a review.
Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.