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'
fi
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
Code: Select all
2014-09-22 at 14:30 - 108.161.120.92
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.