func_shell.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 89425 $")
00031
00032 #include "asterisk/module.h"
00033 #include "asterisk/channel.h"
00034 #include "asterisk/pbx.h"
00035 #include "asterisk/utils.h"
00036 #include "asterisk/app.h"
00037
00038 static int shell_helper(struct ast_channel *chan, const char *cmd, char *data,
00039 char *buf, size_t len)
00040 {
00041 if (ast_strlen_zero(data)) {
00042 ast_log(LOG_WARNING, "Missing Argument! Example: Set(foo=${SHELL(echo \"bar\")})\n");
00043 return -1;
00044 }
00045
00046 if (chan)
00047 ast_autoservice_start(chan);
00048
00049 if (len >= 1) {
00050 FILE *ptr;
00051 char plbuff[4096];
00052
00053 ptr = popen(data, "r");
00054 while (fgets(plbuff, sizeof(plbuff), ptr)) {
00055 strncat(buf, plbuff, len - strlen(buf) - 1);
00056 }
00057 pclose(ptr);
00058 }
00059
00060 if (chan)
00061 ast_autoservice_stop(chan);
00062
00063 return 0;
00064 }
00065
00066 static struct ast_custom_function shell_function = {
00067 .name = "SHELL",
00068 .synopsis = "Executes a command as if you were at a shell.",
00069 .syntax = "SHELL(<command>)",
00070 .read = shell_helper,
00071 .desc =
00072 "Returns the value from a system command\n"
00073 " Example: Set(foo=${SHELL(echo \"bar\")})\n"
00074 " Note: When using the SHELL() dialplan function, your \"SHELL\" is /bin/sh,\n"
00075 " which may differ as to the underlying shell, depending upon your production\n"
00076 " platform. Also keep in mind that if you are using a common path, you should\n"
00077 " be mindful of race conditions that could result from two calls running\n"
00078 " SHELL() simultaneously.\n",
00079 };
00080
00081 static int unload_module(void)
00082 {
00083 return ast_custom_function_unregister(&shell_function);
00084 }
00085
00086 static int load_module(void)
00087 {
00088 return ast_custom_function_register(&shell_function);
00089 }
00090
00091 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Returns the output of a shell command");
00092