execute asterisk cli command C#

断了今生、忘了曾经 提交于 2019-12-04 06:08:24

问题


I need a help with executing asterisk cli commands using C#. I'm able to open terminal window and start asterisk (see the code below), but dont know how to for example execute "sip show peers" command in CLI. Is there any possible way to do it?

using System;
using System.Diagnostics;

namespace runGnomeTerminal
{
    class MainClass
    {
        public static void ExecuteCommand(string command)
        {
            Process proc = new System.Diagnostics.Process ();
            proc.StartInfo.FileName = "/bin/bash";
            proc.StartInfo.Arguments = "-c \" " + command + " \"";
            proc.StartInfo.UseShellExecute = false; 
            proc.StartInfo.RedirectStandardOutput = true;
            proc.Start ();

            while (!proc.StandardOutput.EndOfStream) {
                Console.WriteLine (proc.StandardOutput.ReadLine ());
            }
        }

        public static void Main (string[] args)
        {
            ExecuteCommand("gnome-terminal -x bash -ic 'cd $HOME; sudo asterisk -vvvvr; bash'");

        }
    }
}

回答1:


All you triing to do is overkill. You just need ask asterisk for command, no need in bash or terminal.

Process proc = new System.Diagnostics.Process ();
proc.StartInfo.FileName = "/usr/sbin/asterisk";
proc.StartInfo.Arguments = "-rx \" " + command + " \"";
proc.StartInfo.UseShellExecute = false; 
proc.StartInfo.RedirectStandardOutput = true;
proc.Start ();

But please note, that command start NEW asterisk process, so not optimal. Best way is use asterisk AMI interface(which is just tcp connection, so much less resources use) and issue AMI action COMMAND. There are number of already developed AMI interfaces, including C# one.




回答2:


You can execute asterisk cli commands directly from bash, just -x to the asterisk command. For example ExecuteCommand("gnome-terminal -x bash -ic 'cd $HOME; sudo asterisk -rx "sip show peers"; bash'");.



来源:https://stackoverflow.com/questions/32551425/execute-asterisk-cli-command-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!