[asterisk-users] Script to automatically update externip. Useful for a host with dynamic public IP

John Cahill john at dmcip.com
Mon Feb 6 09:31:23 CST 2012


#!/bin/bash
# checksetexternip.sh
# Author: John Cahill email at johncahill.net
# Licence: GPL v3
# Description: script that queries checkip.dyndns.com to find the server's external IP address. Updates asterisk's externip value and does a sip reload if necessary.
# Last modified 06/02/2012

is_ip(){

    input=$1
    octet1=$(echo $input | cut -d "." -f1)
    octet2=$(echo $input | cut -d "." -f2)
    octet3=$(echo $input | cut -d "." -f3)
    octet4=$(echo $input | cut -d "." -f4)
    stat=1

if [[ $input =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] && [ $octet1 -le 255 ] && [ $octet2 -le 255 ] && [ $octet3 -le 255 ] && [ $octet4 -le 255 ];
  then
    stat=0
fi

return  $stat

}

EXTERNIP=`wget -qO- checkip.dyndns.com | awk '{print $6}'| cut -d"<" -f1`
is_ip $EXTERNIP
if [ $? -ne 0 ]
        then
                logger -s "checksetexternip.sh: External IP address invalid or unavailable, exiting."
                exit 1
fi

OLDEXTERNIP=`grep externip /etc/asterisk/sip_general_custom.conf | cut -d"=" -f2`
if [ "$EXTERNIP" = "$OLDEXTERNIP" ]
        then
                logger -s "checksetexternip.sh: External IP address is the same, nothing to do exiting."
                exit 0
        else
                logger -s "checksetexternip.sh: External IP address has changed, changing /etc/asterisk/sip_general_custom.conf"
                grep -v "externip" /etc/asterisk/sip_general_custom.conf > /etc/asterisk/sip_general_custom.conf.tmp
                echo "externip=$EXTERNIP" >> /etc/asterisk/sip_general_custom.conf.tmp
                cp /etc/asterisk/sip_general_custom.conf.tmp /etc/asterisk/sip_general_custom.conf
                rm /etc/asterisk/sip_general_custom.conf.tmp
                logger -s "Doing asterisk -rx "sip reload""
                asterisk -rx "sip reload"
fi

John Cahill 

Systems Engineer 

Services for Asterisk 
Data Messaging & Communications Ltd 
Fourth Floor 
22 Lever St 
Manchester 
M1 1EA 

Email: john at dmcip.com 
Telephone: 0800 862 0181 
Fax: 0161 850 0126 
jabber: gnujunk at jabber.org 






More information about the asterisk-users mailing list