login | register
Sat 04 of Sep, 2010 [02:59 UTC]

voip-info.org

History

Asterisk cmd Backticks

Created by: Blumagic,Last modification on Thu 24 of Jun, 2010 [17:33 UTC] by JustRumours

Synopsis:

 Execute a shell command and save the result as a variable.

Description of application:

 Backticks(<VARNAME>|<command>)
 
 <VARNAME> - The name of the variable to be set.
 <command> - The shell command to execute; be sure to provide the full path to the command.  I used sh, but I think any language will do.

Description of function:

 Backticks(<command>)
 
 <command> - Same as above.

Notes

  • *CLI> show application BACKTICKS
  • *CLI> show function BACKTICKS

This application is not distributed as part of Asterisk.
S

Return value

Returns the resulting string.

Example

  • application:
exten => s,1,BACKTICKS(FOO|/your/path/command.sh) ; sets FOO to result, see here for more info

  • function:
exten => s,1,Set(FOO=${BACKTICKS(/your/path/command.sh ${arg1} ${arg2})}) ; sets FOO to result, I only could get the "function" to take arguments


  • command.sh:
#!/bin/sh
x=0
if [ -e "/some/path/$1/caller-$2.ulaw" ]; then x=1; fi /* see man test for info on the if argument */
echo $x

The above takes two args, checks for the existence of the file, if it exists return 1, else return 0; i.e. the resulting value of foo.

install

Download from You can download it from http://www.freeswitch.org/asterisk_stuff/app_backticks.c
Follow his install instructions except:
  1. you may want to save app_backticks.c to /usr/src/asterisk
  2. for his step 3, cd /usr/src/asterisk first and then execute /usr/src/asterisk/contrib/scripts/astxs -install app_backticks.c

See also


24607 views strong.



Comments

Comments Filter
222

333Updated version for Asterisk 1.4.24.1

by indreias, Thursday 14 of May, 2009 [13:36:51 UTC]
Based on the ported version made by ngoho I have updated Backticks application for Asterisk 1.4.24.1 (at least this was the version used on our PBX).
The details (in Romanian) could be found here: http://forum.modulo.ro/jforum/posts/list/78.page - go to the 3rd message in the mentioned thread in order to have access to the code (app_backticks.c.1.4)
222

333Backticks in debian

by skliarie, Wednesday 11 of June, 2008 [15:27:46 UTC]
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=485805
hopefully will be included in asterisk base package.

222

333Beware of end-of-line II

by danielm, Thursday 03 of April, 2008 [22:23:37 UTC]
The previous poster gave a perl example. If you use the shell you can use "echo -n " to supress a newline. With awk you can use {ORS=""} to set the output field seperator to nothing instead of the default newline.
222

333BackTicks for Asterisk 1.4

by jlgomez, Tuesday 22 of January, 2008 [12:47:05 UTC]
Hello ngoho.
Can you put your code in a file? because this site modify the code.
I`m using asterisk 1.4 and app_backticks don`t compile. The compile error is:
make: *** No rule to make target `apps_env'. Stop.
-I/usr/src/asterisk-1.4.17/ -I/usr/src/asterisk-1.4.17//include
-c app_backticks.c -o app_backticks.o
sh: -/: invalid option
Usage: sh GNU long option option ...

Thanks!
222

333BackTicks for Asterisk 1.4

by ngoho, Tuesday 11 of September, 2007 [20:09:07 UTC]
I've ported BackTicks to 1.4. Here the code

/*
* backticks Application For Asterisk
*
* Copyright (c) 2004-2007 Anthony Minessale II <anthmct@yahoo.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*
* ported to 1.4 by Hoai-Anh Ngo-Vi <hoaianh@web.de>
*
*
*/

  1. include <stdio.h>
  2. include <asterisk/file.h>
  3. include <asterisk/logger.h>
  4. include <asterisk/channel.h>
  5. include <asterisk/pbx.h>
  6. include <asterisk/module.h>
  7. include <asterisk/lock.h>
  8. include <asterisk/app.h>
  9. include <stdlib.h>
  10. include <unistd.h>
  11. include <string.h>

static char *tdesc = "backticks";
static char *app = "BackTicks";
static char *synopsis = "Execute a shell command and save the result as a variable.";
static char *desc = ""
" Backticks(<VARNAME>|<command>)\n\n"
"Be sure to include a full path!\n"

;

//STANDARD_LOCAL_USER;

//LOCAL_USER_DECL;

static char *do_backticks(char *command, char *buf, size_t len)
{
int fds2, pid = 0;
char *ret = NULL;

memset(buf, 0, len);

if (pipe(fds)) {
ast_log(LOG_WARNING, "Pipe/Exec failed\n");
} else { /* good to go*/

pid = fork();

if (pid < 0) { /* ok maybe not */
ast_log(LOG_WARNING, "Fork failed\n");
close(fds0);
close(fds1);
} else if (pid) { /* parent */
close(fds1);
read(fds0, buf, len);
close(fds0);
ret = buf;
} else { /* child */
char *argv255 = {0};
int argc = 0;
char *p;
char *mycmd = ast_strdupa(command);

close(fds0);
dup2(fds1, STDOUT_FILENO);
argvargc++ = mycmd;

do {
if p = strchr(mycmd, ' ') {
*p = '\0';
mycmd = ++p;
argvargc++ = mycmd;
}
} while (p);

close(fds1);
execv(argv0, argv);
/* DoH! */
ast_log(LOG_ERROR, "exec of %s failed\n", argv0);
exit(0);
}
}

return buf;
}

static int backticks_exec(struct ast_channel *chan, void *data)
{
int res = 0;
struct ast_module_user *u;
const char *usage = "Usage: Backticks(<VARNAME>|<command>)";
char buf1024, *argv2, *mydata;
int argc = 0;


if (!data) {
ast_log(LOG_WARNING, "%s\n", usage);
return -1;
}


u = ast_module_user_add(chan);
/* Do our thing here */

if (!(mydata = ast_strdupa(data))) {
ast_log(LOG_ERROR, "Memory Error!\n");
res = -1;
} else {
ifargc = ast_app_separate_args(mydata, ') < 2) {
ast_log(LOG_WARNING, "%s\n", usage);
res = -1;
}

if (do_backticks(argv1, buf, sizeof(buf))) {
pbx_builtin_setvar_helper(chan, argv0, buf);
} else {
ast_log(LOG_WARNING, "No Data!\n");
res = -1;
}
}
ast_module_user_remove(u);
return res;
}


static char *function_backticks(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *ret = NULL;

if (do_backticks(data, buf, len)) {
ret = buf;
}

return ret;
}

static struct ast_custom_function backticks_function = {
.name = "BACKTICKS",
.desc = "Executes a shell command and evaluates to the result.",
.syntax = "BACKTICKS(<command>)",
.synopsis = "Executes a shell command.",
.read = function_backticks
};


int unload_module(void)
{
int res;
res = ast_custom_function_unregister(&backticks_function);
res |= ast_unregister_application(app);
ast_module_user_hangup_all();

return res;
}

int load_module(void)
{
int res;

res = ast_custom_function_register(&backticks_function);
res |= ast_register_application(app, backticks_exec, synopsis, desc);

return res;
}

char *description(void)
{
return tdesc;
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Third Party BackTicks() application");

222

333Beware of end-of-line

by ajw, Monday 30 of October, 2006 [22:26:48 UTC]
Note that the variable set by BACKTICKS will pick up any carriage returns! So if you want the Asterisk variable FOO to be set to BAR, in Perl for example make sure you output print "BAR"; and NOT print "BAR\n";