After having further issues with using externhost=yourdomain.dyndns.org in my sip_nat.conf file I decided to use the dynamic IP address and have trixbox update the sip_nat.conf file automatically using a bash script coupled with a cron job.
Your sip clients can still connect with the Dynamic DNS name without any troubles as long as the externip= is the valid public IP.
Please refer to my previous post Trixbox – External sip clients, no audio for additional information on how to setup your external sip clients.
The process is as follows.
1. Create a bash script
2. Update crontab to include an hourly update check against the IP
3. Sit back and relax ![]()
Log into your trixbox via SSH, once logged in issue the command: nano /root/update_dyndns.sh
Paste the below contents into your new file, you should only need to change the following two values
localnet=
mask=
#interogates whatismyip.org to determine the outside IP of the
#!/bin/bash
# Interogates whatismyip.org to determine the outside IP of the
# machine and checks it against what it was at the last check.
# Then it creates a sip_nat.conf file, moves it to the proper
# directory and performs a sip reload.
# set some variables
localnet=”192.168.1.0″ #change this number to your local network subnet
mask=”255.255.255.0″ #change this to your subnet mask
nat=”yes” #Set the nat parameter if you like. If set to no
#this line will not be included in the final file
# change to a working directory. Make sure this directory exists
cd /root/
rm -f index.html # Remove the last file
wget whatismyip.org
read newaddy < “index.html”
read oldaddy < “oldindex.html”
if [ $newaddy != $oldaddy ]
then
mv index.html oldindex.html
echo “externip=$newaddy” > sip_nat.conf
echo “localnet=$localnet/$mask” >> sip_nat.conf
if [ $nat == "yes" ]
then
echo “nat=$nat” >> sip_nat.conf
fi
chmod 775 sip_nat.conf
chown asterisk:asterisk sip_nat.conf
cp sip_nat.conf /etc/asterisk/sip_nat.conf
asterisk -rx “sip reload”
fi
#THE LAST THING IN YOUR NEW FILE SHOULD BE fi DO NOT PASTE ANYTHING ELSE BELOW.
The above script was taken and modified from an unknown website, if it’s yours! thanks and let me know so I can link back to you.
Once you have pasted the above contents into the new file, save the file ctrl+x then enter y then press enter.
The first time you run the script it will fail with errors, here is the work around.
1. run the script, command: /root/update_dyndns.sh
This will create an index.html file in the /root/ dir.
2. command: mv /root/index.html /root/oldindex.html
3. command: nano /root/oldindex.html
Edit one of the numbers where you see the IP address, this will force the script to notice the change and update your sip_nat.conf file for the first time!
4. command: /root/update_dyndns.sh
You should see the following output.
[trixbox1.localdomain ~]# /root/update_dyndns.sh
–16:57:50– http://whatismyip.org/
Resolving whatismyip.org… 75.147.234.41
Connecting to whatismyip.org|75.147.234.41|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: unspecified [text/plain]
Saving to: `index.html’
[ <=> ] 13 –.-K/s in 0s
16:57:51 (147 KB/s) – `index.html’ saved [13]
[trixbox1.localdomain ~]#
If you have followed the above steps correctly, looking at your /etc/asterisk/sip_nat.conf file should look like this..
[trixbox1.localdomain ~]# less /etc/asterisk/sip_nat.conf
externip=203.xx.xx.xx
localnet=192.168.1.0/255.255.255.0
nat=yes
Now comes the question? I don’t want to run this script each time I feel like updating the IP address, you want this to happen automatically, this is where the magic of cron jobs come into play *claps*, the basic concept is to run our script at a predefined time.
Using the website below, I created a cron task that runs, every day, every hour of every month of every year, you can use the website link below in order to customize this to suite your own needs.
58 * * * * /root/update_dyndns.sh > /dev/null
information taken from http://www.htmlbasix.com/crontab.shtml
- At the prompt, type in ‘crontab -e‘. This will open up your crontab file, or create a new one if it doesn’t exist.
When this file opens, you will see other cron jobs listed in here, or if you haven’t any – you’ll see a bunch of lines with ‘~’ on them. - Use the cursor to go down until you can’t move the cursor down any more. This is where you start your new line. Press ‘o‘ to insert a new line.
- Press ‘o‘ to insert a new line. If you want to edit a line, press ‘i‘.
- Copy the code above, then right click into your SSH or telnet client to paste the above code in. This should all go in as a new line.
- Press the ‘esc‘ key to exit out of edit mode.
- To save the changes and exit, type the following in: ZZ
If you want to exit without saving changes, type in: :q! - Once you have exited, to view the new entry in your crontab file type crontab -l in at the prompt. This will list the contents of your crontab file.
That’s it! everything is now done ![]()
Comments or questions welcome.
Download the Bash Script here!
Or use the following command from your terminal
wget http://blog.simplic8.com/wp-content/uploads/update_dyndns.sh
Popularity: 21% [?]
Tags: asterisk, sip_nat.conf, trixbox
Well it’s about time somebody came up with a solution to this externhost thing!!!
I have been searching and searching for over a week and nothing was working… but this solves it!
And… there is no need for a dyndns account and no need for ddclient!!
Hi Jeff,
Thanks for the kind comments, I guess I should have named the script update_dynip.sh instead
I still use dyndns domain to ssh to my asterisk box tho. But certainly takes the pain away with this automated updating of a dynamic IP situation with external sip clients.
Kind regards,
Shayne.
Hi, thank you very much for this script. I found it after many hours of testing and googling. Tried reaching out for help via the trixbox forum etc., but without success.
You helped solve a problem I had with two different providers, one requiring externhost or externip (1und1.de), while the other (sipcall) won’t work with the externhost being set.
OK, if I link from my website back to this tip?
Hi,
Glad I could help
I get alot of hits on this post but not many comments, so i’m sure it’s helped out alot of people.
I’m happy for you to link to this post or any other post on my blog.
Kind regards,
Shayne.
Hi,
Thanks for your good work. I can’t get this script working though, i.e. I know next to nothing about bash scripting. Could you please post the script as a downloadable link? I’m having problems with the formatting.
Hi Jordy,
I’ve sent you an email with the script.
I have also updated the post.. your probably better off using the following command straight from your Asterisk box in order to retrieve the file.
wget http://blog.simplic8.com/wp-content/uploads/update_dyndns.sh
You can use nano to edit the script, just use the following command without the quotations “nano /path/to/script/update_dyndns.sh”
Also note, when you use wget, it will be downloaded into the current dir you are working in.. You can check this but running the following command “pwd”
So when you use the wget command, you may need to move it to a different directory, as such, use this command “mv /path/to/file/from/pwd/command/update_dyndns.sh /root/
Kind regards,
Shayne.
Hi Shanyne,
Thanks a million, also for your kind email. After I performed those steps, the sip_nat.conf gets updated correctly, every time I changed the value in oldindex.html for testing purposes. I was wondering why this awesome solution is not posted anywhere on the trixbox forums. Even better in my opinion, they should fix the externhost= thing. Anyway, I have two remaining questions with regards to your script that I’m hoping you could help with.
1. When I manipulate ip_nat.conf the cron job returns:
No such command ‘âsip’ (type ‘help âsip’ for other possible commands). Which part of the script fails here, and how could I fix that.
2. Could you maybe briefly explain how I would add an option to the script that sets the externrefresh= option in sip_nat.conf?
Again thanks a lot for your great work and time!!
After googling some more I realize my second question is actually obsolete, externrefresh doesn’t serve any purpose when using this great script!
Thanks!
Hi Jordy,
The script doesn’t work, as it’s my fault
I’ve updated it again, I missed changing some of the encoded ” at 1am this morning
Use wget and overwrite your script again, should work this time
let me know if it works alright.
Thanks again for your kind words…
Cheers,
Shayne.