[Asterisk-Users] Script to update externip for A@H/AMP [was Re: SIP Extension behind NAT, Asterisk on NAT (DMZ)]

Tom Rymes trymes at cascadelinksystems.com
Sun Nov 27 17:36:41 MST 2005


On Nov 26, 2005, at 3:06 PM, Manny A. Wise wrote:

[snip]

> The problem is not updating the FQDN name in dyndns.org..that part is
> working great...the problem now is..how to get the IP change into the
> sip_nat.conf... but I am sure has to be a way... :)

How about this? You have to add back in the first "shebang" line that  
defines /bin/sh as the program to use to run the script.

<<<------------BEGIN SCRIPT------------->>>
# Script to update Asterisk's externip= setting with the current
# IP address. Writes changes to sip_nat.conf for Asterisk at Home/AMP
# This script is only useful if you have a dynamic IP Address and
# are using NAT.

# define where to write temporary files
Tmp=/tmp/externipupdate$$.txt

# define the hostname to lookup
host=myhost.mydomain.dom

# Use dig to get our current IP address (assuming that it has been
# properly updated via DynDNS client or otherwise. Set the variable
# "ip_address" to the value of the IP address.

ip_address=`dig $host +short`

# Write the new settings to a temporary file and then overwrite the
# exisiting /etc/asterisk/sip_nat.conf file with the temporary file
# using the mv command.

echo "nat=yes" > $Tmp
echo "externip=$ip_address" >> $Tmp
# Change the following line to reflect your local network. Add multiple
# localnet= lines if you have more than one local network.
echo "localnet=10.0.0.0/255.255.255.0" >> $Tmp
mv $Tmp /etc/asterisk/sip_nat.conf

# Tell Asterisk to reload SIP to make the changes take effect

/usr/sbin/asterisk -rx "sip reload"
<<<------------------END SCRIPT---------------->>>

That ought to do the trick. I tested it with my A at H config. I will  
leave the task of finding a way to automatically run this program  
when needed as an exercise to the reader. (cron would work if it  
changes at regular time intervals, I suppose)

Keep in mind that there will be a delay between when the address  
changes to when it is updated in DynDNS and then another delay as the  
change propagates throughout the DNS system. Lastly, depending on how  
you call the script, there will be a delay between when it propagates  
through DNS and when this script is run.

Maybe there is a way to use the DynDNS client to get the new IP  
address and write it to sip_nat.conf at the same time it updates the  
DynDNS service? Are there DynDNS clients that allow you to run an  
external program every time the IP changes? Kind of like Comedian  
mail's externnotify parameter?

Tom

--------------------
Tom Rymes
Cascade Link Systems
www.cascadelinksystems.com
(603) 375-1414

"Intelligent technology solutions for small businesses."



More information about the asterisk-users mailing list