2018年8月24日 星期五

NTP安裝設定

1.若沒NTP服務請先安裝
#yum install ntp

2.設定/etc/ntp.conf
# 1. 先處理權限方面的問題,包括放行上層伺服器以及開放區網用戶來源:
restrict default kod nomodify notrap nopeer noquery     <==拒絕 IPv4 的用戶
restrict -6 default kod nomodify notrap nopeer noquery  <==拒絕 IPv6 的用戶
restrict tock.stdtime.gov.tw   <==放行 tock.stdtime.gov.tw 進入本 NTP 伺服器
restrict tick.stdtime.gov.tw
restrict time.stdtime.gov.tw
restrict 127.0.0.1
restrict -6 ::1
#restrict 192.168.1.0 mask 255.255.255.0 nomodify

# 2. 設定主機來源,請先將原本的 [0|1|2].centos.pool.ntp.org 的設定註解掉:
server tock.stdtime.gov.tw prefer  <==以這部主機為最優先
server tick.stdtime.gov.tw
server time.stdtime.gov.tw

# 3.預設時間差異分析檔案與暫不用到的 keys 等,不需要更動它:
driftfile /var/lib/ntp/drift
keys      /etc/ntp/keys

3.啟動NTP服務
#systemctl start ntpd

4.將NTP預設起動
#chkconfig ntpd on

2018年8月21日 星期二

MySQL(MariaDB)將Latin1改成UTF-8

1. #vim /etc/my.cnf

2.將下列複製貼到檔案內
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

3. #reboot

2018年8月16日 星期四

elastix安裝 G723 G729

1.下載 g723、g729 模組檔
http://asterisk.hosting.lv/下載相對應的模組檔
先找asterisk的版本再找相對應CPU的版本
現在的電腦應該都是對應到x86_64-core2-sse4

2.檔案存到asterisk/modules
路徑為/usr/lib/asterisk/modules(32bit)  /usr/lib64/asterisk/modules(64bit)

3.重新啟動asterisk服務
#service asterisk restart

4.啟用codec
編輯sip_general_custom.conf
加入
allow=g723
allow=g729

5.到 asterisk-cli 執行 core show translation recalc 10 確認是否有啟用

2018年7月15日 星期日

CentOS 7 把網卡eno16777736改成eth0

CentOS 7預設網卡名稱是eno16777736
看了很不習慣上網找了修改方法

1. vi /etc/sysconfig/grub
找到GRUB_CMDLINE_LINUX="...."這行
在雙引號內加入net.ifnames=0 biosdevname=0

2. 用 grub2-mkconfig 指令重新建立 GRUB 的設定檔
grub2-mkconfig -o /boot/grub2/grub.cfg

3. 改掉ifcfg-eno16777736名稱
mv /etc/sysconfig/network-scripts/ifcfg-eno16777736 /etc/sysconfig/network-scripts/ifcfg-eth0

4. 修改ifcfg-eth0
NAME=eth0

5.reboot

Asterisk manager API (AMI)

Asterisk manager API(AMI)
Asterisk控制介面(AMI)允許管理用戶端程式連接到一個asterisk實例並且可以通過TCP/IP流發送命令或讀取事件。這在試圖跟蹤asterisk的狀態或其中的電話用戶端狀態時很有用,AMI說明了用戶端基於的一般(可能是動態的)規則。
一種簡單的"key: value"協定用於已連接的管理用戶端和asteriskPBX之間的資訊傳遞。每行都用回車換行(/r/n)來結束。

協定特點:
在發送命令前,你必須建立一個連接。
在認證後,資料包任何時間都可以在兩個方向上互相傳輸。
資料包第一行為"Action"時表明是由管理用戶端發送到asterisk,第一行為"Event"或"Response"表明有asterisk發送到管理用戶端。資料包內各行順序可以變化,對資料包沒有影響。
回車換行由於區分每一行,空行(兩個連續回車換行)用於表明命令結束,Asterisk開始處理命令。

資料包類型:
資料包的類型通過一下幾種關鍵字來劃分
Action:一個由管理用戶端發送到Asterisk的資料包,請求執行特定的動作。只有有限的幾種動作供用戶端使用,每個動作是由Asterisk伺服器上的模組所決定的。一次只能執行一個動作,該動作資料包包含了動作名稱以及執行所必需的參數。
Response:Asterisk對管理用戶端動作的回應。
Event: 有關Asterisk核心或擴展模組發生事件的資訊。

建立管理連接和用戶身份驗證:     
為了與Asterisk進行管理通話,管理用戶端必須建立到Asterisk伺服器偵聽埠(一般是5038)的TCP/IP連接,並使用登陸動作進行身份驗證。這就要求在Asterisk伺服器上創建用戶帳戶,用戶帳戶在/etc/asterisk/manager.conf中配置。帳戶由允許登陸的主機,登錄密碼以及被授予的許可權列表組成。
登錄和身份驗證的經理,你必須發送一個" 登錄 "行動,用您的用戶名和秘密(密碼作為參數)。 Here is an example:下面是一個例子:
Action: login
Username: admin
Secret: secret

你還可以通過"Events: off"阻止事件資料包發送到你的連接。例如:
Action: login
Username: admin
Secret: secret
Events: off

動作數據包:     
向Asterisk發送動作時,額外的關鍵字可以提供進一步的動作資訊。例如,你可能希望指定號碼或通道的斷開。此外,如果您的操作會導致Asterisk來執行撥號規則中的條目,你可以傳遞變數給撥號規則。這與你傳遞關鍵字的方式是一樣的。
向Asterisk發送動作資料包時,按照以下格式:
Action: <action type><CRLF>
<Key 1>: <Value 1><CRLF>
<Key 2>: <Value 2><CRLF>
...
Variable: <Variable 1>=<Value 1><CRLF>
Variable: <Variable 2>=<Value 2><CRLF>
...
<CRLF>

管理動作(Action):     
通過CLI命令show manager commands的輸出結果就是管理動作列表。
具體如下:
AbsoluteTimeout:這個命令將要求Asterisk在指定秒數後掛斷特定的通道,從而有效地結束通話。
參數:
Channel :哪些管道掛斷,如SIP/123-1c20
Timeout :到通道應掛斷的秒數
例子:
請求資訊
Action: AbsoluteTimeout
Channel: SIP/123-1c20
Timeout: 20

返回成功資訊
Response: Success
Message: Timeout Set

返回失敗資訊
Response: Error
Message: No such channel

ChangeMonitor:更改記錄某通道資訊檔案名。
參數:
Channel:通道名稱
File:檔案名

例子:
發送請求
ACTION: ChangeMonitor
Channel: SIP/x7065558529-1c20
File: 20050103-140105_cc51

返回失敗資訊
Response: Error
Message: No such channel

返回成功資訊
Response: Success
Message: Stopped monitoring channel

Command:發送Asterisk CLI命令。
參數:
Command:Asterisk CLI 命令。

例子:
發送請求
ACTION: COMMAND
command: Show Channels

返回資訊
Response: Follows
Channel (Context Extension Pri ) State Appl. Data
0 active channel(s)
--END COMMAND-

注意:返回的事件響應結果並不是發送一個回車換行(/r/n),而只發送回車(/n),如果你在中斷處理常式中只等待回車換行,那麼這條資訊將會被被拋棄。
例如command返回資訊如下:
Response: Follows/r/n
Channel Location State Application(Data)/n
SIP/x7065551212b-1af (None) Ringing AppDial((Outgoing Line))/n
SIP/x7065551212c-2aa 29@default:2 Ring Dial(SIP/x7065551212b)/n
2 active channels/n
1 active call/r/n
--END COMMAND--/r/n
/r/n


Events:事件控制流,用於啟用/禁用發送給管理用戶端的事件。
參數:
EventMask: 'on'表示所有事件均會被發送到管理用戶端。
'off'表示所有時間都不會被發送。
'system,call,log'用於選擇被發送的事件。
提示:有些事件是啟用或禁已經在.conf檔中配置了, They might be disabled by default.他們可能是默認為禁用。

例子:
注意在此例子中EventMask事件沒有立即返回的回應資訊,只有在設備登出時才返回相關資訊。
發送請求:
ACTION: EVENTS
EVENTMASK: ON
返回資訊:
Event: PeerStatus
Peer: SIP/ChannelName
PeerStatus: Unregistered
Cause: Expired

