2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2006, 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 value of a system call.
21 * \note Inspiration and Guidance from Russell! Thank You!
23 * \author Brandon Kruse <bkruse@digium.com>
30 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
32 #include "asterisk/module.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/utils.h"
36 #include "asterisk/app.h"
38 static int shell_helper(struct ast_channel
*chan
, const char *cmd
, char *data
,
39 char *buf
, size_t len
)
41 if (ast_strlen_zero(data
)) {
42 ast_log(LOG_WARNING
, "Missing Argument! Example: Set(foo=${SHELL(echo \"bar\")})\n");
47 ast_autoservice_start(chan
);
53 ptr
= popen(data
, "r");
54 while (fgets(plbuff
, sizeof(plbuff
), ptr
)) {
55 strncat(buf
, plbuff
, len
- strlen(buf
) - 1);
61 ast_autoservice_stop(chan
);
66 static struct ast_custom_function shell_function
= {
68 .synopsis
= "Executes a command as if you were at a shell.",
69 .syntax
= "SHELL(<command>)",
72 "Returns the value from a system command\n"
73 " Example: Set(foo=${SHELL(echo \"bar\")})\n"
74 " Note: When using the SHELL() dialplan function, your \"SHELL\" is /bin/sh,\n"
75 " which may differ as to the underlying shell, depending upon your production\n"
76 " platform. Also keep in mind that if you are using a common path, you should\n"
77 " be mindful of race conditions that could result from two calls running\n"
78 " SHELL() simultaneously.\n",
81 static int unload_module(void)
83 return ast_custom_function_unregister(&shell_function
);
86 static int load_module(void)
88 return ast_custom_function_register(&shell_function
);
91 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Returns the output of a shell command");