John Cahill
2012-Feb-06 15:31 UTC
[asterisk-users] Script to automatically update externip. Useful for a host with dynamic public IP
#!/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
John Cahill
2012-Feb-06 15:47 UTC
[asterisk-users] Script to automatically update externip. Useful for a host with dynamic public IP
Note: You'll probably have to change /etc/asterisk/sip_general_custom.conf
to /etc/asterisk/sip.conf in the script depending on your set-up.
----- Original Message -----
From: "John Cahill" <john at dmcip.com>
To: "Asterisk Users Mailing" <asterisk-users at
lists.digium.com>
Sent: Monday, 6 February, 2012 3:31:23 PM
Subject: [asterisk-users] Script to automatically update externip. Useful for a
host with dynamic public IP
#!/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
--
_____________________________________________________________________
-- 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
--
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
Raj Mathur (राज माथुर)
2012-Feb-07 03:18 UTC
[asterisk-users] Script to automatically update externip. Useful for a host with dynamic public IP
On Monday 06 Feb 2012, John Cahill wrote:> 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.tmpYou could also do something like: sed -i -e "s/^externip *=.*/externip = $EXTERNIP/" /etc/asterisk/sip.conf Apologies for the wrapped code. Regards, -- Raj -- Raj Mathur || raju at kandalaya.org || GPG: http://otheronepercent.blogspot.com || http://kandalaya.org || CC68 It is the mind that moves || http://schizoid.in || D17F
Klaverstyn, David C
2012-Feb-07 03:35 UTC
[asterisk-users] Script to automatically update externip. Useful for a host with dynamic public IP
To me it would be simpler to use externhost instead of externip and then use a dynamic DNS service. It has worked flawlessly for me for many years. Regards David. -----Original Message----- From: asterisk-users-bounces at lists.digium.com [mailto:asterisk-users-bounces at lists.digium.com] On Behalf Of Raj Mathur (??? ?????) Sent: Tuesday, 7 February 2012 1:19 PM To: asterisk-users at lists.digium.com Subject: Re: [asterisk-users] Script to automatically update externip. Useful for a host with dynamic public IP On Monday 06 Feb 2012, John Cahill wrote:> 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.tmpYou could also do something like: sed -i -e "s/^externip *=.*/externip = $EXTERNIP/" /etc/asterisk/sip.conf Apologies for the wrapped code. Regards, -- Raj -- Raj Mathur || raju at kandalaya.org || GPG: http://otheronepercent.blogspot.com || http://kandalaya.org || CC68 It is the mind that moves || http://schizoid.in || D17F -- _____________________________________________________________________ -- 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