Asterisk 系統

70
Asterisk 系系 Asterisk 系系系系系系系 http://downloads.oreilly.com/ books/9780596510480.pdf

description

Asterisk 系統. Asterisk 系統架構與說明 http://downloads.oreilly.com/books/9780596510480.pdf. Asterisk 系統架構與介紹. 什麼是 Asterisk. PSTN. VoIP. Asterisk 系統架構與介紹. Basic Web page(HTML) Hello World! . 簡單的應用. Basic Extension(Dialplan) - PowerPoint PPT Presentation

Transcript of Asterisk 系統

Page 1: Asterisk  系統

Asterisk 系統Asterisk 系統架構與說明

http://downloads.oreilly.com/books/9780596510480.pdf

Page 2: Asterisk  系統

Asterisk 系統架構與介紹•什麼是 Asterisk

PSTN

VoIP

Page 3: Asterisk  系統

Asterisk 系統架構與介紹

Page 4: Asterisk  系統

簡單的應用Basic Extension(Dialplan)

exten => 100,1,Answer()exten => 100,n,wait(1)exten => 100,n,Play(hello)exten => Hangup()

Basic Web page(HTML)

<html><head></head><body><h1>Hello World!</h1></body>

</html>

Page 5: Asterisk  系統

複雜的應用•複雜的 VoIP 服務 •複雜的網路服務

Page 6: Asterisk  系統

搞定 Asterisk 的關鍵要素•Linux 基本管理能力

•TCP/IP 網路基礎•VoIP 基礎

•Script 程式概念

Page 7: Asterisk  系統

Asterisk 硬體測試結果• 硬體 :

• AMD Athlon64 X2 4200 • 1G RAM• 80G SATA

• 測試方式 : SIP call• Connection• Wait• Play audio• disconnection

同時通話數 330 330 550

CPU 使用率 149% 14.8% 57.6%

平均負載 49 25 60

儲存裝置 硬碟 RAM Disk RAM Disk

Page 8: Asterisk  系統

Asterisk 軟體系統

Page 9: Asterisk  系統

Asterisk : System ServiceLinux Operating System

Web DNS mail proxy firewall

DHCP

Page 10: Asterisk  系統

Asterisk : module channelsLinux Operating System

SIP IAX2 Skype DAHDI

Page 11: Asterisk  系統

Asterisk : module resourceLinux Operating System

SIP IAX2 Skype DAHDI

CDR Codec Format Config

Page 12: Asterisk  系統

Asterisk : Command Line InterfaceLinux Operating System

SIP IAX2 Skype DAHDI

CDR Codec Format Config

CLI

Page 13: Asterisk  系統

Asterisk : Applications and Functions

Linux Operating System

SIP IAX2 Skype DAHDI

CDR Codec Format Config

CLI Applications

Functions

Page 14: Asterisk  系統

Asterisk : Manager Interface(AMI)Linux Operating System

SIP IAX2 Skype DAHDI

CDR Codec Format Config

CLI Applications

FunctionsDialplan

AMI

Page 15: Asterisk  系統

Asterisk : Manager Interface(AMI)Linux Operating System

SIP IAX2 Skype DAHDI

CDR Codec Format Config

CLI Applications

FunctionsDialplan

AMI

Page 16: Asterisk  系統

Asterisk Call flow•B2BUA : Back to Back User Agent

PSTN

Internet

Page 17: Asterisk  系統

Asterisk Call process

SIP channel driver

PBX Engine SIP channel driver

sip.conf[100]type=friendhost=dynamiccontext=inside

extensions.conf[inside]exten => 101,1,Dial(SIP/101,20)exten => 101,n,Voicemail(101)

exten => 102,1,Dial(SIP/102,20)exten => 102,n,Voicemail(102)

101 Ring!

Phone 102 Phone 101

Page 18: Asterisk  系統

Asterisk : Connectivity

Page 19: Asterisk  系統

Asterisk Solution : PBX

Page 20: Asterisk  系統

Asterisk•全球已經有非常多以 Asterisk 為基礎的系統