當EventMask參數為'off'時會立即返回回應資訊,如下:
發送:
ACTION: EVENTS
EVENTMASK: OFF
返回:
Response: Events Off

ExtensionState:檢查分機的狀態。
參數:Exten,Context,ActionID

例子:
發送:
ACTION: ExtensionState
Context: default
Exten: 301
ActionID: 1

接收:
Response: Success
ActionID: 1
Message: Extension Status
Exten: 2001
Context: default
Hint: SIP/2001
Status: 0

Status狀態碼:
-1 =擴展找不到
0 =空閒
1 =在使用
2 =忙
4 =不可用
8 =振鈴
16 =正等待

GetVar:獲取一個通道變數。
參數:Channel,Variable,actionID
ActionID是一個可選的參數匹配資訊。

例子:
發送:
ACTION: GetVar
Channel: SIP/5060-44d225d0
Variable: extension
成功返回:
Response: Success
Value: 17065551419
失敗返回:
Response: Error
Message: No such channel

Response: Success
varname: (null)

Hangup:掛斷指定通道。
參數:Channel

例子:

發送:
ACTION: Hangup
Channel: SIP/x7065558529-99a0

接收:
Event: Unlink
Channel1: SIP/127.180.254.109-44df88e8
Channel2: SIP/x7065558529-99a0
Uniqueid1: 1124989110.20473
Uniqueid2: 1124989110.20474

Event: Hangup
Channel: SIP/x7065558529-99a0
Uniqueid: 1124989110.20474
Cause: 16

Event: Hangup
Channel: SIP/127.180.254.109-44df88e8
Uniqueid: 1124989110.20473
Cause: 16

Response: Success
Message: Channel Hungup

IAXpeers:顯示iax資訊清單。
例子:

發送:
Action: IAXPeers

接收:
Name/Username Host Mask Port Status
8002 (Unspecified) (D) 255.255.255.255 0 UNKNOWN
8001 192.168.0.91 (D) 255.255.255.255 4569 OK (5 ms)
2 iax2 peers [1 online, 1 offline, 0 unmonitored]

ListCommands:顯示命令清單。
參數:ActionID

例子:
發送:
Action:ListCommands
接收:Response: Success
AbsoluteTimeout: Set Absolute Timeout (Priv: call,all)
AgentCallbackLogin: Sets an agent as logged in by callback (Priv: agent,all)
AgentLogoff: Sets an agent as no longer logged in (Priv: agent,all)
Agents: Lists agents and their status (Priv: agent,all)
ChangeMonitor: Change monitoring filename of a channel (Priv: call,all)
Command: Execute Asterisk CLI Command (Priv: command,all)
DBGet: Get DB Entry (Priv: system,all)
DBPut: Put DB Entry (Priv: system,all)
Events: Control Event Flow (Priv: )
ExtensionState: Check Extension Status (Priv: call,all)
Getvar: Gets a Channel Variable (Priv: call,all)
Hangup: Hangup Channel (Priv: call,all)
IAXnetstats: Show IAX Netstats (Priv: )
IAXpeers: List IAX Peers (Priv: )
ListCommands: List available manager commands (Priv: )
Logoff: Logoff Manager (Priv: )
MailboxCount: Check Mailbox Message Count (Priv: call,all)
MailboxStatus: Check Mailbox (Priv: call,all)
MeetmeMute: Mute a Meetme user (Priv: call,all)
MeetmeUnmute: Unmute a Meetme user (Priv: call,all)
Monitor: Monitor a channel (Priv: call,all)
Originate: Originate Call (Priv: call,all)
Park: Park a channel (Priv: call,all)
ParkedCalls: List parked calls (Priv: )
PauseMonitor: Pause monitoring of a channel (Priv: call,all)
Ping: Keepalive command (Priv: )
PlayDTMF: Play DTMF signal on a specific channel. (Priv: call,all)
QueueAdd: Add interface to queue. (Priv: agent,all)
QueuePause: Makes a queue member temporarily unavailable (Priv: agent,all)
QueueRemove: Remove interface from queue. (Priv: agent,all)
Queues: Queues (Priv: )
QueueStatus: Queue Status (Priv: )
Redirect: Redirect (transfer) a call (Priv: call,all)
SetCDRUserField: Set the CDR UserField (Priv: call,all)
Setvar: Set Channel Variable (Priv: call,all)
SIPpeers: List SIP peers (text format) (Priv: system,all)
SIPshowpeer: Show SIP peer (text format) (Priv: system,all)
Status: Lists channel status (Priv: call,all)
StopMonitor: Stop monitoring a channel (Priv: call,all)
UnpauseMonitor: Unpause monitoring of a channel (Priv: call,all)
UserEvent: Send an arbitrary event (Priv: user,all)
WaitEvent: Wait for an event to occur (Priv: )
ZapDialOffHook: Dial over channel while offhook (Priv: )
ZapDNDoff: Toggle channel Do Not Disturb status OFF (Priv: )
ZapDNDon: Toggle channel Do Not Disturb status ON (Priv: )
ZapHangup: Hangup Channel (Priv: )
ZapRestart: Fully Restart channels (terminates calls) (Priv: )
ZapShowChannels: Show status channels (Priv: )
ZapTransfer: Transfer Channel (Priv: )

Logoff:註銷管理用戶端。
例子:
發送:
ACTION: LOGOFF
接收:
Response: Goodbye
Message: Thanks for all the fish.

MailboxCount:檢查語音信箱是否有新資訊。
參數:MailBox,actionID
例子:
發送:
Action: MailboxCount
Mailbox: 2001@device
接收:
Response: Success
Message: Mailbox Message Count
Mailbox: 2001@device
NewMessages: 1
OldMessages: 0

MailboxStatus:檢查語音信箱狀態。
參數:MailBox,actionID
例子:
發送:
Action: MailboxStatus
Mailbox: 2001@device
接收:
Response: Success
Message: Mailbox Status
Mailbox: 2001@device
Waiting: 1

Monitor:監聽某個通道。
參數:Channel, File, Format, Mix
例子:
發送:
ACTION: Monitor
Channel: SIP/x7062618529-643d
File: channelsavefile
Mix: 1

返回:
Response: Success
Message: Started monitoring channel

失敗返回
Response: Error
Message: No such channel

Originate:產生一個呼叫。(privilege: call,all)     
參數:
Channel:呼叫連接產生的通道。
Context:呼叫連接所使用的Context(Context,Exten,Priority必須同時使用)
Exten: 呼叫連接所使用的分機(Context,Exten,Priority必須同時使用)

Priority:呼叫連接的優先順序。(Context,Exten,Priority必須同時使用)
TimeOut:呼叫連接的超時(,單位為毫秒,預設為30000毫秒)
CallID:呼叫連接的來電顯示。
Variable:通道變數設置。本地通道和連接通道都將被設置。
Account:呼叫所用密碼。
Application:使用此次呼叫的應用程式。(使用參數和資料)
Data:應用程式參數所使用的資料。
Async:呼叫是否為非同步。(允許多次調用不等待響應生成)
ActionID:請求識別字。 It allows you to identify the response to thisrequest.它允許您確定這一請求的回應資訊。 You may use a number or a string. Useful whenyou make several simultaneous requests.當您有用幾個併發請求,您可以使用一個數位或字串來區分。

事件的順序:首先SGI通道鳴響,然後當他被接聽後,通過上下文呼叫另一端的分機。注意超時只用於呼叫通道,呼叫分機時的超時另外指定。

使用非同步導致1 OriginateResponse事件,包含了失敗的原因。 Reason may be one of the following:原因可能是下列之一:
0 = no such extension or number 0 =沒有這樣的延長或編號
1 = no answer 1 =無應答
4 = answered 4 =回答
8 = congested or not available 8 =擁擠或不可用

例1(從SIP通道呼叫一個分機,這將導致外部呼叫無法產生直到sip通道掛斷)
發送:
Action: Originate
Channel: SIP/101test
Context: default
Exten: 8135551212
Priority: 1
Callerid: 3125551212
Timeout: 30000
Variable: var1=23|var2=24|var3=25
ActionID: ABC45678901234567890

