login | register
Sat 05 of Jul, 2008 [03:23 UTC]

voip-info.org

Search with Google
Search this site with Google. Results may not include recent changes.
 
Google Ads
Shoutbox
  • Samuel, Thu 03 of Jul, 2008 [13:41 UTC]: ok thank you
  • Mats Karlsson, Thu 03 of Jul, 2008 [13:37 UTC]: Nice Samuel, will look forward to rad it.
  • bwl_fernstudent, Thu 03 of Jul, 2008 [09:08 UTC]: Your blog shows some usefull code
  • Samuel, Thu 03 of Jul, 2008 [08:04 UTC]: I'll translate it, for sure
  • Mats Karlsson, Wed 02 of Jul, 2008 [20:46 UTC]: LOL, in french! Translate it to English and I will read it.
  • Samuel, Wed 02 of Jul, 2008 [08:07 UTC]: Hello, i wrote a blog about Asterisk, speaking about installation,programming and more http://sambranche.blogspot.com/
  • Nick Barnes, Tue 01 of Jul, 2008 [17:46 UTC]: Steve - Asterisk doesn't 'fit into linux' - it's an application which runs on top of Linux.
  • Steve, Mon 30 of Jun, 2008 [18:07 UTC]: anyone know where I can find a block diagram of how asterisk fits into linux. my f'ing bosses want me to draw something up.. ugh.
  • akbar, Fri 27 of Jun, 2008 [10:37 UTC]: marley_boyz@yahoo.com how to configure call forward, call back, call pick up using TDM and asterisk 1.2.13... please help me.. thx...
  • Matthew Williams, Tue 24 of Jun, 2008 [22:37 UTC]: We are looking for Tier II VoIP Support Technicians in St Louis. Send resumes to mwilliams AT voxitas DOT com.
Server Stats
  • Execution time: 0.45s
  • Memory usage: 2.61MB
  • Database queries: 34
  • GZIP: Disabled
  • Server load: 0.73

Asterisk cmd Backticks

This entry documents a third party addon. This application is not available in the default Asterisk installation. Installation instructions for this addon may be found here

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

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.

Easy Install

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


9356 views strong.

Created by Blumagic, Last modification by ngoho on Tue 11 of Sep, 2007 [20:04 UTC]

Comments Filter

Backticks in debian

by Arie Skliarouk on 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.

Beware of end-of-line II

by Marcus Daniel on 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.

BackTicks for Asterisk 1.4

by jlgomez on 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!

BackTicks for Asterisk 1.4

by ngoho on 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 {
if((argc = ast_app_separate_args(mydata, '|', argv, sizeof(argv) / sizeof(argv0))) < 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");

Beware of end-of-line

by Andrew White on 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";

Please update this page with new information, just login and click on the "Edit" or "Add Comment" button above. Get a free login here: Register Thanks! - support@voip-info.org

Page Changes | Comments

Sponsored by:

Terms of Service Privacy Policy
© 2003-2008 VOIP-Info.org LLC

Powered by bitweaver