• Freepbx• Elastix• PBX in a flash

•有非常多的硬體廠商• Digium• Sangoma• Voicetronix

•有非常多的 add-on• CDR• ODBC• Voicemail• Security• Conference

Page 21: Asterisk  系統

Digium’s AsteriskNOW

Page 22: Asterisk  系統

Asterisk Applications•VoIP Gateway•IVR•Call Center•Conference Bridge

Page 23: Asterisk  系統

Asterisk 系統建置Asterisk 系統建置Asterisk 系統調整

SIP client 軟體 (Windows/Mac)SIP Client軟體 (for Android)

SIP hardphoneSIP.conf

Extension.conftcpdump/Wireshark

Asterisk CLI command

Page 24: Asterisk  系統

相關連結•使用手冊

•http://ofps.oreilly.com/titles/9780596517342/index.html •AsteriskNOW

•https://www.asterisk.org/downloads•SIP Client

•Windows•http://www.counterpath.com/x-lite-4-for-windows-download.html

•Mac•http://www.counterpath.com/x-lite-4-for-mac-download.html

•iPad•http://www.counterpath.com/bria-ipad-edition.html

•Android•https://play.google.com/store/apps/details?id=com.csipsimple&hl=zh_TW

•Wireshark•https://www.wireshark.org/download.html

Page 25: Asterisk  系統

AsteriskNOW

Page 26: Asterisk  系統

Installing AsteriskNOW

Page 27: Asterisk  系統

Installing AsteriskNOW

Page 28: Asterisk  系統

Installing AsteriskNOW

Page 29: Asterisk  系統

Installing AsteriskNOW

Page 30: Asterisk  系統

Installing AsteriskNOW

Page 31: Asterisk  系統

Installing AsteriskNOW

Page 32: Asterisk  系統

Installing AsteriskNOW

Page 33: Asterisk  系統

Installing AsteriskNOW

Page 34: Asterisk  系統

Installing AsteriskNOW

Page 35: Asterisk  系統

Installing AsteriskNOW•要啟動的服務

•httpd•ntpd•sendmail•sshd

Page 36: Asterisk  系統

啟動 NTP 以做為時間校準•為了確保通話時間記錄的精確性 , 以及讓 Asterisk 和其他程式間正確地運作 , 因此必須保持時間的正確性 . •Ntpd deamon 可以確保你的系統時間的正確性 .

•透過 AsteriskNOW, 我們只要啟動 ntpd 後 , 系統便可以和其他ntp server 進行時間較正 , 並可以提供做為其他電腦的系統校正 , 以達到整體系統時間的正確性 .

# yum install ntp...Is this ok [y/N]: y...# ntpdate pool.ntp.org# chkconfig ntpd on# service ntpd start

Page 37: Asterisk  系統

新增系統管理者•一般來說 , 使用 root 帳號做為管理有極大的風險 .•建議新增一個非 root 的管理者 , 但使用權限與 root 相近 .•透過 adduser command 可以新增一個使用者 .•密碼切勿使用範例或易猜的數字、英文名 .

# adduser asteriskpbx# passwd asteriskpbxChanging password for user asteriskpbx.New UNIX password:Retype new UNIX password:

Page 38: Asterisk  系統

新增系統管理者• 為了讓我們新增的使用者有 supervisor do (sudo) 的權限 , 我們透過 visudo 這個程式來修改 sudoer這個設定檔 , 以提高asteriskpbx 這個使用者的權限 .如果一開始沒有安裝 , 可以後續再安裝此 package.

•透過 visudo , 可以先找到下面這行 , 把前面的# 取消 , 以啟動此行的設定 .

# yum install sudo

# visudo

## Allows people in group wheel to run all commands%wheel ALL=(ALL) ALL

Page 39: Asterisk  系統

新增系統管理者•然後用 vi 或 nano 修改 /etc/group 這個檔案 , 以讓我們這個asteriskpbx 帳號可以和 root 的權限群組一致 .

•存檔 , 登出 root, 然後用 asteriskpbx 帳號登入 . 透過下面指令試看看是否可以使用

