#!/bin/bash
# checksetexternip.sh
# Author: John Cahill [email protected]
# 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: [email protected] 
Telephone: 0800 862 0181 
Fax: 0161 850 0126 
jabber: [email protected] 




--
_____________________________________________________________________
-- Bandwidth and Colocation Provided by http://www.api-digital.com --
New to Asterisk? Join us for a live introductory webinar every Thurs:
               http://www.asterisk.org/hello

asterisk-users mailing list
To UNSUBSCRIBE or update options visit:
   http://lists.digium.com/mailman/listinfo/asterisk-users

Reply via email to