【问题标题】:execute asterisk cli command C#执行星号 cli 命令 C#
【发布时间】:2016-09-21 21:11:10
【问题描述】:

我需要有关使用 C# 执行星号 cli 命令的帮助。我可以打开终端窗口并启动星号(参见下面的代码),但不知道如何在 CLI 中执行“sip show peers”命令。有什么办法吗?

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'");

        }
    }
}

【问题讨论】:

  • 您是否必须通过键盘输入 (stdinput) 将输入提供给 asterik?您可以使用您创建的进程的StreamReader 将输入写入该进程(StandardInput 属性),不要忘记将RedirectStandardInput 设置为true。如果命令是通过 shell 命令行参数给出的,你应该改变你的 ExecuteCommand 调用。

标签: c# asterisk


【解决方案1】:

你试图做的只是矫枉过正。您只需要询问星号即可获得命令,在 bash 或终端中不需要。

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 ();

但请注意,该命令启动新的星号进程,因此不是最佳的。最好的方法是使用星号 AMI 接口(这只是 tcp 连接,资源使用少得多)并发出 AMI 操作命令。 有许多已经开发的 AMI 接口,包括 C# 接口。

【讨论】:

    【解决方案2】:

    您可以直接从 bash 执行星号 cli 命令,只需 -x 到星号命令即可。 例如ExecuteCommand("gnome-terminal -x bash -ic 'cd $HOME; sudo asterisk -rx "sip show peers"; bash'");

    【讨论】:

      猜你喜欢
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      • 2018-11-30
      • 2012-06-05
      • 2017-05-11
      相关资源
      最近更新 更多