func_shell.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2006-2012, Digium, Inc.
  5. *
  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.
  11. *
  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.
  15. */
  16. /*! \file
  17. *
  18. * SHELL function to return the output generated by a command issued to the system shell.
  19. *
  20. * \note Inspiration and Guidance from Russell! Thank You!
  21. *
  22. * \author Brandon Kruse <bkruse@digium.com>
  23. *
  24. * \ingroup functions
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/module.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/utils.h"
  35. #include "asterisk/app.h"
  36. static int shell_helper(struct ast_channel *chan, const char *cmd, char *data,
  37. char *buf, size_t len)
  38. {
  39. int res = 0;
  40. if (ast_strlen_zero(data)) {
  41. ast_log(LOG_WARNING, "Missing Argument! Example: Set(foo=${SHELL(echo \"bar\")})\n");
  42. return -1;
  43. }
  44. if (chan) {
  45. ast_autoservice_start(chan);
  46. }
  47. if (len >= 1) {
  48. FILE *ptr;
  49. char plbuff[4096];
  50. ptr = popen(data, "r");
  51. if (ptr) {
  52. while (fgets(plbuff, sizeof(plbuff), ptr)) {
  53. strncat(buf, plbuff, len - strlen(buf) - 1);
  54. }
  55. pclose(ptr);
  56. } else {
  57. ast_log(LOG_WARNING, "Failed to execute shell command '%s'\n", data);
  58. res = -1;
  59. }
  60. }
  61. if (chan) {
  62. ast_autoservice_stop(chan);
  63. }
  64. return res;
  65. }
  66. /*** DOCUMENTATION
  67. <function name="SHELL" language="en_US">
  68. <synopsis>
  69. Executes a command using the system shell and captures its output.
  70. </synopsis>
  71. <syntax>
  72. <parameter name="command" required="true">
  73. <para>The command that the shell should execute.</para>
  74. </parameter>
  75. </syntax>
  76. <description>
  77. <para>Collects the output generated by a command executed by the system shell</para>
  78. <para>Example: <literal>Set(foo=${SHELL(echo bar)})</literal></para>
  79. <note>
  80. <para>The command supplied to this function will be executed by the
  81. system's shell, typically specified in the SHELL environment variable. There
  82. are many different system shells available with somewhat different behaviors,
  83. so the output generated by this function may vary between platforms.</para>
  84. <para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
  85. is set to <literal>no</literal>, this function can only be executed from the
  86. dialplan, and not directly from external protocols.</para>
  87. </note>
  88. </description>
  89. </function>
  90. ***/
  91. static struct ast_custom_function shell_function = {
  92. .name = "SHELL",
  93. .read = shell_helper,
  94. };
  95. static int unload_module(void)
  96. {
  97. return ast_custom_function_unregister(&shell_function);
  98. }
  99. static int load_module(void)
  100. {
  101. return ast_custom_function_register_escalating(&shell_function, AST_CFE_READ);
  102. }
  103. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Collects the output generated by a command executed by the system shell");