分機8135551212是外呼號碼。這個例子表明了變數的使用方法

例2(從呼出通道呼叫一個本地分機,這將導致該分機不再響鈴直到zap通道掛斷)
發送:
Action: Originate
Channel: Zap/g2/8135551212
Context: default
Exten: 101
Priority: 1
Timeout: 30000
Callerid: 3125551212

101為一個本地分機號。

例3(運行系統命令)
發送:
Action: Originate
Channel: Local/1@dummy
Application: system
Data: /path/to/script

提示:如果你希望通道連接到任意分機和上下文,需要指定一個本地通道

ParkedCalls:設置顯示保持的呼叫清單。
參數:ActionID
發送:
Action: PakedCalls

返回:
Response: Success
Message: Parked calls will follow

Event: ParkedCallsComplete
Ping     
例子:
發送:
Action: ping
返回:
Response: Pong

QueueAdd:添加佇列成員。
參數:
Queue:添加成員的佇列名。
Interface: 成員名(例如sip/2001)
Penalty:數值,值越高越靠後。
Paused:暫停(true)或不暫停(false)
ActionID:(前面已介紹)

例子:
發送:
Action: QueueAdd
Queue: 200
Interface: sip/3001
Penalty: 1
Paused: true
返回:
Response: Success
Message: Added interface to queue

Event: QueueMemberAdded
Privilege: agent,all
Queue: 200
Location: sip/3001
MemberName: sip/3001
Membership: dynamic
Penalty: 1
CallsTaken: 0
LastCall: 0
Status: 5
Paused: 1

QueueRemove:移除佇列成員。
參數:
Queue:移除成員的佇列名。
Interface: 成員名(例如sip/2001)

例子:
發送:
Action: QueueRemove
Queue: 200
Interface: sip/3001
返回:
Response: Success
Message: Removed interface from queue

Event: QueueMemberRemoved
Privilege: agent,all
Queue: 200
Location: sip/3001
MemberName: sip/3001

Queues:返回全部佇列的配置以及電話資料資訊。
例子:
發送:
Action: Queues
返回:
200 has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime), W:0, C:0, A:0, SL:0.0% within 0s
Members:
Local/2002@from-internal/n (Invalid) has taken no calls yet
Local/2003@from-internal/n (Invalid) has taken no calls yet
Local/2001@from-internal/n (Invalid) has taken no calls yet
No Callers

default has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime), W:0, C:0, A:0, SL:0.0% within 0s
No Members
No Callers

QueueStatus:返回全部佇列的電話資料資訊。
例子:
發送:
Action: QueueStatus
返回:
Response: Success
Message: Queue status will follow

Event: QueueParams
Queue: 200
Max: 0
Calls: 0
Holdtime: 0
Completed: 0
Abandoned: 0
ServiceLevel: 0
ServicelevelPerf: 0.0
Weight: 0

Event: QueueMember
Queue: 200
Name: Local/2002@from-internal/n
Location: Local/2002@from-internal/n
Membership: static
Penalty: 0
CallsTaken: 0
LastCall: 0
Status: 4
Paused: 0

Event: QueueMember
Queue: 200
Name: Local/2003@from-internal/n
Location: Local/2003@from-internal/n
Membership: static
Penalty: 0
CallsTaken: 0
LastCall: 0
Status: 4
Paused: 0

Event: QueueMember
Queue: 200
Name: Local/2001@from-internal/n
Location: Local/2001@from-internal/n
Membership: static
Penalty: 0
CallsTaken: 0
LastCall: 0
Status: 4
Paused: 0

Event: QueueParams
Queue: default
Max: 0
Calls: 0
Holdtime: 0
Completed: 0
Abandoned: 0
ServiceLevel: 0
ServicelevelPerf: 0.0
Weight: 0

Event: QueueStatusComplete

Redirect:重定向一個呼叫。
參數:
Channel:重定向的通道。
ExtraChannel:重定向的第二通道(可選)
Exten:重定向的分機。
Context: 重定向的上下文。
Priority:重定向的優先順序
ActionID:(可選)

例子(把兩個呼叫重定向到meetme房間)
Action: Redirect
Channel: Zap/73-1
ExtraChannel: SIP/199testphone-1f3c
Exten: 8600029
Context: default
Priority: 1


8600029的上下文是default,並將進入meetme房間。

SetCDRUserField:設置使用者的呼叫詳細記錄。
參數:UserField,Channel,Append

SetVar:設置通道變數。
參數:Channel, Variable, Value

例子:
發送:
Action: Setvar
Channel: Zap/1-1
Variable: AtestVariable
Value: This is now set

SIPpeers:顯示sip使用者清單。
例子:
發送:
Action: SIPpeers
返回
Response: Success
Message: Peer status list will follow

Event: PeerEntry
Channeltype: SIP
ObjectName: 3001
ChanObjectType: peer
IPaddress: -none-
IPport: 0
Dynamic: yes
Natsupport: yes
VideoSupport: no
ACL: no
Status: UNKNOWN
RealtimeDevice: no

Event: PeerEntry
Channeltype: SIP
ObjectName: 2003
ChanObjectType: peer
IPaddress: -none-
IPport: 0
Dynamic: yes
Natsupport: yes
VideoSupport: no
ACL: no
Status: UNKNOWN
RealtimeDevice: no

Event: PeerEntry
Channeltype: SIP
ObjectName: 2002
ChanObjectType: peer
IPaddress: -none-
IPport: 0
Dynamic: yes
Natsupport: yes
VideoSupport: no
ACL: no
Status: UNKNOWN
RealtimeDevice: no

Event: PeerEntry
Channeltype: SIP
ObjectName: 2001
ChanObjectType: peer
IPaddress: -none-
IPport: 0
Dynamic: yes
Natsupport: yes
VideoSupport: no
ACL: no
Status: UNKNOWN
RealtimeDevice: no

Event: PeerlistComplete
ListItems: 4

Status:返回所有開放通道或指定通道的狀態。
參數:Channel (可選), ActionID (可選)
例子:
發送:
Action:Status

返回:
Response: Success
Message: Channel status will follow

Event: StatusComplete

StopMonitor:停止檢測某通道。
參數:Channel
例子:
發送:
Action: StopMonitor
Channel: Zap/1-1

ZapDialOffhook:從空閒的ZAP通道撥打呼叫。
參數:
ZapChannel: ZAP 通道
Number: 所撥打的號碼

ZapDNDoff:綁定ZAP通道而不影響其關閉狀態
參數:ZAPChannel

ZapDNDon:綁定ZAP通道而不影響其開放狀態
參數:ZAPChannel

ZapHangup:掛斷ZAP通道。
參數:ZAPChannel

ZapTransfer:轉撥ZAP通道。
參數:ZAPChannel

ZapShowChannels:顯示所有ZAP通道資訊。
參數:ActionID(可選)

Asterisk 1.2.1新增:
AgentCallBackLogin:設置使用者以回撥方式登錄。
參數:
Agent:用戶登錄的ID
Exten: 用於回撥的分機
Context: 用於回撥的上下文
AckCall: 設置在用戶回撥時進行驗證
WrapupTime: 掛斷重撥的最小間隔時間
ActionID:

例子:
發送:
Action: AgentCallBackLogin
Agent: 1234
Exten: 1234
Context: myqueues
AckCall: true
WrapupTime: 30
ActionID: 12345

AgentLogoff:註銷一個用戶。
參數:
Agent:用戶登錄Id

例子:
發送:
Action: AgentLogoff
Agent: 1234

Agents:顯示所有使用者資訊。
例子:
發送:
Action: Agents

DBGet:查詢資料庫資訊。
參數:Family,Key

發送:
Action: DBGet
Family:
Key:

失敗返回:
Response: Error
Message: Database entry not found

成功返回:
Response: Success
Message: Result will follow

Event: DBGetResponse
Family:
Key:
Val:

在Perl腳本中的運用:
use Asterisk::Manager;
my $astman = new Asterisk::Manager;
$astman->user('');
$astman->secret('');
$astman->host('');
$astman->connect || die $astman->error . "/n";

$astman->sendcommand(Action => 'DBGet', Family => '', Key => '');
my @result = $astman->sendcommand(Event => 'DBGetResponse');
$astman->disconnect;

