<- ...Back>   Continue... ->

Pattern Match

 

You may have noticed that in the 0900-allow context in the  example extension the following appeared

 

 

 

exten => _0900X.,1,Dial(ZAP/g1/${EXTEN})

 

 

The number part of the extension starts with and underscore (_) and there is a capital X and full stop after the actual number 0900. This is Asterisk's pattern matching feature. Imagine if we had to type in all the possible 0900 numbers we wanted to allow. We would end up with a very large extensions.conf and of course if you found another 0900 number you wanted to call you'd have to edit the file.

 

 

The pattern matching for the above example says

 

 

I match any number that starts with 0900 and has at least one digit after the 0900

 

 

Pattern matching allows us to use characters and number parameters to select the numbers this extension represents. Pattern matching always starts with an underscore, if there is no underscore Asterisk with only dial the number if there is an exact match   dialed by a user.

 

 

 


For pattern matching you can use the following characters and number groups:

 

X  Any digit from 0 to 9

Z Any digit from 1 to 9

N Any digit from 2 to 9

.  Wildcard, matches anything

 

You can also specify the actual digits that may appear in a position in the extension number using

 

[123-5,9] Allow digit 1,2,3,4,5 and 9.

 

 

 

 

 

 

Pattern matching characters can be repeated any number of times, but unless the character is the full stop (.) then there must be a digit present in the number dialed, ie

 

 

 

exten => _1234XXXZ,1,Dial(ZAP/g1/${EXTEN})

 

 

will only match an 8 digit dialed number, where the first 4 digits are 1234 followed by 3 more digits, followed by any digit in the range 1 to 9.

 


Let's Get It Working

 

So now that we have a phone defined in sip.conf, a configured softphone we need to create the context in extensions.conf.

 

If you saved and quit extensions.conf earlier on then you'll need to start vi and edit it once more, if you are still editing it then you get a gold star (That's possibly two you have now, I will be counting how many you have at the end).

 

 

So if you exited extensions.conf for some reason, type these lines in the console, hitting return at the end of each line.

 

 

cd /etc/asterisk

vi extensions.conf

 

 

 

You may notice that with changed directory again, we had to do this because you haven't been doing what you're told, so we need to be sure you are in the right place. Remember to hit the insert key before typing to make sure you're in insert mode.

 

 

Right. Now we get to add the context for your Xlite phone, underneath the globals section and its entries, hit return a couple more times to space things out and add

 

 

[mytest]

exten => 1367,1,Dial(SIP/1000)

 

exten => 2890,1,Wait(2)

exten => 2890,2,Answer

exten => 2890,3,Playback(demo-echotest)

exten => 2890,4,Echo()

 

exten => 2468,1,Dial(${XLITE})

 

 

 

Now save the file, (hit escape then type a colon (:), a lower case W and a lower case Q). This will save and exit.

 

What did we just do? Well  the [mytest] part is a context, this matches the context= line in your sip.conf (other than the square brackets), where you defined your phone (at the bottom of the file). That means that your Xlite phone will see the extensions in this context, in our case there are three extensions defined – 1367, 2890 and 2468.

We define an extension using the word exten followed by the => characters. The extension number follows this which, for our first exten => line, is 1367. Did you notice that the extension number seems to bear no resemblance to the information in sip.conf? We added this to sip.conf

 

 

 

[1000]

username=1000

secret=abc123

context=mytest

host=dynamic

 

 

 

But there's no mention of the number 1367. Relax, this is deliberate. I was explaining how extensions.conf worked to someone and they thought that the extension had to be the same as the phone definition i.e. The extension had to be 1000. This is not the case, so I'm telling you upfront so you don't ask the same question.  Next comes the priority. Priorities start at one  (1) and go up, they should be sequential (except in some special circumstances which we'll get to later). For our extension 1367 there's only one priority, but for extension 2890 there are four (4).

 

 

So what on earth are these priorities? It's simple, Asterisk will execute the command for each priority of an extension in turn, in the order, 1,2,3 etc.. i.e. It will count up. For extension 2890 it will execute the line Wait(2) then then line Answer etc.

 

 

Getting back to our first extension, 1367, the relationship between the entry in sip.conf and the extension is in the Dial command. The command executed is Dial, and it takes parameters, in this case the parameter we supply is

 

 

 

SIP/1000

 

 

What does that mean? Well, this is a SIP device, so we're telling Asterisk to use SIP as the method, or technology, to contact it. The 1000 is the name of the device, defined in sip.conf – it's the bit that appears between the square brackets, ie

 

 

 

 

[1000]

 

 

 

So, in total, this extension line says,

 

When someone calls extension 1367, the first thing I do is call the device 1000 using the SIP protocol.

 

 

