📜 ⬆️ ⬇️

Asterisk. Start

Writing this article led me to a virtually complete lack of how-to on setting up Asterisk, with examples clear to the beginner. On the network, you can find a lot of information on setting up IVR, on setting up authorization of SIP users via LDAP, manuals on creating HA clusters with Asterisks inside, etc., but there is not a single article on how to start it from scratch, and even from examples. Almost everywhere it is proposed to immediately use all the possibilities offered by Asterisk, and if you remove some of the functionality offered in the manual, in most cases this will result in an inoperable design. This article is the result of walking a rake ... reading manuals. If you are in the same situation as I was a couple of years ago - welcome under cat.

And so, the situation: you just found out about Asterisk and its capabilities, and you wanted to use it at home, or your management wanted IP telephony to the office.

The first thing we need is a server with an installed OS. For a number of reasons, I chose CentOS 6 for my servers. All the examples will be linked to this OS, since it is on it that the most stable servers are obtained. However, I am in no way trying to limit you to a choice. Aster will normally start under Debian, Arch, and even on FreeBSD. I note: it is desirable not to use hypervisors at the initial stage, since you can get a “metallic” voice, or its complete absence. About timers and interruptions will tell in the next article, as their description and configuration is beyond the scope of this article. In the event that you do not have a free server and you cannot do without a virtual machine, be sure to install the x86 guest.

After you have installed the OS on the server, you can proceed with the installation of Asterisk. Add the Didgium repositories:
  rpm -Uvh http://packages.asterisk.org/centos/6/current/i386/RPMS/asterisknow-version-3.0.1-2_centos6.noarch.rpm
 yum update 

Install dnsmasq:
  yum install dnsmasq 

Install Asterisk:
  yum install asterisk asterisk-configs --enablerepo = asterisk-12 

Important note: the number at the end of the line indicates the major version of Asterisk. The example will install the most recent version of the 12th branch, available in the repositories.
')
After installation, restart the server and go to the Asterisk shell:
  asterisk -rv 

Pay attention to the keys that run the shell. The number of keys "v" affects the amount of information displayed when you call, their number varies from 1 to 14.

If the installation was successful and Asterisk was launched, you will see this invitation:
  asterisk * CLI> 

Congratulations, Asterisk is established and ready to go. But for now, we don’t need a shell, so we’re writing exit.

In order to save your time and make the material understandable, we will look at the theory with a concrete example, and also analyze the main terms that you will have to operate on when setting up, when communicating with the support of the operator and your colleagues. For example, we will get two internal users with numbers 100 and 101, and two telephony operators. One of the operators will provide us with city numbers through a trunk with registration, the other will provide access to long-distance lines using a trunk without registration.

The first thing you need to do is create peers in the /etc/asterisk/sip.conf file. Open it:
  nano /etc/asterisk/sip.conf 

And immediately go to the end of the file. Insert the following text:
  [internal] (!)
 type = friend
 insecure = invite, port
 context = office
 fromdomain = <domain name or IP>
 host = dynamic
 disallow = all
 allow = alaw
 qualify = yes
 canreinvite = no
 nat = no

 [100] (internal)
 secret = XXX
 [101] (internal)
 secret = XXX 

We have got two internal peers with numbers 100 and 101. Let us examine these settings, since their understanding is the key to the successful implementation of Asterisk.

[internal] (!)

[internal] is the name of the template, and (!) is the template pointer.
Why do we immediately start using templates? Because they reduce setup time and reduce the amount of text with the configuration file, and for understanding they are very simple.

type = friend

Available options: “peer”, “user” and “friend”. There is often an erroneous opinion about how they differ. Many people think that the “user” parameter allows only outgoing, “peer” - only incoming calls, and “friend” allows calls to both sides. This is not true. The use of the “peer” key disables the validation of the username and password when making a call. When using the “peer” parameter, Asterisk only matches the IP address and port number of the call source, when using “user”, the username field is checked, and the source address is not checked. The "friend" parameter forces the username field and source IP address to be verified.

insecure = invite, port
invite - authentication is disabled for an incoming call.
port - source port checking is disabled.

At the initial setting list both keys.

context = office

The context in which outgoing calls from this device will be processed. Details below.

fromdomain = <domain name or IP>

SIP domain name. For the initial setup, specify the IP address of the server with Asterisk.

host = dynamic

IP address of the peer. In the case of using login and password authentication, set dynamic. A specific IP is indicated only if the peer settings are used for a trunk without registration.

disallow = all
allow = alaw

Specify the allowed codecs.

In our example, the first line prohibits the use of all codecs, and the second allows g711-a. The codec settings are individual for each case, however, most Russian and Ukrainian providers use g711a and g729. The latter is propietary, and Asterisk is supported only in Passthrough mode (that is, transcoding is not possible).

qualify = yes

