2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2006-2012, Digium, Inc.
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
19 * SHELL function to return the output generated by a command issued to the system shell.
21 * \note Inspiration and Guidance from Russell! Thank You!
23 * \author Brandon Kruse <bkruse@digium.com>
29 <support_level>core</support_level>
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include "asterisk/module.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/utils.h"
40 #include "asterisk/app.h"
42 static int shell_helper(struct ast_channel *chan, const char *cmd, char *data,
43 char *buf, size_t len)
47 if (ast_strlen_zero(data)) {
48 ast_log(LOG_WARNING, "Missing Argument! Example: Set(foo=${SHELL(echo \"bar\")})\n");
53 ast_autoservice_start(chan);
60 ptr = popen(data, "r");
62 while (fgets(plbuff, sizeof(plbuff), ptr)) {
63 strncat(buf, plbuff, len - strlen(buf) - 1);
67 ast_log(LOG_WARNING, "Failed to execute shell command '%s'\n", data);
73 ast_autoservice_stop(chan);
80 <function name="SHELL" language="en_US">
82 Executes a command using the system shell and captures its output.
85 <parameter name="command" required="true">
86 <para>The command that the shell should execute.</para>
90 <para>Collects the output generated by a command executed by the system shell</para>
91 <para>Example: <literal>Set(foo=${SHELL(echo \bar\)})</literal></para>
92 <note><para>The command supplied to this function will be executed by the
93 system's shell, typically specified in the SHELL environment variable. There
94 are many different system shells available with somewhat different behaviors,
95 so the output generated by this function may vary between platforms.</para></note>
100 static struct ast_custom_function shell_function = {
102 .read = shell_helper,
105 static int unload_module(void)
107 return ast_custom_function_unregister(&shell_function);
110 static int load_module(void)
112 return ast_custom_function_register(&shell_function);
115 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Collects the output generated by a command executed by the system shell");