Featured image of post opensips exec模块介绍

opensips exec模块介绍

背景介绍

opensipsexec模块用于执行外部命令,可以用于执行脚本,和上章介绍的kamailioexec模块类似。

官方文档地址: exec,本次测试的opensips版本是:

version: opensips 3.3.10 (x86_64/linux)

配置解析

1
2
3
4
# 是否打开执行脚本来设置环境变量,默认:0
modparam("exec", "setvars", 1)
# 设置最长的执行时间,单位s,默认:0,如果超过这个时间,就会被杀死
modparam("exec", "time_to_kill", 20)

重要函数

exec(command, [stdin], [stdout], [stderr], [envavp])

  • command: 要执行的命令
  • stdin: 输入参数,默认为空
  • stdout: 输出参数,默认为空
  • stderr: 错误输出参数,默认为空
  • envavp: 环境变量,默认为空,环境变量名为: OSIPS_EXEC_#,#为环境变量的索引,从0开始。比如: OSIPS_EXEC_0

async(exec(command)) 可以异步执行命令.

实战

  1. 配置文件:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
loadmodule "exec.so"
modparam("exec","setvars", 1)
modparam("exec","time_to_kill", 20)

... 
route {
    ...
    $avp(env) = "a";
    $avp(env) = "b";
    exec("pwd ", , $var(out), $var(err), $avp(env));
    xlog("The output is $var(out)\n");
    xlog("Received the following error:$var(err)\n");
    ...
}
  1. 输出结果:

    syslog日志:

    1
    2
    
    May 20 11:44:27 [1492] The output is /
    May 20 11:44:27 [1492] Received the following error:<null>
    
本博客已稳定运行
发表了37篇文章 · 总计67.06k字
本站总访问量 次 · 您是本站第 位访问者
粤ICP备2025368587号-1| 使用 Hugo 构建
主题 StackJimmy 设计