This line causes Asterisk to poll the device or softphone with OPTIONS packets. Required for monitoring and troubleshooting.

canreinvite = no

Forbids to send media directly between devices. I recommend to put "no", to simplify the settings.

nat = no

We say Asterisku that the feast is not for naty. The description of keys and options for their use, if the server is behind it, is beyond the scope of the article. About the crawl options in the next article.

[100] (internal)
secret = XXX
[101] (internal)
secret = XXX

Here we set the name of the feast and take the settings from the template. The only unique parameters in our example are the peer name and password.
At this point, we finished setting up internal peers, moving on to setting up the interface with the operators. Add 2 entries to the end of sip.conf:
  [operator1]
 fromdomain = <domain name or IP>
 host = 1.2.3.4
 insecure = invite, port
 port = 5060
 qualify = yes
 type = friend
 username = YourLogin
 secret = YourPass
 disallow = all
 allow = alaw
 context = operator1 

I will not paint the values ​​of each line, because all settings are identical to those of the internal peers, with the exception of the host field. In the event that Asterisk acts as a client (and for the server of the operator our asterisk is a client), we need to specify the address of the server of the operator or its dns-name.

Add the second operator:
  [operator2]
 fromdomain = <domain name or IP>
 host = 5.6.7.8
 insecure = invite, port
 port = 5060
 qualify = yes
 type = friend
 disallow = all
 allow = alaw. 
 context = operator2 

There are no differences either, except for the absence of the username and secret lines, since, I remind you, the second operator does not use the registration.

It remains to configure the registration on the server operator number 1. To do this, in the sip.conf file, before the section describing the operator settings, insert the following line:
  register => udp: // YourLogin: YourPass: YourLogin@1.2.3.4/YourLogin 

The syntax for an unprepared person looks complicated, so don’t bother and just call the provider's support and ask them how to register on the server with Asterisk, or ask them to send the settings. As a rule, the majority of operators without any problems send a configuration example for their soft switches.

Surely you have edited the configuration files under a user with limited rights. If this is so, then Asterisk will not be able to access the configuration file, so we write:
  chown asterisk: asterisk /etc/asterisk/sip.conf 

This completes the setting of peers, it is enough to work in the minimum configuration, save and close the file and return to the Asterisk shell with the command:
  asterisk -rv 

In the shell of Asterisk write the command:
  sip reload 

Now you can register users with logins 100 and 101 on your Asterisk. Check the registration status with the following command:
  sip show peers 

If you configured everything correctly, you will see something like this:
 asterisk * CLI> sip show Formersport / ACN Port Status Description 100/100 10.0.0.52 D Yes Yes 59080 OK (1 ms) 101/101 10.0.0.57 D Yes Yes 49973 OK (1 ms) operator1 1.2 .3.4 No No 5060 OK (22 ms) operator2 5.6.7.8 No No 5060 OK (22 ms) 

If you see this, then congratulations, your devices or softphones have successfully registered and Asterisk saw the servers of the operators.

Registration status is checked by the command:
  sip show registry 

If registration was successful, then you should see the following conclusion:
  asterisk * CLI> sip show registry
 Host dnsmgr Username Refresh State Reg.Time
 1.2.3.4:5060 N YourLogin 120 Registered
 1 SIP registrations. 

This completes the setting of peers, let's move on to setting up the dialplan. Dialplan is the heart of Asterisk, with the help of it all calls are processed. Asterisk understands several languages, but in our example we will use the standard one that appeared in the very first Asterisk releases. The configuration file is stored in the /etc/asterisk/extensions.conf file.

Open it with the command:
  nano /etc/asterisk/extensions.conf 

Standard configuration files store many default rules. We do not need them, so we will clear the contents and write the following:
  [general]
 static = yes
 writeprotect = no

 [globals] 

These are the parameters necessary for the normal reading of the dialplan, so they should not be changed.

Let's start with the simple. We need to call from number 100, to number 101. To do this, you need to write a rule immediately after the globals section:
  [office]
 exten => _1XX, 1, Dial (SIP / $ {EXTEN}) 

Let's sort this line.
[office] is the name of the context in which calls from the peer are processed.
exten => - pointer to the beginning of the step.
_1XX - mask. On the mask dwell a little more. With the help of it, all calls that fall into the context of the dialplan are sorted.

The mask uses a set of patterns to sort caller-id calls:
  X - any digit from 0-9
 N - any number from 2-9
 [234-6] - numbers 2, 3, 4 and 6
 .  - any possible characters 

The mask begins with the character "_", which means that it is a template. In case you forget to specify it, Asterisk will take 1XX for the called number and transmit the Dial-pattern instead of the phone number, and the call will not take place.
1 - action number.

Dial is an application. In the dialplan, you can use more than 200 different applications that are used for manipulating calls. Now, at the very beginning, we will use only one application - Dial. From the name it is clear that it is used for making calls.