We'll deal with commands a little later, for the moment look at extension 2890 and say out loud (say it loud, say it proud) what you think is going to happen and in what order.

 

 

Right, done that? Really? Did anyone look at you quizzically? Ignore them.

 

 

The last extension we added, 2468, demonstrates the use of the variable we defined in the [globals] section of our extensions.conf, simply put the ${XLITE} is replaced with the value for XLITE= in globals, in our case SIP/1000.

 

Now we actually get to do the fun part. Start Asterisk on your server using the command

 

 

 

asterisk -vvvvvvvvc

 

 

 

The number of v's doesn't really matter, the more there are the more verbose asterisk will be in its display, but don't omit the c from the end. When you do this you will see a great deal of information scroll past quite quickly, don't worry about this, just wait until you get a prompt like

 

 

 

*CLI>

 

 

Now start up Xlite. After a few seconds you should see a message on the console along the lines of

 

 

 

    -- Registered SIP '1000' at XXX.XXX.XXX.XXX port 5060 expires 1800

    -- Saved useragent "X-Lite release 1103m" for peer 1000

 

 

 

Where XXX.XXX.XXX.XXX is the IP address of your PC.  Xlite should also be lying to you, telling you your number is 1000. That's generally because you would make the extension match the entry in sip.conf but I deliberately made it different to prove a point. What Xlite really means is that to dial you the Asterisk box will talk to the address sip:1000@ip-of-your-pc. Ok, you've read lots, now let's try something mildly interesting. From your Xlite phone, dial 2890 and hit the green button. The phone should dial the Asterisk server and play you a message about an echo test, echo test, echo test. Sorry about that. Everything you say should be repeated back to you with almost (depends on the machine) no delay at all. Talk to yourself for a while and congratulate yourself also, you've got this far without too much trouble. When you're finished, simply hang up Xlite (the red button)

 

 


Priorities are normally sequential, however some applications will jump to another priority if something happens. For example

 

The Dial command will leap to the current priority + 101 if the dialed number is busy, and priority+101 exists. This is how we get the dial command to jump  to voice mail if we're on the phone.

 

exten => 100,1,Dial(SIP/100)

exten => 100,2,VoiceMail(u100)

exten => 100,3,hangup

 

exten => 100,102,VoiceMail(b100)

exten => 100,103,hangup

 

 


Starting, Stopping and Reloading Asterisk

 

 

There are a number of ways to start Asterisk, the most common is to start it as a daemon by using the command

 

 

 

asterisk

 

 

 

 

 

 

This will start Asterisk in the background and return you to the shell prompt and has the advantage that if your session disconnects from the server for any reason, Asterisk will continue to run. If you try to start Asterisk as a daemon and it is already running you will see an error message along the lines of

 

 

 

ERROR[8192]: File asterisk.c, Line 1249 (main): Asterisk already running on /var/run/asterisk.ctl. Use 'asterisk -r' to connect.

 

 

If you see this message you can simply connect to the running daemon by using

 

 

 

asterisk -r

 

 

 

 

 

 

If you want to stop Asterisk completely, connect to the Asterisk CLI and use

 

 

 

stop now

 

 

 

 

 

 

 

This will stop Asterisk IMMEDIATELY and will KILL any calls in progress. Use it with care.  If you have connected to the Asterisk CLI using asterisk -r and want to get back to the shell prompt without stopping the running Asterisk use

 

 

 

 

exit

 

 

 

 

 

You can reconnect to the running Asterisk daemon at any time by using the asterisk -r command, you can also have multiple connections to the running daemon by simply issuing another asterisk -r from another console/putty connection.

 

 

Up until now we have done all our configuration editing when Asterisk is in a stopped state. This is all well and good while you are getting started, but when you run a live system you don;t want to stop Asterisk and then start it again just to load new configuration options. Thankfully you don't have to do that, you can simply use the command

 

 

 

reload

 

 

 

 

 

 

While connected to the Asterisk CLI. This will cause Asterisk to reload most of it's configuration files. There are exceptions to this, specifically the zaptel related configuration files. To get Asterisk to acknowledge changes for the zaptel configuration you must restart Asterisk, you can do this using the command

 

 

 

restart now

 

 

 

 

 

 

Again, this will stop Asterisk IMMEDIATELY and will KILL any calls in progress, although Asterisk will restart itself almost immediately.

 

 


Sometimes you don't want to hang around while all the calls complete to restart Asterisk. We have a number of options we can use to get Asterisk to restart;

 

restart now                         Restart Asterisk immediately

 

restart when convenient     Restart Asterisk when there are no calls

 

restart gracefully               Asterisk will stop accepting calls and

                                         will restart when there are no calls.

 

 

 

 

 

You can also apply the same logic to the STOP command.


<- ...Back>   Continue... ->