将 bash 返回值放入星号拨号计划
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14143481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
geting a bash returned value into asterisk dialplan
提问by user1946171
I am new in asteriskand need help. I have a bash script that i need to run from my dial plan and get the value returned from it and put it into a variable in my dial plan.
我是新来的asterisk,需要帮助。我有一个 bash 脚本,我需要从我的拨号计划中运行它并获取从它返回的值并将其放入我的拨号计划中的一个变量中。
For example my bash script is test.sh:
例如我的 bash 脚本是test.sh:
#! /bin/bash
echo "61"
My dial plan:
我的拨号计划:
exten => 3333,1,Set(result=${shell(/root/test.sh)})
exten => 3333,2,Verbose(result is: ${result})
The script have a 777 Permissions and when I run it manually from the shell command line it is working. I want to run it from dialplan and use the returned value in a variable, I get nothing back ( no value returned )
该脚本具有 777 权限,当我从 shell 命令行手动运行它时,它正在工作。我想从拨号计划运行它并在变量中使用返回的值,我什么也没得到(没有返回值)
My asterisk version is Asterisk 1.6.2.20
我的星号版本是星号 1.6.2.20
回答by arheops
Your dialplan not work, becuase asterisk is running under asterisk user and can't read your script in root directory. To troubleshoot issues with script i recomend stop asterisk and start it in console as
您的拨号计划不起作用,因为 asterisk 在 asterisk 用户下运行并且无法在根目录中读取您的脚本。要解决脚本问题,我建议停止星号并在控制台中启动它
asterisk -vvvgc
That way you will see errors generated by scripts.
这样,您将看到脚本生成的错误。
BUT
但
Since there are no any difference in cpu cost between shell start and agi script start, i recomend you use AGI script.
由于 shell 启动和 agi 脚本启动之间的 CPU 成本没有任何区别,我建议您使用 AGI 脚本。
To do it, you have put your script in /var/lib/asterisk/agi-bin/
为此,您已将脚本放在 /var/lib/asterisk/agi-bin/
#!/bin/bash
echo 'SET VARIABLE result "61" '
Dialplan will be
拨号方案将是
exten => 3333,1,AGI(test.sh)
exten => 3333,2,Verbose(result is: ${result})
p.s best practice is not use scripts at all, becuase starting shell is hi cpu cost operation and it will not scale well. Most of tasks can be done in dialplan, inluding complex checks and database query/update tasks.
ps 最佳实践是根本不使用脚本,因为启动 shell 是高 CPU 成本的操作,并且无法很好地扩展。大多数任务都可以在拨号计划中完成,包括复杂的检查和数据库查询/更新任务。
回答by Igor Chubin
Try to use ${SHELL(date)} to check if it works.
尝试使用 ${SHELL(date)} 来检查它是否有效。
From my extensions.conf:
从我的 extensions.conf:
exten => 4002,1,Answer
exten => 4002,n,Set(RESULT=${SHELL(date)})
exten => 4002,n,NoOp(${RESULT})
exten => 4002,n,Hangup
From asterisk.log after call on 4002:
从 asterisk.log 调用 4002 后:
== Using SIP RTP CoS mark 5
-- Executing [4002@demo2:1] Answer("SIP/100-00000585", "") in new stack
-- Executing [4002@demo2:2] Set("SIP/100-00000585", "RESULT=Thu Jan 3 20:00:08 EET 2013
") in new stack
-- Executing [4002@demo2:3] NoOp("SIP/100-00000585", "Thu Jan 3 20:00:08 EET 2013
") in new stack
-- Executing [4002@demo2:4] Hangup("SIP/100-00000585", "") in new stack
== Spawn extension (demo2, 4002, 4) exited non-zero on 'SIP/100-00000585'*emphasized text*
回答by MichelV69
Do you have SELinux enabled? Often SELinux will block this kind of access to shell scripts.
你启用了 SELinux 吗?通常 SELinux 会阻止这种对 shell 脚本的访问。
回答by Calin Nicolae Necula
Create the script in /etc/asterisk/ or in other folders that asterisk has permisions:
在 /etc/asterisk/ 或其他具有 asterisk 权限的文件夹中创建脚本:
chown asterisk /folder
chown 星号/文件夹
chgrp asterisk /folder
chgrp 星号/文件夹
Also keep in mind that you can just run the command like this:
还要记住,您可以像这样运行命令:
exten => 3333,1,Set(result=${shell(echo 61)})
扩展 => 3333,1,Set(result=${shell(echo 61)})
回答by codewandler
Always also make sure your asterisk user does have a shell.
始终还要确保您的星号用户确实有一个外壳。
cat /etc/passwd | grep asterisk
In my case I had /bin/false for security reasons. So the following command wont work:
就我而言,出于安全原因,我有 /bin/false 。所以下面的命令不起作用:
su asterisk -c 'whoami'