my $value = $result[7]; -> Value 7 is the returned Value
DBPut:更新資料庫資訊。
發送:
Action: DBPut
Family:
Key:
Value:  (note, as of Asterisk 1.4 The Manager API wants Val: )

返回:
Response: Success
Message: Updated database successfully

使用方法同DBGet一樣。

QueuePause:停止/開啟佇列中某個成員的使用。
參數:
Queue: 成員所在佇列名.(可選,如無此項將在成員所在的所有佇列停止成員的使用)
Interface: 佇列成員名 (如sip/1000, zap/1-1, agent/1000)
Paused: 停止或開啟 (true or false)

SIPshowPeer:顯示指定peer資訊     
參數: peer:指定的peer。

Asterisk 1.4.0新增:
PlayDTMF:在指定通道上發送雙音多頻信號數位。
參數:
Channel:指定的通道
Digit:雙音多頻信號數位

發送:
Action: PlayDTMF
Channel: SIP/123-e2b2
Digit: 1

成功返回:
Response: Success
Message: DTMF successfully queued

失敗返回:
Response: Error
Message: No such channel

UpdateConfig:更新設定檔。
參數:
SrcFilename: 要讀取的設定檔(.conf)
DstFilename: 要更新的設定檔(.conf)
Reload: 是否重新載入(asterisk或者模組名稱)
Action-XXXXXX: 採取的動作 (NewCat、RenameCat、DelCat、Update、Delete、Append)
Cat-XXXXXX: 操作物件
Var-XXXXXX: 採用的變數
Value-XXXXXX: 變數的值
Match-XXXXXX: 其他匹配資訊

例1:(更新manager.conf)
action: updateconfig
reload: yes
srcfilename: manager.conf
dstfilename: manager.conf
action-000000: append
cat-000000: newuser
var-000000: secret
value-000000: nottelling
例2:(更新extension.conf,通過'>'增加首碼)
action:updateconfig
reload:yes
srcfilename: extensions.conf
dstfilename: extensions.conf
Action-000000:append
Cat-000000: ami-test
Var-000000: exten
Value-000000: >999,1,Dial(SIP/Bob)

例3:(刪除manager.conf中的配置資訊)
action: updateconfig
srcfilename: manager.conf
dstfilename: manager.conf
action-000000: delete
cat-000000: newuser
var-000000: secret
value-000000: nottelling
match-000000: nottelling

刪除時必須設置'value'和'match',不設置'match'將會刪除變數為'var'所有條目

GetConfig:顯示設定檔內容,主要給AJAM和asterisk圖形介面使用。
變數:FileName:顯示的檔案名(.conf)。




Asterisk事件(Event):     
以下的事件都在asterisk源碼中定義。

使用者狀態事件:     

'Agentcallbacklogin' 事件 :
描述:
[來源於chan_agent.c]

示例:
Event: Agentcallbacklogin
Agent:
Loginchan:
Uniqueid:

'Agentcallbacklogoff'事件
描述:
[來源於chan_agent.c]

示例:
Event: Agentcallbacklogoff
Agent:
Loginchan:
Logintime:
Reason: Autologoff
Uniqueid:

Event: Agentcallbacklogoff
Agent:
Loginchan:
Logintime:
Uniqueid:


'AgentCalled'事件
描述:
[來源於 app_queue.c]

示例:
Event: AgentCalled
AgentCalled:
ChannelCalling:
CallerID:
Context:
Extension:
Priority:

'AgentComplete'事件
描述:
[來源於 app_queue.c]

示例:
Event: AgentComplete
Queue:
Uniqueid:
Channel:
Member:
MemberName:
HoldTime:
TalkTime:
Reason:


'AgentConnect'事件
描述:
[來源於 app_queue.c]

示例:
Event: AgentConnect
Queue:
Uniqueid:
Channel:
Member:
MemberName:
Holdtime:
BridgedChannel:


'AgentDump'事件
描述:
[來源於 app_queue.c]

示例:
Event: AgentDump
Queue:
Uniqueid:
Channel:
Member:
MemberName:


'Agentlogin'事件
描述:
[來源於 chan_agent.c]

示例:
Event: Agentlogin
Agent:
Channel:
Uniqueid:


'Agentlogoff'事件
描述:
[來源於 chan_agent.c]

示例:
Event: Agentlogoff
Agent:
Logintime:
Uniqueid:


'QueueMemberAdded'事件
描述:

佇列增加成員動作
[來源於 app_queue.c]

示例:
Queue: testing
Location: Agent/AgentId
Membership: dynamic
Penalty: 0
CallsTaken: 0
LastCall: 0
Status: 4
Paused: 1


'QueueMemberPaused'事件

描述:

回應動作: QueuePause
[來源於 app_queue.c]

示例:
Event: QueueMemberPaused
Location:
MemberName:
Paused:

'QueueMemberStatus'事件
描述:
[來源於 app_queue.c]


Status的值可能為:
/*設備是有效的,但不知道的通道狀態*/
define AST_DEVICE_UNKNOWN 0

/*設備沒有使用 */
define AST_DEVICE_NOT_INUSE 1

/*設備使用中 */
define AST_DEVICE_INUSE 2


/*設備忙 */
define AST_DEVICE_BUSY 3

/*設備無效 */
define AST_DEVICE_INVALID 4

/*設備不可用 */
define AST_DEVICE_UNAVAILABLE 5

/* 設備在響鈴*/
define AST_DEVICE_RINGING 6

/*設備在響鈴並在使用中*/
define AST_DEVICE_RINGINUSE 7

/*設備在等待 */
define AST_DEVICE_ONHOLD 8


示例:
Event: QueueMemberStatus
Queue:
Location:
MemberName:
Membership:
Penalty: CallsTaken:
LastCall:
Status:
Paused:



命令狀態事件:     

呼叫狀態事件:     

'CDR'事件
描述:
[來源於 cdr_manager.c]

必須在 cdr_manager.conf 設定檔中有:

general
enabled = yes

示例:
Event: Cdr
AccountCode:
Source:
Destination:
DestinationContext:
CallerID:
Channel:
DestinationChannel:
LastApplication:
LastData:
StartTime:
AnswerTime:
EndTime:
Duration:
BillableSeconds:
Disposition:
AMAFlags:
UniqueID:
UserField:


'Dial'事件
描述:
[來源於 app_dial.c]

示例:
Event: Dial
Privilege: call,all
Source: Local/900@default-2dbf,2
Destination: SIP/900-4c21
CallerID:
CallerIDName: default
SrcUniqueID: 1149161705.2
DestUniqueID: 1149161705.4


'ExtensionStatus'事件
描述:
[來源於 manager.c]

示例:
Event: ExtensionStatus
Exten:
Context:
Status:


'Hangup'事件
描述:
[來源於 channel.c]

示例:
Event: Hangup
Channel: SIP/101-3f3f
Uniqueid: 1094154427.10
Cause: 0

Cause 代碼:

未分配 = 1
無到達路由 = 2
無路由目的地 = 3
無效通道 = 6
正在通話中 = 7
正常掛機 = 16
用戶忙 = 17
無應答 = 18
無人接聽 = 19
拒絕接聽 = 21
號碼已更改 = 22
目的次序不對 = 27
無效的數位格式 = 28
設備拒絕 = 29
查詢回應 = 30
正常未指定 = 31
正常的呼叫擁塞 = 34
網路狀態差 = 38
正常的臨時故障 = 41
交換機擁塞 = 42
信息過時 = 43
請求通道無效 = 44
被搶佔 = 45
無呼叫保持或呼叫等待 = 50
禁止呼出 = 52
禁止呼入 = 54
負載過重導致失效 = 57
負載過多導致不可達 = 58
負載過多導致為實現 = 65
通道未實現 = 66
設備未實現 = 69
無效的呼叫證明 = 81
不相容的目的地 = 88
無效的不明資訊 = 95
IE流覽器關閉 = 96
不識別的資訊類型 = 97
錯誤資訊 = 98
無IE流覽器 = 99
無效的IE信息 = 100
呼叫狀態錯誤 = 101
計時到達 = 102
強制性IE流覽器長度誤差 = 103
協定錯誤 = 111
互通 = 127
未定義的 = 0


'MusicOnHold'事件
描述:
事件表示電話等待時,播放音樂
示例:

