How to Replace exten with same in Asterisk

Replace "exten =>" with " same =>"

Today I'm going to give you a simple perl one-liner that will let you convert an entire extensions.conf file with a simple command.

Disclaimer!
This code comes with no warranties, implied or expressed in any form. I cannot guarantee this will work in all situations. If you have some strange dialplan or errors in your code, or if you fail to make a backup, or even if you follow this page to the letter, you could still break your dialplan. Always always always make backups before doing anything, especially on a production system! If you break it, that's not my fault.

This code is here to help, because I've seen other people ask for this type of script, so I'm providing it here. Always test it thoroughly on your system to ensure that it works for you, and doesn't alter the dialplan in any unexpected ways. That said, if you do find any issue with it, please let me know and I'll be happy to improve this page.

Description
This will replace all instances of "exten => xxx,n,..." with " same => n,..."

You could easily package this into a perl script, however I'm going to give you the command line version, and we'll set it up as an alias.

Without further ado, here is the script: (Triple click to select all)

perl -pe 's/exten => ([][0-9a-zA-Z\/.*_+-]+),([^1][()a-zA-Z0-9+-]?),([a-zA-Z]+.*)/ same => \2,\3/g; s/exten => ([][0-9a-zA-Z\/.*_+-]+),([n0-9][()a-zA-Z0-9+-]+),([a-zA-Z]+.*)/ same => \2,\3/g'

Now, to turn this into an alias, simply edit ~/.bash_profile or ~/.bashrc and add the following line:
alias ext2same="{insert script}"

This becomes:

alias ext2same="perl -pe 's/exten => ([][0-9a-zA-Z\/.*_+-]+),([^1][()a-zA-Z0-9+-]?),([a-zA-Z]+.*)/ same => \2,\3/g; s/exten => ([][0-9a-zA-Z\/.*_+-]+),([n0-9][()a-zA-Z0-9+-]+),([a-zA-Z]+.*)/ same => \2,\3/g'"

After saving, run
$ source ~/.bash_profile
(or ~/.bashrc depending which file you modified)

Now you can use the script, and it should be available every time you login. (Note: screen doesn't always run a full login in each window, so you may need to source the files inside a screen session)

To use this, run:

$ ext2same /etc/asterisk/extensions.conf > /etc/asterisk/extensions.conf.new
DISCLAIMER! DO NOT redirect the output to the same file, the shell will automatically truncate the destination file FIRST, then read it, which is not what you want.

If you want to use this "in-place" change perl -pe into perl -pi.bk -e. This will automatically change the actual file, but it will also create a backup with a .bk extension.

alias ext2sameIP="perl -pi.bk -e 's/exten => ([][0-9a-zA-Z\/.*_+-]+),([^1][()a-zA-Z0-9+-]?),([a-zA-Z]+.*)/ same => \2,\3/g; s/exten => ([][0-9a-zA-Z\/.*_+-]+),([n0-9][()a-zA-Z0-9+-]+),([a-zA-Z]+.*)/ same => \2,\3/g'"

Thus:
$ ext2sameIP /etc/asterisk/extensions.conf

Will automatically change the actual extensions.conf, but will also create a backup called /etc/asterisk/extensions.conf.bk

(If you don't want to make a backup, remove the ".bk" after the -i option, but don't come to me if you accidentally break your script.)

Revision History:
2015-04-19-2: Now using sandpaper to disable 'smart-quotes' in the code blocks! (Allows direct copy/paste of code into shell/vi/emacs)
2015-04-19-1: First several drafts

Tagged with: , , ,
Posted in Asterisk, FreePBX, Tips n Tricks

Leave a Reply

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

*