System()
For Asterisk >=1.2
Synopsis
Execute a system (Linux shell) commandDescription
System(command) - System command aloneSystem(command arg1 arg2 etc) - Pass in some arguments
System(command|args) - Use the standard asterisk syntax to pass in arguments
Technical Info
Executes a command by using system().System() passes the string unaltered to system(3). Running "man 3 system" will show exactly what system(3) does:
system() executes a command specified in string by calling
/bin/sh -c string, and returns after the command has been completed.
Therefore System(command arg1 arg2 etc) can be used to pass along arguments.
Return codes
System(command): Executes a command by using system(). If the command fails, the console should report a fallthrough.Result of execution is returned in the SYSTEMSTATUS channel variable:
FAILURE Could not execute the specified command
SUCCESS Specified command successfully executed
NOTE - not documented, but can also return APPERROR
As far as I can understand it will return APPERROR when the command exits with a non-zero return-value.
Old behaviour:
If the command itself executes but is in error, and if there exists a priority n + 101, where 'n' is the priority of the current instance, then the channel will be setup to continue at that priority level. Note that this jump functionality has been deprecated and will only occur if the global priority jumping option is enabled in extensions.conf (priorityjumping=yes).
app_backticks (3rd party addition)
Execute a shell command and save the result as a variable; available as both application and function from here. A patch to use this application with the new module system in Asterisk 1.4+ can be found hereExample 1
exten => s,1,system(echo "${DATETIME} - ${CALLERID} - ${CHANNEL}" >> /var/log/asterisk/calls)Example 2
; dial 700 = Restart Asterisk exten => 700,1,Playback(posix-restarting) ; "Restarting asterisk" exten => 700,2,Wait(1) exten => 700,3,System(/usr/sbin/asterisk -rx reload) exten => 700,4,Hangup
Example 3
; Extension 200 Mini Call ID WinPopup Exampleexten => 200,1,NoOp(${CALLERID} ${DATETIME})
exten => 200,2,System(/bin/echo -e "'Incoming Call From: ${CALLERID} \\r Received: ${DATETIME}'"|/usr/bin/smbclient -M target_netbiosname)
exten => 200,3,Dial,sip/sipuser|30|t
exten => 200,4,Congestion
Example 4
; Dump call info to a serial receipt printer on ttyS1exten => 200,1,NoOp(${CALLERID} ${DATETIME})
exten => 200,2,System(/bin/echo "'${CALLERID} ${DATETIME}'" > /dev/ttyS1)
exten => 200,3,Dial,sip/sipuser|30|t
exten => 200,4,Congestion
Security
My associate received a call from PG&E and realized the horror of System()
Problem
Running System() has access to any program the user running asterisk has access toCALLERID can be set by a remote user
Example
exten => 200,1,Set(CALLERID(NAME)=PG&/bin/echo BADIDEA > /ROOTED.txt)exten => 200,2,System(/bin/echo -e "'Incoming Call From: ${CALLERID} \\r Received: ${DATETIME}'"|/usr/bin/smbclient -M target_netbiosname)
Measure 1
By using perl I was able to prevent the first example but I'm sure the possibility is still there:exten => 200,1,Set(CALLERID(NAME)=PG&/bin/echo BADIDEA > /ROOTED.txt)
exten => 200,2,System(/bin/perl -e 'print Incoming Call From:" . "${CALLERIDNAME}" | /usr/bin/smbclient -M target_netbiosname)
Measure 2
Fine tune this to your liking:
[macro-smb-cid]
exten => s,1,NoOp
exten => s,2,Set(regx="([a-zA-Z0-9]+)")
exten => s,3,Set(CCIDNAME=$["${CALLERIDNAME}": ${regx}])
exten => s,4,Set(regx="([0-9]+)")
exten => s,5,Set(CCIDNUM=$["${CALLERIDNUM}": ${regx}])
exten => s,6,TrySystem(/usr/bin/perl -e \'print "${DATETIME} " . " ${CCIDNAME} " . " \<${CCIDNUM}\>" \' | /usr/bin/smbclient -M target_netbiosname)
Update!! Asterisk-1.4 returns 0 (as it should) when your regex matches. Instead use the function FILTER.
This example is experimental...
[macro-smb-cid]
exten => s,1,NoOp
exten => s,2,Set(FLTNA=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz)
exten => s,3,Set(CIDNA=${FILTER(${FLTNA},${CALLERID(NAME)})})
exten => s,4,Set(FLTNU=(0123456789)
exten => s,5,Set(CIDNU=${FILTER(${FLTNU},${CALLERID(num)})})
exten => s,6,TrySystem(/usr/bin/perl -e \'print "${DATETIME} " . " ${CCIDNAME} " . " \<${CCIDNUM}\>" \' | /usr/bin/smbclient -M target_netbiosname)
I'd like to hear from you if you know another way... cwidger@gmail.com
See also
- Asterisk cmd TrySystem: Unlike System() returns 0 on any situation
- Asterisk cmd Exec
- Asterisk cmd ExecIf
- AGI: Asterisk Gateway Interface
- Call notification: Example for generating pop-up messages
Asterisk | Applications | Functions | Variables | Expressions | Asterisk FAQ
Page Changes
Re: System() after hangup
On a more general note, the APPERROR value of SYSTEMSTATUS is returned by the TrySystem command, when the requested program is executed ok, but has a non-zero return code, according to core show function TrySystem in Asterisk 1.4
System() after hangup
exten => s,n,Dial(Zap/g1/${ARG1}|120|g)
exten => s,n,System(/usr/scripts/asterisk_etl.php ${UNIQUEID})
Any help would be greatly appreciated.
What?!
Command has disappeared