Event: MusicOnHold
Channel:
State:
Uniqueid:


'Join'事件
描述:
[來源於 app_queue.c]

示例:
Event: Join
Channel:
CallerID:
Queue:
Position:
Count:


'Leave'事件
描述:
[來源於 app_queue.c]

示例:
Event: Leave
Channel:
Queue:
Count:


'Link'事件
描述:
當兩個通道已連接後開始交換聲音資料時發生'link'事件

示例:

Event: Link
Channel1: SIP/101-3f3f
Channel2: Zap/2-1
Uniqueid1: 1094154427.10
Uniqueid2: 1094154427.11


'MeetmeJoin'事件
描述:
[來源於 app_meetme.c]

示例:
Event: MeetmeJoin
Channel:
Uniqueid:
Meetme:
Usernum:


'MeetmeLeave'事件
描述:
[來源於 app_meetme.c]

示例:
Event: MeetmeLeave
Channel:
Uniqueid:
Meetme:
Usernum:


'MeetmeStopTalking'事件
描述:
[來源於 app_meetme.c]

注意:
這要求在meetme應用中以選中T選

示例:

Event: MeetmeStopTalking
Privilege: call,all
Channel: SIP/200-ABC1
Uniqueid: 1234567890.1
Meetme: 400
Usernum: 2



'MeetmeTalking'事件
描述:
[來源於 app_meetme.c]

注意:
這要求在meetme應用中以選中T選項

示例:

Event: MeetmeTalking
Privilege: call,all
Channel: SIP/200-ABC1
Uniqueid: 1234567890.1
Meetme: 400
Usernum: 2


'MessageWaiting'事件
描述:
[來源於 app_voicemail.c]

示例:
Event: MessageWaiting
Mailbox: @
Waiting:
New:
Old:

Event: MessageWaiting
Mailbox:
Waiting:


'Newcallerid'事件
描述:
[來源於 channel.c]

示例:
Event: Newcallerid
Channel:
Callerid:
Uniqueid:


'Newchannel'事件
描述:
[來源於 channel.c]

示例:
Event: Newchannel
Channel: Zap/2-1
State: Rsrvd
Callerid:
Uniqueid: 1094154427.11

Event: Newchannel
Channel: SIP/101-3f3f
State: Ring
Callerid: 101
Uniqueid: 1094154427.10


'Newexten'事件
描述:
當一項PBX函數運行(例如執行撥號規則)時發生此事件。

示例:
Event: Newexten
Channel: SIP/101-00c7
Context: macro-ext
Extension: s
Priority: 3
Application: Goto
AppData: s-BUSY
Uniqueid: 1094154321.8

Event: Newexten
Channel: SIP/101-3f3f
Context: local_extensions
Extension: 917070
Priority: 1
Application: AGI
AppData: /etc/asterisk/agi/ks_doorman_pickup.py|channel_up
Uniqueid: 1094154427.10

Event: Newexten
Channel: SIP/101-3f3f
Context: local_extensions
Extension: 917070
Priority: 2
Application: Dial
AppData: Zap/G1/17070
Uniqueid: 1094154427.10


'ParkedCall'事件
描述:
[來源於 res_features.c]

示例:
Event: ParkedCall
Exten: Channel:
From:
Timeout:
CallerID:


'Rename'事件
描述:
[來源於 channel.c: channel 'rename' event]

示例:
Event: Rename
Oldname:
Newname:
Uniqueid:


'SetCDRUserField'事件
描述:
[來源於 app_setcdruserfield.c]

示例:


'Unlink'事件
描述:

當兩個連接的通道斷開是發生此事件,通常為掛機時

示例:

Event: Unlink
Channel1: SIP/101-3f3f
Channel2: Zap/2-1
Uniqueid1: 1094154427.10
Uniqueid2: 1094154427.11


'UnParkedCall'事件
描述:
[來源於 res_features.c]


日誌狀態事件:     

系統狀態事件:     

'Alarm'事件:
描述:
[來源於 chan_zap.c]

示例:
Event: Alarm
Alarm:<(Red|Yellow|Blue|No|Unknown)Alarm|Recovering|Loopback|Not Open|None>
Channel:


'AlarmClear'事件:
描述:
[來源於 chan_zap.c]

示例:
Event: AlarmClear
Channel:


'DNDState'事件:
描述:
[來源於 chan_dahdi.c]

示例:
Event: DNDState
Channel: Zap/1
Status:


'LogChannel'事件
描述:
[來源於 logger.c]

示例:
Event: LogChannel
Channel: /var/log/asterisk/messages
Enabled: Yes

Event: LogChannel
Channel: /var/log/asterisk/messages
Enabled: No
Reason: 13 - Permission denied



'PeerStatus'事件
描述:
當用戶註冊或註銷時發生此事件
[來源於 chan_sip.c, chan_iax2.c]

示例:
Event: PeerStatus
Peer: SIP/2005
PeerStatus: Registered

Event: PeerStatus
Peer: SIP/2005
PeerStatus: Unregistered
Cause: Expired

Event: PeerStatus
Peer: IAX2/2007
PeerStatus:
Time: 1000



'Registry'事件
描述:

當應戶註冊時發生的事件
[來源於 chan_sip.c, chan_iax2.c]

示例:
Event: Registry
Channel: SIP
Domain: sip.domain
Status: Registered


'Reload'事件
描述:
當 "RELOAD" 命令執行時發生此事件
[來源於 manager.c]

示例:
Event: Reload
Message: Reload Requested


'Shutdown'事件
描述:
[來源於 asterisk.c]

示例:
Event: Shutdown
Shutdown:
Restart: [tr]



User Status事件:     

'UserEvent'事件
描述:
[來源於 app_userevent.c]

示例:
Event:
Channel:
Uniqueid:

Event:
Channel:
Uniqueid:



詳細狀態事件:   
'Newstate'事件
示例:
Event: Newstate
Channel: Zap/2-1
State: Dialing
Callerid: 101
Uniqueid: 1094154427.11

Event: Newstate
Channel: Zap/2-1
State: Up
Callerid: 101
Uniqueid: 1094154427.11


'ParkedCallsComplete:'事件
描述:
發生在ParkedCalls事件之後
示例:
Event: ParkedCallsComplete


'QueueParams:'事件
描述:
回應Queues動作的事件
示例:
Event: QueueParams
Queue: sales
Max: 0
Calls: 0
Holdtime: 0
Completed: 0
Abandoned: 0
ServiceLevel: 0
ServicelevelPerf: 0.0


'QueueMember'事件
描述:
回應Queues動作並且佇列中有成員是發生的事件。
示例:
Event: QueueMember
Queue: sales
Location: SIP/101
Membership: dynamic
Penalty: 0
CallsTaken: 0
LastCall: 0


'QueueStatusEnd'事件
描述:
回應Queues動作表示輸出結束。
示例:
Event: QueueStatusEnd


'Status'事件
示例:
Event: Status
Channel: Zap/2-1
CallerID: 101
Account:
State: Up
Link: SIP/101-5cf0
Uniqueid: 1094166088.26

Event: Status
Channel: SIP/101-5cf0

CallerID: 101
Account:
State: Up
Context: local_extensions
Extension: 917070
Priority: 2
Seconds: 11
Link: Zap/2-1
Uniqueid: 1094166088.25

'StatusComplete'事件
描述:
回應Status動作的事件,表示狀態輸出結束。
示例:
Event: StatusComplete


'ZapShowChannels'事件
描述:
回應ZapShowChannels動作的事件。
示例:
Event: ZapShowChannels
Channel: 2
Signalling: FXS Kewlstart
Context: pstn_menu
Alarm: No Alarm


'ZapShowChannelsComplete事件
描述:
回應ZapShowChannels動作的事件,表示輸出結束。
示例:
Event: ZapShowChannelsComplete



動作回應(Response)     
格式:
Response: ( Success或Error)
Message: (顯示資訊)

示例:
無動作或輸入有誤:
Response: Error
Message: Missing action in request

未知命令:
Response: Error
Message: Invalid/unknown command

無許可權:
Response: Error
Message: Permission denied

成功:
Response: Success
Message: Zap channel status will follow

文章內容參考:http://182.92.241.109/index.php/post/149.html

apache 增加不同port(端口、埠)

