2.2 Calling "Hello World" from the CLI
In this first example, we create a simple "Hello World" dialplan and call it from the Asterisk console, or CLI (command-line interface).
2.2.1 Configuring Asterisk
After a standard install, you should find these files in the /etc/asterisk directory:
debian:/usr/src# cd /etc/asterisk debian:/etc/asterisk# ls adsi.conf cdr_tds.conf indications.conf privacy.conf adtranvofr.conf codecs.conf logger.conf queues.conf agents.conf dnsmgr.conf manager.conf res_odbc.conf alarmreceiver.conf dundi.conf meetme.conf rpt.conf alsa.conf enum.conf mgcp.conf rtp.conf asterisk.adsi extconfig.conf misdn.conf sip.conf asterisk.conf extensions.ael modem.conf sip_notify.conf cdr.conf extensions.conf modules.conf skinny.conf cdr_custom.conf features.conf musiconhold.conf telcordia-1.adsi cdr_manager.conf festival.conf osp.conf voicemail.conf cdr_odbc.conf iax.conf oss.conf vpb.conf cdr_pgsql.conf iaxprov.conf phone.conf zapata.conf debian:/etc/asterisk#
That's a long list, but don't worry; we care about only one of them for our example: extensions.conf. To keep things simple, we'll move the sample extensions.conf file created by make samples to /var/tmp/asterisk-etc-backup/ (so that we can retrieve it later if required):
debian:/etc/asterisk# mkdir -p /var/tmp/asterisk-etc-backup debian:/etc/asterisk# mv extensions.* /var/tmp/asterisk-etc-backup/ debian:/etc/asterisk#
Using your favorite console text editor 2 enter the following text into /etc/asterisk/extensions.conf:
[default] exten => 1001,1,Answer() exten => 1001,2,Playback(hello-world) exten => 1001,3,Hangup()
2.2.2 Starting Asterisk and Calling "Hello World"
You might be surprised that just four lines are enough to configure Asterisk. Perhaps you thought Asterisk was more complicated than that. In any case, let's give it a try. Start Asterisk with the command asterisk -c (the -c switch gives us the console):
debian:/etc/asterisk# asterisk -c Asterisk 1.4.21, Copyright (C) 1999 - 2008 Digium, Inc. and others. Created by Mark Spencer <markster@digium.com> [...] [ Booting... [ Reading Master Configuration ] [...] Asterisk Ready. *CLI>
With this console, you can operate a running Asterisk server and give it commands interactively and in real time. Let's try generating a call to our "Hello World" extension with console dial 1001 :
*CLI> console dial 1001 *CLI> << Console call has been answered >> << Hangup on console >> *CLI>
The command console dial 1001 calls extension 1001. This extension answers and plays the hello-world.gsm sound file from the /var/lib/asterisk/sounds directory.
2.2.2.1 What Is an Extension?
An extension is a programming unit in a dialplan. Every extension consists of at least one line, written in the following format:
exten => extension_name,priority,application
Here, priority describes the sequence of the individual extension elements. Our extension 1001 has three priorities:
exten => 1001,1,Answer() exten => 1001,2,Playback(hello-world) exten => 1001,3,Hangup()
The applications are self-explanatory:
-
Answer()
Answers and opens a new Asterisk channel (see Appendix B, "Dialplan Applications").
-
Playback(hello-world)
Plays the file hello-world.gsm in the current channel (see Appendix B).
-
Hangup()
Hangs up and closes the channel (see Appendix B).
2.2.2.2 Increasing Verbosity
When you are debugging Asterisk, you'll often find it helpful to increase the verbosity of the console messages. When Asterisk is started with asterisk -c , the verbose level is set to 0 (the allowed range is 0 to 10). You can increase this to level 5 from the console with the command core set verbose 5 , which is a good level to use for debugging:
*CLI> core set verbose 5 Verbosity was 0 and is now 5 *CLI>
Now, when you enter the command console dial 1001 , you see details about the dialplan execution:
*CLI> console dial 1001 == Console is full duplex *CLI> -- Executing [1001@default:1] Answer("Console/dsp", "") in new stack << Console call has been answered >> -- Executing [1001@default:2] Playback("Console/dsp", "hello-world") in new stack -- <Console/dsp> Playing 'hello-world' (language 'en') *CLI> -- Executing [1001@default:3] Hangup("Console/dsp", "") in new stack == Spawn extension (default, 1001, 3) exited non-zero on 'Console/dsp' << Hangup on console >> *CLI>
You can also set the verbose level to 5 at start time with the switch -vvvvv (five v's).
2.2.3 Stopping Asterisk
Enter stop now , and Asterisk stops:
*CLI> stop now debian:/etc/asterisk#