Dynamic IP Script For Asterisk

Tips and Tricks for Networking

Moderator: jkerr82508

User avatar
Snorkasaurus
Berserk
Posts: 587
Joined: 30 Dec 2013, 19:19
Contact:

Dynamic IP Script For Asterisk

Postby Snorkasaurus » 22 Sep 2014, 19:47

I run an Asterisk phone switch at my house which is blessed/plagued with a dynamic IP address. About two weeks ago we had a pretty bad storm here and I suffered the loss of two PC's, two network switches, one DSL modem (which should be called a "media converter), and one network cable... not to mention the roof of the boat being ruined and a tree coming down in the yard. Anyways, I rebuilt some machines to replace the dead ones and Asterisk just happened to be one of the services I had to get running again. I was able to get it working, but in the process found that I had not backed up the script I was using to check my externip setting in my sip.conf and update it if I happened to have a new IP. In short, the setting specifically states what your external (outside of your NAT) IP address is for help with connectivity with your trunk providers... if you have the wrong address in there your phone calls (SIP initiation) may get lost.

I don't remember where I found the script last time, but when I did some searching this time I couldn't find it and was disappointed to see that all the solutions I could find were either too complicated or too simple. So I kind of melted a few of the solutions together in to the following externip.sh script.

Code: Select all

#!/bin/bash
confip=`cat /etc/asterisk/sip.conf  | grep externip | cut -c 10-`
wanip=`curl -q tnx.nl/ip`
if [ $confip != $wanip ]; then
  echo $(date +%Y)-$(date +%m)-$(date +%d) at $(date +%k):$(date +%M) - $wanip >> /path/externip.log
  sed -i 's/externip=.*/externip='"$wanip"'/' /etc/asterisk/sip.conf
  asterisk -rx 'sip reload'
fi

confip pulls the IP address from your sip.conf while wanip uses tnx.nl to locate the real external ip. If they differ then an entry like this is made in the externip.log file

Code: Select all

2014-09-22 at 14:30 - 108.161.120.92

and then the sip.conf is updated with the new IP address. Finally it reloads the config file in Asterisk which makes it read the new address. I run this script every ten minutes via cron so I don't really have to think about it. :-)

Suggestions of more elegant ways to do this appreciated.

S.

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Dynamic IP Script For Asterisk

Postby viking60 » 23 Sep 2014, 10:53

You could use another page:

Code: Select all

#!/bin/bash
confip=`cat /etc/asterisk/sip.conf  | grep externip | cut -c 10-`
wanip=`curl http://myexternalip.com/raw `
if [ $confip != $wanip ]; then
  echo $(date +%Y)-$(date +%m)-$(date +%d) at $(date +%k):$(date +%M) - $wanip >> /path/externip.log
  sed -i 's/externip=.*/externip='"$wanip"'/' /etc/asterisk/sip.conf
  asterisk -rx 'sip reload'
fi

This will result in a clean external IP.

Code: Select all

curl -q tnx.nl/ip 
gives me an "percent" extension:
89.233.39.172%
It might be due to me using DNScrypt :confused

To avoid this garbage you can also simply add an echo to your command:

Code: Select all

wanip=`curl -q tnx.nl/ip ;echo`


Which gives the clean:

89.233.39.172


I don't know anything about Asterix though - except that he is a friend of Obelix..... :-D

Looks interesting though :think: So far I have learned that Asterisk is the Apache of communications applications. +1

PS: All IP's here are fakes.
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
Snorkasaurus
Berserk
Posts: 587
Joined: 30 Dec 2013, 19:19
Contact:

Re: Dynamic IP Script For Asterisk

Postby Snorkasaurus » 23 Sep 2014, 13:07

viking60 wrote:

Code: Select all

curl -q tnx.nl/ip 
gives me an "percent" extension:
89.233.39.172%

Weird... I don't seem to get the % sign from them. I have noticed in the past that "what is my ip" web sites will not last forever and it is occasionally necessary to find a new one. I really should build some error checking in to the script so that if tnx.nl is dead I won't be screwing up my externip setting.
viking60 wrote:I don't know anything about Asterix though - except that he is a friend of Obelix..... :-D

Looks interesting though :think: So far I have learned that Asterisk is the Apache of communications applications. +1

Asterisk is freakin' awesome. I have two trunks (VoIP phone lines) one of which is free to call most places in Canada and the other is 0.5 cents per minute to anywhere in North America with pretty sweet rates in the rest of the world too. If the number I am calling is not on the list of "free places" on my normal provider I just dial 9 first and then the number to use my second provider. I have a blacklist setup that plays a "donkey noise" and hangs up on callers, and I set my phones up to only ring between 8am and 10pm - at night it just drops to voicemail. If someone calls from an unrecognized number (not blacklisted and not whitelisted) it says "telemarketers press 1 to hang up now, everyone else press 9 to talk to me". You can do essentially anything you can think of with Asterisk, far more complicated than my little switch... it is all in the dialplan. You can also get prebuilt systems like FreePBX that have management web-apps bundled with them and some pre-configured services like Internet based CallerID lookups and GTalk connectivity.

PS: At the last place I worked I managed an Asterisk box named Obelix. :-)

PS2: I also managed a dozen Mitel switches and liked the Asterisk box better.

S.

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Dynamic IP Script For Asterisk

Postby viking60 » 23 Sep 2014, 15:08

Snorkasaurus wrote:Weird... I don't seem to get the % sign from them. I have noticed in the past that "what is my ip" web sites will not last forever and it is occasionally necessary to find a new one. I really should build some error checking in to the script so that if tnx.nl is dead I won't be screwing up my externip setting.


If you add ;echo you should be fine in any case.. like this:

Code: Select all

#!/bin/bash
confip=`cat /etc/asterisk/sip.conf  | grep externip | cut -c 10-`
wanip=`curl -q tnx.nl/ip ;echo`
if [ $confip != $wanip ]; then
  echo $(date +%Y)-$(date +%m)-$(date +%d) at $(date +%k):$(date +%M) - $wanip >> /path/externip.log
  sed -i 's/externip=.*/externip='"$wanip"'/' /etc/asterisk/sip.conf
  asterisk -rx 'sip reload'
fi
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"


Return to “Networking”