(SIP / $ {EXTEN}) - arguments for the application. In our example, for internal peers, we use the SIP protocol, so the first argument that we give to dial-y is the signaling protocol used. $ {EXTEN} is the current extension, its value is taken from the header (from the destination field).

/ - separator for passed arguments.

Each of the parameters is separated by commas. In our example, three-digit numbers are used, so the mask should have 3 patterns. In case you want to use a different numbering length, write the required number of patterns in the mask. Now save the file, open the asterisk shell and write the command:
  dialplan reload 

If there are problems with access to the file, then we write:
  chown asterisk: asterisk /etc/asterisk/extensions.conf 

And again we restart the dialplan through the Asterisk shell.

Now we try to call. If you set everything up correctly, then peer with number 100 will reach the peer with number 101. Now we can call inside the office, between peers, who work in the office context. In order to call the city, we need to write a rule for outgoing calls through the first operator. We register it in the office context, it looks like this:
  exten => _XXXXXXX, 1, Dial (SIP / $ {EXTEN} @ operator1) 

Restart the dialplan and try to call the city.

Now we need to take an incoming call. Make a new context in the dialplan, write to the end of the file:
  [operator1]
  exten => s, 1, Dial (SIP / 100 & SIP / 101) 

Let me explain what it means "s". This is a standard mask in Asterisk, under which absolutely all the challenges fall. That is, if we use a trunk with registration, the incoming caller-id will be “s”.

Save and restart the dialplan. Now we can take an incoming call through the operator providing us a landline number.

Now we needed to call a cell phone with a federal number. Let's add one more line to the [office] context:
  exten => _89XXXXXXXXX, 1, Dial (SIP / $ {EXTEN} @ operator1) 

Save, restart the dialplan and call. Everything. Now we can fully use the services of the operator providing the city number.

However, here lies a small problem that will arise when calling to mobile phones in other regions. Since the phone number is federal, all calls will go through the first operator. Therefore, it is appropriate to reduce the size of the masks in the dialplan. For example:
  exten => _8909 [89] [2-9] [1-3] XXXX, 1, Dial (SIP / $ {EXTEN} @ operator1)
 exten => _8901 [456] [2-5] 1XXXX, 1, Dial (SIP / $ {EXTEN} @ operator1)
 exten => _XXXXXXX, 1, Dial (SIP / $ {EXTEN} @ operator1) 

With this design, all calls to phones with the codes from the ranges 890982XXXXX-890999XXXXX and 8901421XXXX-8901651XXXX will go through the first operator. Not quite comfortable, agree with that. In future articles I will lay out the configuration of the dialplan for automatic selection of the operator depending on the direction of the call, since this again goes beyond the scope of this article.
Now we have to make long-distance calls through the second operator. Add a line to the [office] context:
  exten => _ [78] [3-689] XXXXXXXXX, 1, Set (CALLERID (num) = 74991234567)
 same => n, Dial (SIP / $ {EXTEN} @ operator2) 

As you noticed, there are new applications that handle calls. Set (CALLERID (num) = 74991234567) - since we use a trunk without registration, this means that we have to send the caller-id operator. This number does not have to exist. Here we simply introduce ourselves to the provider and no more. Which number to substitute depends on the operator. Some provide a caller-id substitution service to another operator’s number, for making outgoing long-distance calls or for calling. For details, contact the operator. same => n - a pointer that allows you not to write every time a mask and action number. For example:
  exten => 78121234567,1, Answer
 exten => 78121234567,1, Set (CALLERID (name) = Trunk_1
 exten => 78121234567,1, Dial (SIP / 1000) 

Can be replaced by:
  exten => 78121234567,1, Answer
 same => n, Set (CALLERID (name) = Trunk_1
 same => n, Dial (SIP / 1000) 

In the second case, everything is much simpler, isn't it?

That's all, we set up Asterisk. Now we can call to the city, to the cellular, to the intercity and we have an internal connection between the feasts. We get this, quite working dialplan:
  [general]
 static = yes
 writeprotect = no

 [globals]

 [office]
 exten => _8909 [89] [2-9] [1-3] XXXX, 1, Dial (SIP / $ {EXTEN} @ operator1)
 exten => _8901 [456] [2-5] 1XXXX, 1, Dial (SIP / $ {EXTEN} @ operator1)
 exten => _ [78] [3-689] XXXXXXXXX, 1, Set (CALLERID (num) = 74991234567)
 same => n, Dial (SIP / $ {EXTEN} @ operator2)
 exten => _XXXXXXX, 1, Dial (SIP / $ {EXTEN} @ operator1)
 exten => _1XX, 1, Dial (SIP / $ {EXTEN})

 [operator1]
 exten => s, 1, Dial (SIP / 100 & SIP / 101)

Thanks for attention.

Source: https://habr.com/ru/post/264981/


All Articles