•因為是初次登入 , 所以會確認此帳號的密碼 , 以確認其權限無誤 . 正確的狀況下 , 應該可以看到 linux 顯示 root 底下的檔案及目錄 .

wheel:x:10:root,asteriskpbx

$ sudo ls /root/[sudo] password for asteriskpbx:

Page 40: Asterisk  系統

新增系統管理者•為了方便使用 , 不要經常輸入整個路徑 , 所以我們要將常用的路徑加到系統裡 , 或修改 bash 使用者檔案 , 以方便使用 .•在 AsteriskNOW下 , CentOS 提供 bash , 所以我們可以修改我們使

用者路徑底下的 .bash_profile 檔案 .

•此檔案放在使用者 asteriskpbx 目錄下的隱藏檔 . 在裡面修改如下 :

$ vim ~/.bash_profilePATH=$PATH:$HOME/bin:/usr/sbin:/sbin

Page 41: Asterisk  系統

asterisk.conf•The [directories] Section

•TableOption Value/Example Notes

astetcdir /etc/asterisk The location where the Asterisk configuration files are stored.

astmoddir /usr/lib/asterisk/modules The location where loadable modules are stored.

astvarlibdir /var/lib/asteriskThe base location for variable state information used by various parts of Asterisk. This includes items that are written out by Asterisk at runtime.

astdbdir /var/lib/asteriskAsterisk will store its internal database in this directory as a file called astdb.

astkeydir /var/lib/asteriskAsterisk will use a subdirectory calledkeys in this directory as the default location for loading keys for encryption.

astdatadir /var/lib/asteriskThis is the base directory for system-provided data, such as the sound files that come with Asterisk.

astagidir /var/lib/asterisk/ agi-binAsterisk will use a subdirectory calledagi-bin in this directory as the default location for loading AGI scripts.

astspooldir /var/spool/asteriskThe Asterisk spool directory, where voicemail, call recordings, and the call origination spool are stored.

astrundir /var/run/asteriskThe location where Asterisk will write out its UNIX control socket as well as its process ID (PID) file.

astlogdir /var/log/asterisk The directory where Asterisk will store its log files.

Page 42: Asterisk  系統

asterisk.conf•The [options] Section - 1

Option Value/Example Notes

verbose 3Sets the default verbose setting for the Asterisk logger. This value is also set by the -v command-line option. The verbose level is 0by default.

debug 3Sets the default debug setting for the Asterisk logger. This value is also set by the -d command-line option. The debug level is 0 by default.

alwaysfork yesForking forces Asterisk to always run in the background. This option is set to no by default.

nofork yesForces Asterisk to always run in the foreground. This option is set to no by default.

quiet yesQuiet mode reduces the amount of output seen at the console when Asterisk is run in the foreground. This option is set tono by default.

timestamp yesAdds timestamps to all output except output from a CLI command. This option is set to noby default.

execincludes yesEnables the use of #exec in Asterisk configuration files. This option is set to no by default.

console yesRuns Asterisk in console mode. Asterisk will run in the foreground and will present a prompt for CLI commands. This option is set tono by default.

Page 43: Asterisk  系統

asterisk.conf

NotesValue/

ExampleOption

Configures how the prompt language is used in building the path for a sound file. By default, this is yes, which places the language before any subdirectories, such asen/digits/1.gsm. Setting this option to no causes Asterisk to behave as it did in previous versions, placing the language as the last directory in the path, (e.g. digits/en/1.gsm).

yeslanguageprefix

Tells Asterisk to generate a core dump in the case of a crash. This option is set to no by default.[b]

yesdumpcore

Disables a few warning messages. This option was put in place to silence warnings that are generally correct, but may be considered to be so obvious that they become an annoyance. This option is set to no by default.

yesdontwarn

Suppresses color output from the Asterisk console. This is useful when saving console output to a file. This option is set to no by default.

yesnocolor

Loads keys from the astkeydir at startup. This option is set to no by default.[a]

yesinitcrypto

Runs the Asterisk application with real-time priority. This option is set to no by default.

yeshighpriority

