Dynamic IP Script For Asterisk
Posted: 22 Sep 2014, 19:47
I run an Asterisk phone switch at my house which is blessed/plagued with a dynamic IP address. About two weeks ago we had a pretty bad storm here and I suffered the loss of two PC's, two network switches, one DSL modem (which should be called a "media converter), and one network cable... not to mention the roof of the boat being ruined and a tree coming down in the yard. Anyways, I rebuilt some machines to replace the dead ones and Asterisk just happened to be one of the services I had to get running again. I was able to get it working, but in the process found that I had not backed up the script I was using to check my externip setting in my sip.conf and update it if I happened to have a new IP. In short, the setting specifically states what your external (outside of your NAT) IP address is for help with connectivity with your trunk providers... if you have the wrong address in there your phone calls (SIP initiation) may get lost.
I don't remember where I found the script last time, but when I did some searching this time I couldn't find it and was disappointed to see that all the solutions I could find were either too complicated or too simple. So I kind of melted a few of the solutions together in to the following externip.sh script.
confip pulls the IP address from your sip.conf while wanip uses tnx.nl to locate the real external ip. If they differ then an entry like this is made in the externip.log file
and then the sip.conf is updated with the new IP address. Finally it reloads the config file in Asterisk which makes it read the new address. I run this script every ten minutes via cron so I don't really have to think about it. :-)
Suggestions of more elegant ways to do this appreciated.
S.
I don't remember where I found the script last time, but when I did some searching this time I couldn't find it and was disappointed to see that all the solutions I could find were either too complicated or too simple. So I kind of melted a few of the solutions together in to the following externip.sh script.
Code: Select all
#!/bin/bash
confip=`cat /etc/asterisk/sip.conf | grep externip | cut -c 10-`
wanip=`curl -q tnx.nl/ip`
if [ $confip != $wanip ]; then
echo $(date +%Y)-$(date +%m)-$(date +%d) at $(date +%k):$(date +%M) - $wanip >> /path/externip.log
sed -i 's/externip=.*/externip='"$wanip"'/' /etc/asterisk/sip.conf
asterisk -rx 'sip reload'
ficonfip pulls the IP address from your sip.conf while wanip uses tnx.nl to locate the real external ip. If they differ then an entry like this is made in the externip.log file
Code: Select all
2014-09-22 at 14:30 - 108.161.120.92and then the sip.conf is updated with the new IP address. Finally it reloads the config file in Asterisk which makes it read the new address. I run this script every ten minutes via cron so I don't really have to think about it. :-)
Suggestions of more elegant ways to do this appreciated.
S.
