Asterisk, and other worldly endeavours.

A blog by Leif Madsen

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 , , , , , ,

8 Responses

Subscribe to comments with RSS.

  1. Very nice trick.

    While your PHP example is good, this can be done via shell somewhat simpler.


    echo “externaddr=$(curl -s “”)”


    2011/03/01 at 10:20 am

  2. Hi, Ihad a question…I tried with a shell scrpit adding an exten on the extension.conf, but it doesn´t work. I don´t know what is wrong, please help.
    This is the script:

    echo “exten => 777,1,Dial(SIP/777,30)”


    2011/04/12 at 12:36 pm

    • You probably need to enable the ability to use exec in the asterisk.conf file. It is not enabled by default.

      Leif Madsen

      2011/04/12 at 1:17 pm

      • well I do enable it, but still doesn´t work….
        In the asterisk console i can see this warning:
        == Parsing ‘/var/tmp/exec.1302632385316731.1088866624’: == Found
        [Apr 12 13:19:45] WARNING[2758]: config.c:1175 process_text_line: No ‘=’ (equal sign) in line 1 of /var/tmp/exec.1302632385316731.1088866624


        2011/04/12 at 1:19 pm

        • That’s because you haven’t tested your script. You need to escape the parts that bash is choking on:

          echo “exten =\> 777,1,Dial\(SIP/777,30\)”

          Leif Madsen

          2011/04/12 at 1:28 pm

          • hi, i tried scaping the parts, but it still doesn´t work, when I reload asterisk dialplan this warning apears:

            == Parsing ‘/etc/asterisk/extensions.conf’: == Found
            == Parsing ‘/var/tmp/exec.1302638845385145.1120131392’: == Found
            [Apr 12 15:07:25] WARNING[2867]: config.c:1175 process_text_line: No ‘=’ (equal sign) in line 1 of /var/tmp/exec.1302638845385145.1120131392

            I really don´t know what happens.


            2011/04/12 at 3:10 pm

            • I finally have the answer to the problem. The warning was because the directory where my script was saved has to have execute access for the user “asterisk”.
              I tried with the permissions for the script, but then I notice that directory in which the script was saved, has to have execute access for “asterisk”. Then I change the owner of the script and the directory, to asterisk.asterisk and give permissions like this: d–x—— to the directory, and permisions like this: -r-x—— to the script, and problem solved…
              I hope it will help someone else… 😀


              2011/04/25 at 11:49 am

  3. Hey Leif – love the URL :)!

Comments are closed.

%d bloggers like this: