Articles

Bami: A Proof of concept Asterisk Manager Interface (AMI) client written in bash for the Asterisk PBX


Receiving and Sending events from the Asterisk PBX in a shell script

In "The Asterisk Manager Interface (AMI) Protocol" I explained the inners of the AMI protocol, and talked about actions, events, and responses.

As a complement of that article I wrote (just for fun) a little shell script called Bami, which stands for Bash Asterisk Manager Interface, which will give you a very quick start on actually doing/testing stuff. Moreover, it might even be useful from time to time! The complete source code is available at github.

But remember, this is just an example and a script written for fun. If you are looking for a more serious piece of work, I encourage you to take a look at PAMI (which is written in PHP), and Nami (in Javascript, for Nodejs).

Configuring and Running Bami with your Asterisk PBX information

Edit bami.sh and setup log path, and AMI username and password. You will also need something like Netcat. Example:

Logging the communication with the Asterisk PBX

Doing a tail -f of the configured logfile will show something like:


Using NetCat to Send and Receive Asterisk Events

Using netcat allow us to communicate through a TCP/IP connection transparently. So the idea is to use netcat to connect to AMI and link the standard input and standard error (stdin and stdout) to this socket (connection).

We're going to read line by line, delimited by \r\n, so we're going to use bash read. Since End-Of-Message delimiter ir \r\n\r\n, this will result in read returning an empty line. So we use this as a sign that a complete message has been read.

We need to login, this means to send the Login action. We'll use bash printf for that, so we can properly write the needed \r\n's.

Once logged in, events start to show up, so we have to read them (once again, reading line by line, and stopping once the eom -empty line- is detected).

Then we can easily recognice the name of the event that has arrived, with a simple bash case.

I decided to stop there, because I think the point is made. So any optimizations and improvements (and actual real features) are an excercise to the reader :) (i.e: sending an action and receiving the response when having events associated to that response, etc).


Log the communication with your PBX

First, the logging functions. We'll use these across Bami to log messages to a file:

Great, nothing special there. Let's now take a look at how to read line by line and a complete PDU (Protocol Data Unit, I felt like calling it this way, it should rather be just "data" or "message"):

Now, the code needed to actually send an action, in this case, Login

Now, the main reading loop, which reads event and acts accordingly:


Ready to start building an Operator Panel for your Asterisk PBX?

As you can see, AMI is very easy to work with. It has it falls, but being a text protocol with a very simple spec (maybe too simple.. ) will allow you to actually do a client in whatever language you'd like.