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