•The [options] Section - 2

Page 44: Asterisk  系統

NotesValue/Exampl

eOption

Sets the minimum number of megabytes of free memory required for Asterisk to continue accepting calls. If Asterisk detects that there is less free memory available than this threshold, new calls will not be accepted. This option is not set by default.

1minmemfree

Sets a maximum load average. If the load average is at or above this threshold, Asterisk will not accept new calls. No threshold is set by default.

0.9maxload

Sets a maximum number of simultaneous inbound channels. No limit is set by default.

100maxcalls

Automatically sets the system name by using the hostname of the system. This option is set tono by default.

yesautosystemname

Gives this instance of Asterisk a unique name. When this has been set, the system name will be used as part of the uniqueid field for channels. This is incredibly useful if more than one system will be logging CDRs to the same database table. By default, this option is not set.

my_ system _name

systemname

Uses a timing source to synchronize audio that will be sent out to a channel in cases such as file playback or music on hold. This option is set to yes by default and should be left that way; its usefulness has greatly diminished over the last few major versions of Asterisk.

yesinternal_timing

asterisk.conf•The [options] Section - 3

Page 45: Asterisk  系統

NotesValue/

ExampleOption

When this option is enabled, the Asterisk configuration directory will be protected with a lock. This helps protect against having more than one application attempting to write to the same file at the same time. The default value isno.

nolockconfdir

Setting this option to yes causes Asterisk to not display notifications of remote console connections and disconnections at the Asterisk CLI. This is useful on systems where there are scripts that use remote consoles heavily. The default setting is no.

yeshideconnect

The built-in documentation for Asterisk applications, functions, and other things is included in an external XML document. This option specifies the preferred language for documentation. If it is not available, the default ofen_US will be used.

en_USdocumentation_language

Sets the system group that the Asterisk application should run as. This option is not set by default.

asteriskrungroup

Sets the system user that the Asterisk application should run as. This option is not set by default, meaning that the application will continue to run as the user that executed the application.

asteriskrunuser

asterisk.conf•The [options] Section - 4

Page 46: Asterisk  系統

asterisk.conf•The [files] Section

Option Value/Example Notes

astctlpermissions 0660 Sets the permissions for the Asterisk control socket.

astctlowner root Sets the owner for the Asterisk control socket.

astctlgroup apache Sets the group for the Asterisk control socket.

astctl asterisk.ctlSets the filename for the Asterisk control socket. The default is asterisk.ctl.

Page 47: Asterisk  系統

asterisk.conf•The [compat] Section

Option Value/Example Notes

pbx_realtime 1.6

In versions earlier than Asterisk 1.6.x, thepbx_realtime module would automatically convert pipe characters into commas for arguments to Asterisk applications. This is no longer done by default. To enable this previous behavior, set this option to 1.4.

res_agi 1.6

In versions earlier than Asterisk 1.6.x, theEXEC AGI command would automatically convert pipe characters into commas for arguments to Asterisk applications. This is no longer done by default. To enable this previous behavior, set this option to 1.4.

app_set 1.6

Starting with the Asterisk 1.6.x releases, theSet() application only allows setting the value of a single variable. Previously, Set() would allow setting more than one variable by separating them with a &. This was done to allow any characters in the value of a variable, including the & character, which was previously used as a separator. MSet() is a new application that behaves like Set() used to. However, setting this option to 1.4 makesSet() behave like MSet().

Page 48: Asterisk  系統

modules.conf•The [modules] Section

Option Value/Example Notes

autoload yes

Instead of explicitly listing which modules to load, you can use this directive to tell Asterisk to load all modules that it finds in the modules directory, with the exception of modules listed as not to be loaded using the noload directive. The default, and our recommendation, is to set this option to yes.

preload res_odbc.so

Indicates that a module should be loaded at the beginning of the module load order. This directive is much less relevant than it used to be; modules now have a load priority built into them that solves the problems that this directive was previously used to solve.

load chan_sip.soDefines a module that should be loaded. This directive is only relevant if autoload is set to no.

noload chan_alsa.soDefines a module that should not be loaded. This directive is only relevant if autoload is set to yes.

require chan_sip.soDoes the same thing as load; additionally, Asterisk will exit if this module fails to load for some reason.

preload-require res_odbc.soDoes the same thing as preload; additionally, Asterisk will exit if this module fails to load for some reason.

Page 49: Asterisk  系統

indications.conf• 不同國家 , 不同電信業者 , 對於其電話系統發出的聲音頻率 , 如 dialtone, busy, ringback 等聲音可能會不同 .

• Indications.conf 定義了電話系統應該發出的聲響頻率 , 以利和 voip 做對應 . (預設是 us)

• 也可以透過 CHANNEL(tonezone)= .. 的應用程式重新設定 .

Page 50: Asterisk  系統

musiconhold.conf•音效檔路徑

•/var/lib/asterisk/moh

•也可以取得免費的音樂 , 重新 convert後置於上述路徑 .•DBS ITCrew Free MOH: CC BY-SA (free, royalty free, commercially usable) on hold music.•StarTel Free Music On Hold: Download Royalty Free Music for your PBX

•建議將音效檔轉成 asterisk 可能有效應用的檔案為最佳 :•*.sln•*.wav•*.pcm•*.gsm

Page 51: Asterisk  系統

Sip.conf

Page 52: Asterisk  系統

設定 sip.conf

• 輸入 [general] [general] context=unauthenticated ; 預設撥進來的 contextallowguest=no ; 不充許未經許可的撥號srvlookup=yes ; 將 DNS查詢的功能開啟udpbindaddr=0.0.0.0 ; 使用 udp 做為所有介面的 service porttcpenable=no ; 不使用 tcp port 做為服務 port

Page 53: Asterisk  系統

設定 sip.conf

• 設定 sip channel 和 設備[000F90B0B771] ; 定義一個 sectiontype=friend ; Channel driver 會先去比對

; honst name, 再比對 IP context=LocalSets ; 指定由此進來的 call 所對應的dialplanhost=dynamic ; 此設定 IP動態 , 要跟asterisk 註冊nat=yes ; 此設備可在 NAT 後

secret=s3CuR#p@s5 ; 指定此設備的密碼 ; dtmfmode=auto ; 接受 dtmf disallow=all ; 將此設備可接受的 codec reset allow=ulaw ; 此設備可使用 ulaw allow=alaw ; 此設備可使用 alaw

Page 54: Asterisk  系統

extensions.conf

Page 55: Asterisk  系統

設定 extensions.conf

[LocalSets] exten => 900,1,Answer() exten => 900,2,PlayBack(hello-world)Exten => 900,3,Hangup()

Page 56: Asterisk  系統

Sip.conf

*

*

Page 57: Asterisk  系統

設定 sip.conf•輸入 [general]

[general] context=unauthenticated ; 預設撥進來的 context, 因為我們不會另外

; 設定一個叫unauthenticated 的 context,

; 即表示進來的需求 , 如果不在我們設定的

; context內 , 那就不會被接受allowguest=no ; 不充許未經許可的撥號srvlookup=yes ; 將 DNS查詢的功能開啟udpbindaddr=0.0.0.0 ; 使用 udp 做為所有介面的 service porttcpenable=no ; 不使用 tcp port 做為服務 port

**

Page 58: Asterisk  系統

設定 sip.conf•設定 sip channel 和設備[c42c031ed01d] ; 定義一個 sectiontype=friend ; Channel driver 會先去比對

; honst name, 再比對 IP context=LocalSets ; 指定由此進來的 call 所對應的dialplanhost=dynamic ; 此設定 IP動態 , 要跟 asterisk

註冊nat=yes ; 此設備可在 NAT 後 secret=s3CuR#p@s5 ; 指定此設備的密碼 ; dtmfmode=auto ; 接受 dtmf disallow=all ; 將此設備可接受的 codec reset allow=ulaw ; 此設備可使用 ulaw allow=alaw ; 此設備可使用 alaw

**

Page 59: Asterisk  系統

Sip.conf•type = friend

**

