Asterisk Configure

最後更新: 2015-10-22

 

概念

Asterisk is a back-to-back user agent (B2BUA). This means that one logical call leg
comes in, and another logical call leg is generated out, and the two
are cross-connected.

 

Install

Ubutun-14:

package

  • asterisk
  • asterisk-config
  • asterisk-core-sounds-en-g722
  • asterisk-moh-opsound-g722
  • asterisk-mysql
  • asterisk-voicemail
  • asterisk-modules
  • asterisk-dahdi (有加 hardware 卡才要此 package)

module

- eSpeak

This module invokes the Espeak TTS engine locally, and uses it to render text to speech.

- flite

to use the Flite voice synthesis engine to render text to speech.

 


Configure

 

# 找出 configure file

server*CLI> config list

res_adsi             /etc/asterisk/adsi.conf
chan_agent           /etc/asterisk/agents.conf
app_alarmreceiver    /etc/asterisk/alarmreceiver.conf
app_amd              /etc/asterisk/amd.conf
......................

# 由於 default configure file 好有參考價值, 所以保存起佢先

mkdir backup;

mv sip.conf backup

mv extensions.conf backup

mv users.conf backup

# 平時主要有兩個 configure file 要改

- users.conf => login 去 asterisk 的 user
- sip.conf => sip channel 的 setting
- extensions.conf => dialplan


Codec

  • G.711 pcmu, pcma(64kbit)
  • G722 (64kbit)
  • BV16 (16kbit)
  • GSM (13kbit)
  • speex (11kbit)

 

 


Asterisk variables

 

Substrings

${foo:offset:length}

Examples:

${123456789:1} - returns the string 23456789
${123456789:-4} - returns the string 6789
${123456789:0:3} - returns the string 123
${123456789:2:3} - returns the string 345
${123456789:-4:3} - returns the string 678

 


Caller ID

 

Caller ID names are currently 8 bit characters, propably ISO8859-1, depending on what your channel drivers handle

Trunk

IAX2 and SIP caller ID names are UTF8

On ISDN Caller ID names are 7 bit, Almost ASCII

* Asterisk does not currently support SIP utf8 caller ID names or caller ID's

 


Configure SIP channels

sip.conf

; General Configure

[general]

; Default context for incoming calls. Defaults to 'default'
context=other

; Disable all transfers (unless enabled in peers or users)
allowtransfer=yes

; IP and port
udpbindaddr=0.0.0.0:5060

; 用不用 TCP
tcpenable=no

; Set the default transports.
; 當 "tcpenable=yes" 才有用
transport=udp

; Enable DNS SRV lookups on outbound calls
srvlookup=no

; Allow or reject guest calls (default is yes)
allowguest=no

; When an incoming INVITE or REGISTER is to be rejected
; always reject with an identical response equivalent to "valid username and invalid password/hash"
alwaysauthreject=yes

; 設定有 yes, nonat, update, outgoing
; RTP media stream to go directly from the caller to the callee
directmedia=yes

; NAT SUPPORT
nat=force_rport,comedia
localnet=192.168.0.0/255.255.0.0
externaddr=12.34.56.78


type=peer

Peers handle both inbound and outbound calls and are matched by ip/port

type=user

matched by their authorization information

type=friend

Asterisk will accept calls from friends like it would for users (authorization)

It is also a peer (as long as its IP is known to Asterisk) (當 "host=dynamic" 時, 要 client login 左先 call 到佢)

其他 setting

qualify

; Consider it down if it's 1 second to reply
; Helps with NAT session
qualify=1000

; How often to check for the host to be up in seconds
;qualifyfreq=60

 

OUTBOUND SIP REGISTRATIONS

