Asterisk: Automatically Update externip When Your IP Address Changes

I got tired of updating Asterisk’s externip setting every time my IP address changed, so I wrote this quick script, saved as /usr/local/bin/asterisk_update_externip:

#!/bin/bash

ip_url="http://automation.whatismyip.com/n09230945.asp"

oldip=`grep externip /etc/asterisk/sip.conf |sed 's/;.*//' |grep -v ^$ |sed s/.*=\ *//`
ip=`curl -s "$ip_url" |head -n 1`

if [ "$oldip" != "$ip" ]
then
        #echo "Updating IP"
        sed -i "s/externip = $oldip/externip = $ip/" /etc/asterisk/sip.conf
        asterisk -r -x "core reload"

fi

I put it in /etc/cron.d/asterisk-externip as follows:

# Update Asterisk's SIP External IP
3,8,13,18,23,28,33,38,43,48,53,58 * * * * root asterisk_update_externip

Enjoy!

This entry was posted in sysadmin. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>