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: http://www.php.net/manual/en/curl.examples-basic.php
Here is the PHP script:
#!/usr/bin/php <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://getip.krisk.org"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($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.