Page 60: Asterisk  系統

Sip.conf•context=LocalSets•當設備順利登入 asterisk 後 , 後續的撥碼計劃(dialplan)則對應到此設定內的 Context.•此 context 在 extensions.conf 裡也會有一個對應的section, 否則即表示此設備僅會使用預設的 dialplan

或 無法使用 .• 此範例所指即為 LocalSets

**

Page 61: Asterisk  系統

Sip.conf•host = dynamic•當 asterisk 要撥給此設備時 , asterisk 必須先知道此設備在網路上的何處 . •當設定為 dymamic 時 , 表示此設備在登入時即會提供 ip 告知 asterisk. 一般即表示此設備為動態 IP, 或其 IP 有可能變動 .•若因安全性需求 , 必須指定固定 IP 時 , 僅需將dynamic 改為設備 IP 即可 , 如 :•host = 192.168.10.200

**

Page 62: Asterisk  系統

Sip.conf•nat = yes•告知 asterisk 此設備可能在 NAT網路後 , 要開啟部分 NAT功能 , 以利其 SIP的運作 .•因為 SIP 在溝通時會帶其 IP, 若此設備在 NAT網路內 , 卻未進行此設定 , 會造成此設備在 SIP 連線時 , 產生連線失敗的狀況 .

**

Page 63: Asterisk  系統

Sip.conf•secret = s3CuR#p@s5•此設備在登入 asterisk 時所使用的密碼 .•密碼安全性越高越好 , 避免被 cracker入侵 , 產生盜打的現象 .•國際上有很多盜打的集團 , 專門找安全性差的 IP PBX, 以進行盜打的行為 , 做為獲利的來源 .•此設定為非必要 . 端看使用者需求 .•可在 linux shell 下此 script, 以隨機取個一個安全性高的密碼 :$dd if=/dev/random count=1 bs=8 2>/dev/null | base64 | sed -e 's/=*$//'

**

Page 64: Asterisk  系統

Sip.conf•dtmfmode=auto •此設定用來定義 DTMF (即電話鍵盤 ) 的格式 , 好用來比對電話所送來的 DTMF 是否與定義的相符 .•可用的選項如下 :•info : 使用 SIP 的 INFO 方式收碼•inband : 使用 inband 音效格式•rfc2833 : 使用 RFC 定義的 out-of-band 方式•auto : 由 asterisk 自動判斷何種格式 . 預設以 rfc2833為優先 . 

**

Page 65: Asterisk  系統

Sip.conf•disallow=all•allow=ulaw•此選項用來指定何種音效編碼方式可在此設備上收送 .•disable = all 放在前面 , 用來 reset 之前可能在別的section 內的設定 .•all = … 用來指定此設備可接受的編碼方式 . 依出現的前後順序表示其優先順序 .

**

Page 66: Asterisk  系統

Don’t forget•因為已重新設定過 sip.conf (or sip_custom.conf), 因此要讓 asterisk 知道有新的設定 .•重新啟動 sip的方式 :•在 linux shell下 :

•在 asterisk CLI下 :

**

#asterisk -x 'sip reload'

localhost*CLI> sip reload

Page 67: Asterisk  系統

extensions.conf

*

*

Page 68: Asterisk  系統

設定 extensions.conf•extensions_custom.conf

[LocalSets] exten => 900,1,Answer() exten => 900,2,PlayBack(hello-world)Exten => 900,3,Hangup()

**

Page 69: Asterisk  系統

sip.conf 與 extensions.conf

sip.conf

[123456789ABC0]context=LocalSets_A

[123456789ABC5]

host=dynamic

extensions.conf

[general][globals]

[LocalSets_A]exten => 105,1,Dial(SIP/123456789ABC5]

Page 70: Asterisk  系統

設定 extensions.conf•設定此設備的分機[LocalSets]exten => 100,1,Dial(SIP/c42c031ed01d);當撥 100時 , 則對應到 000F90B0B771這個 sip channel去 exten => 900,1,Answer() exten => 900,2,PlayBack(hello-world)exten => 900,3,Hangup()

**