[general]
; Asterisk can register as a SIP user agent to a SIP proxy (provider)
register=>[transport://]user[@domain][:secret[:authuser]]@host[:port][/extension]

; retry registration calls every 20 seconds (default)
registertimeout=20

; Number of registration attempts before we give up, 0 = forever
registerattempts=0

[voipbuster]
; Create a SIP peer for outgoing calls
type=friend
username=myusername
secret=mypassword
fromuser=myusername
host=sip.voipbuster.com
dtmfmode=rfc2833
fromdomain=sip.voipbuster.com
context=default

 


User configuration

users.conf

; template configureation (在 users.conf 會用到它)

; '!' indicates to the config parser that this is a only a template
[basic-options](!)

; Set default dtmfmode for sending DTMF. Default: rfc2833
dtmfmode=rfc2833

; peer:只能打, user:只能接, friend:能接能打
type=friend

; another template inheriting basic-options
; [section](name[,name])
[office-phone](!,basic-options)

; client 的 source IP, 
; dynamic = 不管是什麼IP都接受連線
host=dynamic

; Where to start in the dialplan(extensions.conf) when this phone calls
context=my-office

; feature
hassip = yes                      ; 沒此設定時, "sip show peers" 看不到有 user 的.
hasiax = no

[my-codecs](!)

; 可以用什麼 codec
disallow=all
allow=ulaw
allow=alaw


[501](office-phone,my-codecs)
; provide a description
fullname = tim
description = ip phone in office user

; Full caller ID, to override the phones config
callerid=tim <501>

; login password
secret=1234

[502](office-phone,my-codecs)
callerid=ivan <502>
secret=1234

other setting

callwaiting = yes
callwaitingcallerid = yes
threewaycalling = yes
transfer = yes
canpark = yes
cancallforward = yes
callreturn = yes

nat = no

Voicemail

; Create voicemail mailbox and use use macro-stdexten
hasvoicemail = yes

; Set voicemail mailbox 6000 password to 1234
vmsecret = 1234

Group

callgroup=1,3-4                 ; We are in caller groups 1,3,4
pickupgroup=1,3-5               ; We can do call pick-p for call group 1,3,4,5

IP ACL

; ACL: Control access to this account based on IP address

deny=0.0.0.0/0.0.0.0
permit=192.168.0.60/255.255.255.0
permit=192.168.0.60/24

; Use named ACLs defined in acl.conf
acl=named_acl_example

 


dialplan (inbound and outbound calls in Asterisk)

 

extensions.conf -  Static extension configuration file, used by the pbx_config module

[general]
; no => pbx_config will rewrite this file when extensions are modified
static=yes

; CLI command "dialplan save" 可以修改這個檔案
writeprotect=no

; global variables will be cleared and reparsed on a dialplan reload
clearglobalvars=yes

; User context is where entries from users.conf are registered
userscontext=default

[globals]

[default]
include => demo

[demo]
include => stdexten

exten => 2,n,Goto(s,instruct)
exten => 1000,1,Goto(default,s,1)
exten => 1234,n,Gosub(${EXTEN},stdexten(${GLOBAL(CONSOLE)}))
exten => 76245,1,Macro(page,SIP/Grandstream1)
exten => _7XXX,1,Macro(page,SIP/${EXTEN})
exten => 7999,1,Set(TIMEOUT(absolute)=60)
exten => 7999,2,Page(Local/Grandstream1@page&Local/Xlite1@page&Local/1234@page/n,d)
exten => 8500,1,VoicemailMain
exten => 8500,n,Goto(s,6)

[stdexten]
exten => _X.,50000(stdexten),NoOp(Start stdexten)
exten => _X.,n,Set(LOCAL(ext)=${EXTEN})
exten => _X.,n,Set(LOCAL(dev)=${ARG1})
exten => _X.,n,Set(LOCAL(cntx)=${ARG2})
exten => _X.,n,Set(LOCAL(mbx)=${ext}${IF($[!${ISNULL(${cntx})}]?@${cntx})})
exten => a,n,Return()

[my-office]
exten => 501,1,Dial(SIP/501)
exten => 502,1,Dial(SIP/502)

# reload setting

dialplan reload

pattern

'_' => interpreted as a pattern rather than a literal

;   X - any digit from 0-9

;   Z - any digit from 1-9

;   N - any digit from 2-9

;   [1235-9]

;   . - wildcard, matches anything remaining

;   ! - wildcard, causes the matching process to complete as soon

;   s - for ANY call

For example

the extension _NXXXXXX would match normal 7 digit dialings

 

Context

;exten => someexten,{priority|label{+|-}offset}[(alias)],application(arg1,arg2,...)

"include" Statements

# context - demo 套用 context - stdexten

[demo]
include => stdexten

 


asterisk to asterisk (IAX (Inter-Asterisk eXchange))

 

iax.conf

[someIAXpeer]
;To authenticate with a remote IAX server
type=peer
host=some.iax.peer.com
username=myusername
secret=mypassword

[courseUser]
;For user to authenticate with your server
type=user
secret=coursePass
context=default

extensions.conf

exten=>_2xxx,1,Dial(IAX2/someIAXpeer/${EXTEN})

 


Call Management

 

users.conf

; Create manager entry
hasmanager=no
;managerread = system,call,log,verbose,command,agent,user,config
;managerwrite = system,call,log,verbose,command,agent,user,config

manager.conf

; webgui 時才有用
enabled = yes
port = 5038
bindaddr = 127.0.0.1

; No access is allowed by default
; 在 /etc/asterisk/manager.d 設定 login

Predefined Channel Variables

 

${CALLERID(name)}

${CALLERID(num)}

${EXTEN}

${CHANNEL}

${CONTEXT}

To display all channel variable use this command:

DumpChan([<min_verbose_level>])

List:

http://www.voip-info.org/wiki/view/Asterisk+Detailed+Variable+List

 


Variables Usage

 

Set

Set(myvar=1-2-3-a-b-c)

Set(CALLERID(number)=000000)

Set(CALLERID(name)="The Name")

Call

${myvar}

To increment a variable

Set(total=$[${total} + 1])

Split

; delimiter: defaults to -
; fieldspec: number of the field you want, 它是由 "1" 開始的

exten => s,2,Set(AVAILCHAN=${CUT(AVAILCHAN,,1)})

 

 


modules

/etc/asterisk/modules.conf

[modules]
; Asterisk will search for all modules in the /usr/lib/asterisk/modules folder and load them at startup.
autoload=yes

opt:

* preload
* noload
* load

boxA*CLI> module show like res_config

res_config_curl.so             Realtime Curl configuration              0
res_config_ldap.so             LDAP realtime interface                  0
res_config_mysql.so            MySQL RealTime Configuration Driver      0
res_config_sqlite.so           Realtime SQLite configuration            0
res_config_sqlite3.so          SQLite 3 realtime config engine          0

 


Log

 

/etc/asterisk/logger.conf

[general]

; (yyyy-mm-dd HH:MM:SS
dateformat=%F %T       ; ISO 8601 date format

debug => debug
console => notice,warning,error
messages => notice,warning,error
full => notice,warning,error,verbose
syslog.local0 => notice,warning,error

levels:

debug
notice
warning
error
verbose
dtmf

Reload Configure File

logger reload

Rotating logs

/usr/sbin/asterisk -r -x 'logger rotate'

 

 


DOC

http://www.asteriskguru.com/

http://www.asteriskdocs.org/en/3rd_Edition/asterisk-book-html-chunk/index.html

 

 

Creative Commons license icon Creative Commons license icon