vim /etc/httpd/conf/httpd.conf

1.新增監聽的port(端口)
Listen 80 (預設)
Listen 8080

2.新增網站位置
<VirtualHost *:8080>
    DocumentRoot "/var/www/html/website"
    ServerName localhost

<Directory "/var/www/html/website">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
</VirtualHost>

3.重新啟動apach
service httpd restart

2018年7月13日 星期五

mysql忘記密碼重設

停用mysql
/etc/init.d/mysql stop

用下列方法啟動mysql可以跳過驗證
mysqld_safe --skip-grant-tables

不用輸入密碼直接進入
mysql -u root

接著修改密碼
mysql> use mysql;
mysql> UPDATE user SET Password=PASSWORD("password") WHERE User='root';
mysql> flush privileges;
mysql> quit


最後重新啟動mysql
/etc/init.d/mysql stop
/etc/init.d/mysql start

修改Apache(PHP)寫入權限、檔案上傳權限

查看當前啟動和運行apache的用戶
ps -ef | grep httpd


修改擁有者及權限
chown -R apache /var/www/website/upload

chmod -R 755 /var/www/website/upload


備註:
不建議用777有安全性問題

3 組數字, 分別是代表 owner (7); 群組用戶 (5); 其他用戶 (5)
7 = 4 (讀) + 2 (寫) + 1 (執行)
5 = 4 (讀) + 1 (執行)
4 = 4 (讀)

2018年7月3日 星期二

正則、正規表示式 /i, /g

正規表示式/i, /g, /ig, /gi, /m的區別

/i (忽略大小寫)
/g (找出所有匹配的字)
/m (多行查找)
/gi (全部查找並忽略大小寫)
/ig (全部查找並忽略大小寫)

修改 Apache、PHP 上傳檔案最大限制

開啟php.ini

file_uploads = on ;允許http檔案上傳
upload_max_filesize = xxM ;允許上傳檔案的最大值
post_max_size = xxM ;POST可以傳送的最大值

memory_limit = xxM ;每個PHP頁面所吃掉的最大記憶體

存檔後重新動服務
service apache2 restart

PHP Notice: undefined index 完美解決方式

平時用$_GET[‘xx’] 取得參數值時,如果之前不加判斷在未傳進參數時會出現這樣的警告:

PHP Notice: undefined index xxx

雖然可以通過設置錯誤顯示方式來隱藏這個提示,但是這樣也有隱患,就是在伺服器的日誌中會記錄這些提示,導致日誌檔異常龐大!
下面是引用網上流行的解決方法:

首先,這個不是錯誤,是warning。所以如果伺服器不能改,每個變數使用前應當先定義。

方法1:伺服器配置修改(可以改的話優)
修改php.ini設定檔,error_reporting = E_ALL & ~E_NOTICE

方法2:對變數進行初始化,規範書寫(比較煩瑣,因為有大量的變數)。但還沒有找到好定義方法,望大家指教

方法3:每個檔頭部加上:error_reporting(0); 如果不行,只有打開php.ini,找到display_errors,設置為display_errors = Off。以後任何錯誤都不會提示。

方法4 :做判斷:isset($_GET["page"]) if-else判斷

或者加上'@'表示這行如果有錯誤或是警告不要輸出
如:@$page=$_GET["page"]

方法5:file1.php檔把$xx變數付一個值,用post 傳遞給file2.php,
如果file2.php沒有$xx的定義,而直接使用$yy=$xx; 系統就會報錯:"undifined variaable $xx", 如果file2.php的檔開始用$xx="";定義,那麼file1.php的$xx值就傳不過來了!

file2.php裡可以這樣
if(!isset($xx)) $xx="";

但Jones認為,這些方法都不太方便。你不妨這樣解決:

定義一個函數:
function _get($str){
    $val = !empty($_GET[$str]) ? $_GET[$str] : null;
    return $val;
}
然後在用的時候,直接用 _get('str') 代替 $_GET['str']


另一篇提到

方法一(優)
在$_GET['變數']前加入@

@$_GET['test']

方法二
isset($_GET['變數'])前加入一個if判斷

if(isset($_GET['變數']))
{
   ....
}

資料來源:http://shuai.be/archives/php-undefined-index/
資料來源:http://allenplay.blogspot.tw/2011/04/undefined-index-php.html

2018年7月2日 星期一

解決json_encode中文變成unicode

使用 json_encode 時,中文都會變成 unicode,解決有兩種方法:

1. 先用urlencode將中文編碼,json_encode之後再用 urldecode
urldecode(json_encode(urlencode("中文字串")));

2. 如果使用的PHP是5.4以上的版本,可以增加一個參數:JSON_UNESCAPED_UNICODE
json_encode("中文字串", JSON_UNESCAPED_UNICODE);

2018年6月14日 星期四

Apache 設定虛擬目錄

打開httpd.conf
# vim /etc/httpd/conf/httpd.conf

在文件加入
Alias /book "/store/book" //虛擬目錄"/book",實際路徑"/store/book"
<Directory "/store/book">       //定義目錄訪問權限
Options Indexes FollowSymLinks  //Indexes:可以瀏覽目錄
AllowOverride None        //固定格式
Require all granted       //開放所有人訪問
</Directory>

最後重新啟動httpd

用 yum 安裝 elastix

用VMware掛載ISO檔
# mount -o /dev/cdrom /mnt/cdrom

cd /mnt/iso

設定repo
# vi /etc/yum.repos.d/elastix-cd.repo

[elastix-cd]
name=Elastix RPM Repo CD
baseurl=file:///mnt/cdrom/
gpgcheck=0
enabled=1

install Elastix
# yum -y install elastix

reboot
# reboot

修改資料庫管理員密碼
# /etc/rc3.d/S66elastix-firstboot start

若沒此指令進/etc/amportal.conf查資料庫密碼進入mysql>直接變更密碼

reboot again
#reboot

Elastix 首頁的認證
以預設帳號/密碼登入 admin/palosanto


順便記錄下,用yum安裝Elastix是為了要升級資料庫到MariaDB 10.X
因為移除MariaDB會連Elastix都移除只好重裝
裝好後要設定資料庫模式不然設定時會出錯
mysql指令設定
SET @@GLOBAL.sql_mode='';
SET @@SESSION.sql_mode='';

直接改my.cnf重開機後設定才不會跑掉
[mysqld]
sql_mode=

2018年6月13日 星期三

PHP 顯示錯誤訊息、錯誤行數(display_errors)

在程式錯誤時能夠在網頁上顯示錯誤訊息
對於debug是很有幫助

查了網路上的方法
說是可以在PHP裡加一行 ini_set('display_errors', 'On');

但我試了就是沒效(可能哪裡弄錯吧??)
最後直接改php.ini的設定

vi /etc/php.ini

找到 display_errors = Off 改成 display_errors = On

/etc/init.d/httpd restart

最後重整網頁就OK了

PHP PDO連接,提示"Unknown MySQL server host '127.0.0.1:3307"

因為需求MariaDB的連接port改為3307
一支程式在測試Server運作正常,但換到另一台卻出現以下錯誤

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host '192.168.0.5:3307'

後來參考這篇PDO_MYSQL DSN
把連線字串由

$PDOConn = new PDO('mysql:host=192.168.0.5:3307;dbname=testdb', user, password);

改成以下這樣就正常了

$PDOConn = new PDO('mysql:host=192.168.0.5;port=3307;dbname=testdb', user, password);

不知道是不是PHP版本不同的關係

2018年6月11日 星期一

處理mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication

想用PHP自己寫一個功能撈cdr的記錄,但連接Elastix2.5的資料庫時發生下面的錯誤

mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication. Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD(‘your_existing_password’). This will store a new, and more secure, hash value in mysql.user. If this user is used in other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords flag from your my.cnf file

上網查了一下這是mysql密碼編碼是舊版的關係
使用下列sql查出所有帳號密碼長度
select user, length(password) from mysql.user;
如果清單列出來長度41是新編碼方式,16是舊編碼方式

再來使用下列sql變更密碼格式
set old_passwords=FALSE;
set password for 'root'@'%' = password('XXXX');
flush privileges;

之後更新頁面應該可以了

2018年6月10日 星期日

vi指令說明

