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.

One Response to Asterisk: Automatically Update externip When Your IP Address Changes

  1. cure_o1 says:

    Hey Eric:

    Nice script,

    There is another site where you can get your external IP easily.

    curl ifconfig.me

    It returns just a single line in which you can see your public IP in plain text.
    You can explore this page using your browser and see what options you have.

    Best regards.

Leave a Reply

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