0

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

        }
    }
}
user2188452
  • 73
  • 1
  • 8
  • Do you have to feed the input to `asterik` via keyboard input (`stdinput`)? You can use a`StreamReader` of your created process to write input to that proces (`StandardInput` property), don't forget to set `RedirectStandardInput` to `true`. If the commands are given through the shell commandline arguments, you should change your `ExecuteCommand` call. – Maximilian Gerhardt Sep 13 '15 at 15:46

2 Answers2

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.

arheops
  • 15,544
  • 1
  • 21
  • 27
0

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

Sergey S.
  • 171
  • 1
  • 2
  • 7