學linux的話,vi幾乎是必用的
網路上有篇詳細的指令,紀錄一下方便我這個菜鳥以後查詢
資料參考來源:http://www2.nsysu.edu.tw/csmlab/unix/vi_command.htm

.vi 的操作模式
==============
        vi 提供兩種操作模式:輸入模式(insert mode)和指令模式(command mode)
        。當使用者進入 vi 後,即處在指令模式下,此刻鍵入之任何字元皆被視為
        指令。在此模式下可進行刪除、修改等動作。若要輸入資料,則需進入輸入
        模式。
.輸入模式
=========
        如何進入輸入模式
                a (append)      由游標之後加入資料。
                A               由該行之末加入資料。
                i (insert)      由游標之前加入資料。
                I               由該行之首加入資料。
                o (open)        新增一行於該行之下供輸入資料之用。
                O               新增一行於該行之上供輸入資料之用。
        如何離開輸入模式
                《ESC》 結束輸入模式。
.指令模式
=========
游標之移動
        h       向左移一個字元。
        j       向上移一個字元。
        k       向下移一個字元。
        l       向右移一個字元。
        0       移至該行之首
        $       移至該行之末。
        ^       移至該行的第一個字元處。
        H       移至視窗的第一列。
        M       移至視窗的中間那列。
        L       移至視窗的最後一列。
        G       移至該檔案的最後一列。
        +       移至下一列的第一個字元處。
        -       移至上一列的第一個字元處。
        (       移至該句之首。 (註一)
        )       移至該句之末。
        {       移至該段落之首。 (註二)
        }       移至該段落之末。
        nG      移至該檔案的第 n 列。
        n+      移至游標所在位置之後的第 n 列。
        n-      移至游標所在位置之前的第 n 列。
        <Ctrl><g>       會顯示該行之行號、檔案名稱、檔案中最末行之行號、游標
                        所在行號佔總行號之百分比。
        註一:句子(sentence)在vi中是指以『!』、『.』或『?』結束的一串字。
        註二:段落(paragraph)在vi中是指以空白行隔開的文字。
.視窗的移動
===========
        <Ctrl><f>       視窗往下捲一頁。
        <Ctrl><b>       視窗往上捲一頁。
        <Ctrl><d>       視窗往下捲半頁。
        <Ctrl><u>       視窗往上捲半頁。
        <Ctrl><e>       視窗往下捲一行。
        <Ctrl><y>       視窗往上捲一行。
