Asterisk, and other worldly endeavours.

A blog by Leif Madsen

Posts Tagged ‘curl

Using #exec to set externaddr in sip.conf

Today I was working on a system, and knowing that the system is going to get moved, and that often one of the things forgotten is to update the externaddr= option in sip.conf (when Asterisk is sitting behind NAT), I decided to put together a little script that returns the external IP address of the system. Using this script along with an #exec in the sip.conf file will make it so the address gets updated when the system is moved to the new physical location. I used the php5-curl package on Ubuntu. I used the example from this page for cURL:

Here is the PHP script:


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);

echo "externaddr=".$output."\n";


And the Asterisk configuration required asterisk.conf to be modified. Enable the execincludes=yes option, and then add the following to the [general] section of sip.conf.

#exec /etc/asterisk/scripts/set_externaddr.php

Written by Leif Madsen

2011/02/27 at 4:57 pm

Posted in Asterisk

Tagged with , , , , , ,

Video of ‘5 Things You Didn’t Know Asterisk Could Do’ Presentation Now Available

Check out the AstriCon website for my presentation that was recorded this year. I did three presentations this year (5 Things You Didn’t Know Asterisk Could Do, Distributed Call Centres, and the Asterisk Project Update with Russell Bryant) but unfortunately the only one I see so far is the 5 Things presentation (which is a very good presentation! :))

The link to the presentations are here: with mine being at the top of the list.

Written by Leif Madsen

2010/12/21 at 10:26 am

HowTo: Read a value from a file, and say it back


Today on the VoIP Users Conference we discussed my request for recipe ideas in order to start developing some additional documentation. Specifically, I’m looking for problems that are simple, common problems that can be solved in the dialplan, and which are good examples of the dialplan language (markup, script, yadda yadda).

One of the suggestions was something posted to the Asterisk mailing list which was pretty straight forward: to read in the contents of a file, where the file contained a number which was the current temperature. This file was created by an external script, and the poster was looking for how to read in the contents of that file, and play back something that would say the number, followed by “degrees”.

The discussion delved into some possible solutions, and delved into the problem with writing Asterisk recipes in general; that there are always several ways to skin the same cat. Below I will mention some of the examples that had come up on the conference call in the Discussion section, and will then show a dialplan (that works on Asterisk 1.4) that will solve the problem proposed in the Solution section.


To read the contents of a file, where the contents will contain a number, that needs to be read back to the caller, followed by the word “degrees”.


exten => start,1,Verbose(2,Saying the current temperature back to channel ${CHANNEL})

; first, lets verify the the file exists for reading in data
exten => start,n,Set(PATH_TO_FILE=/tmp/current_temp.txt)
exten => start,n,Set(FILE_STATUS=${STAT(e,${PATH_TO_FILE})})
exten => start,n,GotoIf($["${FILE_STATUS}" = "" | "${FILE_STATUS}" = "0"]?no_file,1)

; next lets read in the value from the file since it exists
exten => start,n,Set(MAX_FILE_CHARACTERS=256)
exten => start,n,GotoIf($[${ISNULL(${TEMPERATURE})}]?no_file,1)                               ; verify we got a value

; say the temperature
exten => start,n,Playback(silence/1)
exten => start,n,SayNumber(${TEMPERATURE})
exten => start,n,Playback(en/degrees&en/fahrenheit)
exten => start,n,Return()

exten => no_file,1,Verbose(2,File does not exist)
exten => no_file,n,Playback(silence/1)
exten => no_file,n,Playback(en/feature-not-avail-line)
exten => no_file,n,Return()

And then you can setup some sort of test number that will call the subroutine (I’m using GoSub()).

exten => 555,1,Verbose(2,Checking temperature)
exten => 555,n,GoSub(subSayCurrentTemperature,start,1)
exten => 555,n,Hangup()


The solution above is really the “right” way to do it in Asterisk. But with Asterisk, there always seems to be many “right” ways to solve the same solution. Because the problem was to read in the contents of a file and to say the contents back, then the solution I provided certainly solves that problem directly in dialplan without any external scripts, or dropping to the console.

There were several other solutions that could have been provided for this solution since there was an external PHP script that was generating the file and its contents. That script could have done any of the following:

  • Execute a shell command, and write the contents to the Asterisk database:
    asterisk -rx "database put temperature current 60"
  • Write the contents to a relational database such as MySQL and use func_odbc: Set(TEMPERATURE=${ODBC_TEMP()})
  • Setup a webpage and parse it with curl:
  • On Asterisk 1.6, there is the SHELL() function:
    Set(TEMPERATURE=${SHELL(cat /tmp/current_temperature.txt)})

I particularly like the curl solution since it allows the script to lookup the temperature when the CURL() function calls it, which means it can be real time and not delayed like writing to a text file can be.

So like with all things Asterisk, there are always many inventive ways to solve the same solution.

Written by Leif Madsen

2009/07/17 at 12:21 pm

Posted in Uncategorized

Tagged with , , , ,