I need to invoke a shell call from Asterisk dialplan in order to log some data to a text file, so I tried this:
Set(log=[${STRFTIME(${EPOCH},,%Y-%m-%d %H:%M:%S)}] [${UNIQUEID}] ${ARG1})
SYSTEM(echo "${log}" >> /var/log/asterisk/log.txt)
(No assumptions can be made about the contents of ARG1
. It may contain single quotes, double quotes, ampersands, or any other characters.)
This seemed to work, but I recently found calls that had &
in their caller ID were being dropped as soon as they entered the switch because the function crashed and the call dropped.
I tried replacing double quotes in the echo
statement with single quotes, but if the caller ID name contains a single quote, the function will not crash but it does not echo anything at all.
Here is my dilemma:
ARG1
is variable per call, and could be literally anything. As such, this isn't a static string where I can simply escape quotes.The variable log itself does not contain any variables when expanded. Is there any way here to make the echoing work as desired?
Why do you think you "cannot escape anything?" Put the string in single quotes, and all you need to escape is single quotes. This could be done with the REPLACE
function.
Set(log=[${STRFTIME(${EPOCH},,%Y-%m-%d %H:%M:%S)}] [${UNIQUEID}] ${REPLACE(${ARG1},','"'"')})
SYSTEM(echo '${log}' >> /var/log/asterisk/log.txt)
But I think the cleaner solution is to use the FILE
function instead.
FILE(/var/log/asterisk/log.txt,,,a)=${log}