.刪除、複製及修改指令介紹 (此單元較少使用)
=========================
        d(delete)、c(change)和y(yank)這一類的指令在 vi 中的指令格式為:
        Operator + Scope = command
        (運算子)   (範圍)
        運算子:
        d       刪除指令。刪除資料,但會將刪除資料複製到記憶體緩衝區。
        y       將資料(字組、行列、句子或段落)複製到緩衝區。
        p       放置(put)指令,與 d 和 y 配和使用。可將最後delete或yank的資
                料放置於游標所在位置之行列下。
        c       修改(change)指令,類似delete與insert的組和。刪除一個字組、句
                子等之資料,並插入新鍵資料。
        範圍:
        e       由游標所在位置至該字串的最後一個字元。
        w       由游標所在位置至下一個字串的第一個字元。
        b       由游標所在位置至前一個字串的第一個字元。
        $       由游標所在位置至該行的最後一個字元。
        0       由游標所在位置至該行的第一個字元。
        )       由游標所在位置至下一個句子的第一個字元。
        (       由游標所在位置至該句子的第一個字元。
        {       由游標所在位置至該段落的最後一個字元。
        }       由游標所在位置至該段落的第一個字元。
        整行動作
        dd      刪除整行。
        D       以行為單位,刪除游標後之所有字元。
        cc      修改整行的內容。
        yy      yank整行,使游標所在該行複製到記憶體緩衝區。
.刪除與修改
===========
        x       刪除游標所在該字元。
        X       刪除游標所在之前一字元。
        dd      刪除游標所在該行。
        r       用接於此指令之後的字元取代(replace)游標所在字元。
                如: ra 將游標所在字元以 a 取代之。
        R       進入取代狀態,直到《ESC》為止。
        s       刪除游標所在之字元,並進入輸入模式直到《ESC》。
        S       刪除游標所在之該行資料,並進入輸入模式直到《ESC》。
.搬移與複製
==========
        利用 delete 及 put 指令可完成資料搬移之目的。
        利用 yank 及 put 指令可完成資料複製之目的。
        yank 和 delete 可將指定的資料複製到記憶體緩衝區,而藉由 put 指令
        可將緩衝區內的資料複製到螢幕上。
        例:
        搬移一行        ‧在該行執行 dd
                        ‧游標移至目的地
                        ‧執行 p
        複製一行        ‧在該行執行 yy
                        ‧游標移至目的地
                        ‧執行 p
.指令重複
=========
        在指令模式中,可在指令前面加入一數字 n,則此指令動作會重複執行 n
        次。
        例:
        刪除10行                ‧10dd
        複製10行                ‧10yy
                                ‧游標移至目的地
                                ‧p
        指標往下移10行  ‧10j
.取消前一動作(Undo)
===================
        即復原執行上一指令前的內容。
        u       恢復最後一個指令之前的結果。
        U       恢復游標該行之所有改變。
.搜尋
=====
        在vi中可搜尋某一字串,使游標移至該處。
        /字串           往游標之後尋找該字串。
        ?字串           往游標之前尋找該字串。
        n               往下繼續尋找下一個相同的字串。
        N               往上繼續尋找下一個相同的字串。
.資料的連接
===========
        J       句子的連接。將游標所在之下一行連接至游標該行的後面。
        若某行資料太長亦可將其分成兩行,只要將游標移至分開點,進入輸入模式
        (可利用 a、i等指令)再按《Enter》即可。
.環境的設定
===========
        :set nu                設定資料的行號。
        :set nonu              取消行號設定。
        :set ai                自動內縮。
        :set noai              取消自動內縮。
        自動內縮(automatic indentation)
        在編輯文件或程式時,有時會遇到需要內縮的狀況,『:set ai』即提供自
        動內縮的功能,用下例解釋之:
        ‧vi test
        ‧(進入編輯視窗後)
          this is the test for auto indent
          《Tab》start indent           ← :set ai (設自動內縮)
          《Tab》data
          《Tab》data
          《Tab》data                   ← :set noai (取消自動內縮)
        the end of auto indent.
        ‧註:<Ctrl><d> 可刪除《Tab》字元。
.ex指令
=======
        讀寫資料
        :w                     將緩衝區的資料寫入磁碟中。
        :10,20w test   將第10行至第20行的資料寫入test檔案。
        :10,20w>>test  將第10行至第20行的資料加在test檔案之後。
        :r test                將test檔案的資料讀入編輯緩衝區的最後。
        刪除、複製及搬移
        :10,20d                刪除第10行至第20行的資料。
        :10d                   刪除第10行的資料。
        :%d                    刪除整個編輯緩衝區。
        :10,20co30             將第10行至第20行的資料複製至第30行之後。
        :10,20mo30             將第10行至第20行的資料搬移至第30行之後。
        字串搜尋與取代
        s(substitute)指令可搜尋某行列範圍。
        g(global)指令則可搜尋整個編輯緩衝區的資料。
        s指令以第一個滿足該條件的字串為其取代的對象,若該行有數個滿足該條
        件的字串,也僅能取代第一個,若想取代所有的字串則需加上g參數。
        :1,$s/old/new/g                將檔案中所有的『old』改成『new』。
        :10,20s/^/   /         將第10行至第20行資料的最前面插入5個空白。
        :%s/old/new/g          將編輯緩衝區中所有的『old』改成『new』。
.恢復編輯時被中斷的檔案
=======================
        在編輯過程中,若系統當掉或連線中斷,而緩衝區的資料並還未
        被寫回磁碟時,當再度回到系統,執行下列指令即可回復中斷前
        的檔案內容。
        %vi -r filename
.編輯多個檔案
=============
        vi亦提供同時編輯多個檔案的功能,方法如下:
        %vi file1 file2 ..
        當第一個檔案編修完成後,可利用『:w』將該緩衝區存檔,而後
        再利用 『:n』載入下一個檔案。

CentOS 7 安裝 PHP 7.x

1.更新軟體來源:
# wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
# rpm -Uvh remi-release-7.rpm
# rpm -Uvh epel-release-latest-7.noarch.rpm

2.指定 PHP 安裝版本(7.0、7.1、7.2)
# yum-config-manager --enable remi-php70
# yum-config-manager --enable remi-php71
# yum-config-manager --enable remi-php72

若用yum-config-manager出現command not found
可先用 yum search 查詢在那個套件再安裝
# yum search yum-config-manager
# yum install yum-utils

3.安裝PHP7.X
# yum install -y php php-mysqlnd php-pdo php-xml php-pear php-devel php-mbstring re2c gcc-c++ gcc

4.檢查PHP版本
# php -v

2018年6月9日 星期六

CentOS 更換MySQL 安裝MariaDB 10.x

1.移除 MySQL 伺服器
先停止MySQL運作。
service mysqld stop

移除所有MySQL相關套件
yum remove mysql* mysql-server mysql-devel mysql-libs -y


2.準備安裝 MariaDB
新增MariaDB repo
vi /etc/yum.repos.d/mariadb.repo

32位元系統
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3.7/centos7-x86/
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

64位元系統
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3.7/centos7-amd64/
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

baseurl = http://... 可自行尋找要安裝的版本更改路徑

3.更新CentOS
yum update -y


4.開始安裝 MariaDB
yum install MariaDB-devel MariaDB-client MariaDB-server -y

啟動Mariadb
service mysql start

設定開機啟動MariaDB。
chkconfig mysql on


5.初始化 MariaDB
/usr/bin/mysql_secure_installation
Enter current password for root (enter for none):
default none press enter

Change the root password? [Y/n]
Y:set password

Remove anonymous users? [Y/n]
Y

Disallow root login remotely? [Y/n]
if want root login remotely press n

Remove test database and access to it? [Y/n]
Y

Reload privilege tables now? [Y/n]
Y:reload

6.設定root權限 
mysql -u root -p
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
%:全開; 192.168.0.%允許192.168.0.0~225
FLUSH PRIVILEGES; (使權限表生效)

2018年6月8日 星期五

AsteriskNow 13 安裝後後續設定

之前IPPBX都是用Elastix 2.5,但由於Elastix之後改和3CX合作改為不開源了,剛好最近有新案子要做會結合IPPBX,所以改裝AsteriskNOW來試試,安裝不難省過介紹,後續有些samba及連接資料庫的需求,所以記錄一下步驟

1.設定IP、getway、DNS
ifconfig eth0 192.168.0.55 netmask 255.255.255.0
vi /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
BOOTPROTO=static
BROADCAST=192.168.0.255
IPADDR=192.168.0.55
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
ONBOOT=yes

vi /etc/resolv.conf
search
nameserver 8.8.8.8 (first)
nameserver 8.8.4.4 (second)

/etc/init.d/network restart


2.安裝samba
yum install samba

vi /etc/samba/smb.conf
[www]
path = /var/www/html
comment = www
browseable = yes
writable = yes
security = user

pdbedit -a -u root

/etc/init.d/smb restart
/etc/init.d/nmb restart

3.設定mysql權限
可遠端連線mysql
1.edit my.cnf
vim /etc/my.cnf
bind-address = 0.0.0.0  (modify 127.0.0.1 to 0.0.0.0)

/etc/init.d/mysqld restart

2.setting iptables(若要更安全可設定只允許特定IP)
開放網段
iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 3306 -j ACCEPT
192.168.0.0/16=192.168.0.0-192.168.255.255
192.168.0.0/24=192.168.0.0-192.168.0.255
或開放特定IP
iptables -A INPUT -p tcp -s 192.168.0.5 --dport 3306 -j ACCEPT
其他封鎖
iptables -A INPUT -p tcp --dport 3306 -j DROP

/etc/init.d/iptables save
/etc/init.d/iptables restart

3.remote access mysql
開放外部IP登入
GRANT ALL PRIVILEGES ON *.* TO username@'192.168.0.%' IDENTIFIED BY 'password';
設定下面才能用phpMyAdmin登入
GRANT ALL PRIVILEGES ON *.* TO username@'127.0.0.1' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO username@'localhost' IDENTIFIED BY 'password';

FLUSH PRIVILEGES; (使權限表生效)


4.安裝phpadmin
yum install phpadmin

下載php相對應的版本,直接將檔案放到/var/www/html

開啟遠端連線mysql port:3306

1.edit my.cnf
vim /etc/my.cnf
bind-address = 0.0.0.0  (modify 127.0.0.1 to 0.0.0.0)

/etc/init.d/mysqld restart

3.setting iptables
開放特定網域或IP
iptables -A INPUT -s 192.168.0.0/24 -m tcp -p tcp --dport 3306 -j ACCEPT
其他IP封鎖
iptables -A INPUT -p tcp --dport 3306 -j DROP

/etc/init.d/iptables save
/etc/init.d/iptables restart


2.connect mysql
GRANT ALL PRIVILEGES ON *.* TO username@'192.168.0.%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES; (使權限表生效)

在Linux更改 IP / netmask / geteway / DNS

1. 修改 IP address
A. ifconfig eth0 192.168.0.55 netmask 255.255.255.0

B. vi /etc/sysconfig/network-scripts/ifcfg-eth0

    DEVICE=eth0
    BOOTPROTO=static (or dhcp)
    BROADCAST=192.168.0.255
    IPADDR=192.168.0.55
    NETMASK=255.255.255.0
    GATEWAY=192.168.0.1
    ONBOOT=yes

查看結果
ifconfig -a eth0


2. 修改 default gateway

A. route add default gw 192.168.0.1

B. vi /etc/sysconfig/network-scripts/ifcfg-eth0
   GATEWAY=192.168.0.1


3. 修改 DNS

vi /etc/resolv.conf
search
nameserver 8.8.8.8 (first)
nameserver 8.8.4.4 (second)


4. 重新啟動網路服務
/etc/init.d/network restart

ifdown eth0;ifup eth0 (針對網卡停用啟動)

2018年5月22日 星期二

[PHP]date()函式 時區設定 取得現在時間

PHP當要取得現在時間時用date()函式

echo date('Y/m/d h:i:s');

可取得目前時間,但會發現時間不對
所以必須先設定時區

date_default_timezone_set("Asia/Taipei");

這樣就會正常顯示所在時區的時間了


日期時間格式(以下列出常用的)
Y:四位數年份 2018
y:兩位數年份 18
m:顯示月份,前面會補零,例 05
n:顯示月份,前面不補零,例 5
d:顯示日期,前面會補零,例 01
j:顯示日期,前面不補零,例 1
h:顯示小時(12小時制),前面會補零,例14點顯示02
H:顯示小時(24小時制),前面會補零,例14點顯示14
i:顯示分鐘,00~59
s:顯示秒數,00~59

2018年1月9日 星期二

Asterisk SIP Trunk 設定

之前都是用遠傳的,最近有機會接觸到亞太電信,兩個註冊方式不一樣,一個是使用帳號密碼,一個是用IP認證,記錄一下兩種註冊方式

A. 帳號密碼
Outgoing Setting:
Trunk Name: sip_trunk

PEER Details:
username=77771111
type=friend
secret=XXX
qualify=yes
port=5060
insecure=very
host=ip_address

Register String:
77771111:xxx@ip_address:5060/77771111

B.IP認證
Outgoing Setting:
Trunk Name: sip_trunk
PEER Details:
fromuser=0277771111 <=指定撥出的顯示號碼
host=ip_address
type=friend
context=from-trunk
insecure=port,invite
qualify=yes
nat=yes
dtmfmode=rfc2833
disallow=all
allow=g711&ulaw&alaw

Register String:空白

遠傳在設定時沒甚麼問題,但亞太在註冊時遇到一些問題,一個是Status(用sip show peers查)會顯示Unmonitored,而且撥打時沒有撥號音,對方會響但卻聽不到聲音,查詢後發現是NAT的部分要修改
在sip_nat.conf加入
externip=ip_address(外部IP)
localnet=192.168.0.0/255.255.255.0(內網IP)
若沒固定IP可以使用DDNS
externhost=www.mydomain.com

設定完後要重啟服務生效
使用 sip show settings 檢查有無被套用