func_shell.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2006-2012, Digium, Inc.
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * SHELL function to return the output generated by a command issued to the system shell.
00020  *
00021  * \note Inspiration and Guidance from Russell! Thank You!
00022  *
00023  * \author Brandon Kruse <bkruse@digium.com>
00024  *
00025  * \ingroup functions
00026  */
00027 
00028 /*** MODULEINFO
00029    <support_level>core</support_level>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 403960 $")
00035 
00036 #include "asterisk/module.h"
00037 #include "asterisk/channel.h"
00038 #include "asterisk/pbx.h"
00039 #include "asterisk/utils.h"
00040 #include "asterisk/app.h"
00041 
00042 static int shell_helper(struct ast_channel *chan, const char *cmd, char *data,
00043                                char *buf, size_t len)
00044 {
00045    int res = 0;
00046 
00047    if (ast_strlen_zero(data)) {
00048       ast_log(LOG_WARNING, "Missing Argument!  Example:  Set(foo=${SHELL(echo \"bar\")})\n");
00049       return -1;
00050    }
00051 
00052    if (chan) {
00053       ast_autoservice_start(chan);
00054    }
00055 
00056    if (len >= 1) {
00057       FILE *ptr;
00058       char plbuff[4096];
00059 
00060       ptr = popen(data, "r");
00061       if (ptr) {
00062          while (fgets(plbuff, sizeof(plbuff), ptr)) {
00063             strncat(buf, plbuff, len - strlen(buf) - 1);
00064          }
00065          pclose(ptr);
00066       } else {
00067          ast_log(LOG_WARNING, "Failed to execute shell command '%s'\n", data);
00068          res = -1;
00069       }
00070    }
00071 
00072    if (chan) {
00073       ast_autoservice_stop(chan);
00074    }
00075 
00076    return res;
00077 }
00078 
00079 /*** DOCUMENTATION
00080    <function name="SHELL" language="en_US">
00081       <synopsis>
00082          Executes a command using the system shell and captures its output.
00083       </synopsis>
00084       <syntax>
00085          <parameter name="command" required="true">
00086             <para>The command that the shell should execute.</para>
00087          </parameter>
00088       </syntax>
00089       <description>
00090          <para>Collects the output generated by a command executed by the system shell</para>
00091          <para>Example:  <literal>Set(foo=${SHELL(echo bar)})</literal></para>
00092          <note>
00093             <para>The command supplied to this function will be executed by the
00094             system's shell, typically specified in the SHELL environment variable. There
00095             are many different system shells available with somewhat different behaviors,
00096             so the output generated by this function may vary between platforms.</para>
00097 
00098             <para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
00099             is set to <literal>no</literal>, this function can only be executed from the
00100             dialplan, and not directly from external protocols.</para>
00101          </note>
00102       </description>
00103 
00104    </function>
00105  ***/
00106 static struct ast_custom_function shell_function = {
00107    .name = "SHELL",
00108    .read = shell_helper,
00109 };
00110 
00111 static int unload_module(void)
00112 {
00113    return ast_custom_function_unregister(&shell_function);
00114 }
00115 
00116 static int load_module(void)
00117 {
00118    return ast_custom_function_register_escalating(&shell_function, AST_CFE_READ);
00119 }
00120 
00121 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Collects the output generated by a command executed by the system shell");

Generated on Thu Apr 16 06:27:36 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6