Alternative NAT ip.sh tool

vcallaway

Guru
Joined
May 6, 2008
Messages
170
Reaction score
2
My ISP has issues with ip assignments. They would re-assign a previous address and the script would think all was good. I needed a better solution.

I looked around and grabbed some ideas from various posts and came up with a script that fixes the issue I had. Here is a copy for everyone to replace the existing ip.sh program. Feel free to make suggestions.

Code:
#!/bin/bash

# File to log the IP Address
IPFILE='/var/log/asterisk/externip'

# Your local lan ip block
localnet=192.168.1.0

# Nothing else needs to be changed.
if [ ! -f "$IPFILE" ]; then
        echo "creating $IPFILE"
        echo first_time_usage > $IPFILE
fi

lastip=`cat $IPFILE`
externip=$(curl -s http://myip.dnsomatic.com/)

if [ $externip != $lastip ]; then
        # Writes new IP address (if it has changed) to file.
        echo "$externip" > $IPFILE
        echo "externip=$externip" > /etc/asterisk/sip_custom.conf
        echo "localnet=$localnet/255.255.255.0" >> /etc/asterisk/sip_custom.conf
        echo "srvlookup=yes" >> /etc/asterisk/sip_custom.conf
        echo "nat=yes" >> /etc/asterisk/sip_custom.conf

        asterisk -rx "dialplan reload" ;
else
        exit 0;
fi
exit;
 

wardmundy

Nerd Uno
Joined
Oct 12, 2007
Messages
19,207
Reaction score
5,234
Good improvement! If you'd prefer to use our server for the IP address retrievals, just substitute the line below at the appropriate place above:
Code:
externip=`curl -s -S --user-agent "Mozilla/4.0" http://myip.pbxinaflash.com | awk 'NR==2'`

Don't forget to set localnet correctly for your LAN subnet! Then put this script in your cron jobs to run every 5 or 10 minutes.

For details on all of this, read the Getting Rid of One-Way Audio section of knol.pbxinaflash.com.
 

jades

Guru
Joined
Oct 6, 2008
Messages
160
Reaction score
0
SRV Lookup will sometimes screw up some trunks. I have not tested this script but hope that line can be removed without having an issue with this script
 

vcallaway

Guru
Joined
May 6, 2008
Messages
170
Reaction score
2
I think SRV lookup was recommended by one of my providers. I'm not entirely sure why.

I've just started playing with all of this. My first exposure was at Linuxfest last year and I'm still on the low end of the learning curve.
 

jades

Guru
Joined
Oct 6, 2008
Messages
160
Reaction score
0
SRV Lookup was temporarily recommended by Voice Pulse until they were bombarded with phone calls that they are experiencing issues. If the srv lookup line can be removed and the script will work just as good, I urge you to remove it.
 

rossiv

Guru
Joined
Oct 26, 2008
Messages
2,624
Reaction score
139
CRON messages

I have CRON messages coming into my mailbox on my PIAF server EVERY FIVE MINUTES. This is what EVERY ONE says:
Code:
Message 5:
From [email protected]  Tue Mar  3 18:15:01 2009
Date: Tue, 3 Mar 2009 18:15:01 -0500
From: [email protected] (Cron Daemon)
To: [email protected]
Subject: Cron <asterisk@pbx> /var/lib/asterisk/agi-bin/ip.sh > /dev/null
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <SHELL=/bin/bash>
X-Cron-Env: <PATH=/sbin:/bin:/usr/sbin:/usr/bin>
X-Cron-Env: <MAILTO=root>
X-Cron-Env: <HOME=/>
X-Cron-Env: <LOGNAME=asterisk>
X-Cron-Env: <USER=asterisk>

/var/lib/asterisk/agi-bin/ip.sh: line 5: syntax error near unexpected token `then'
/var/lib/asterisk/agi-bin/ip.sh: line 5: `IPFILE='/var/log/asterisk/externip' if [ ! -f "$IPFILE" ]; then'

What does that mean?
 

The Deacon

Guru
Joined
Jan 29, 2008
Messages
296
Reaction score
14
Have you looked at the script in /var/lib/asterisk/agi-bin/ip.sh to see what it's doing?

How long ago did this start? Have you installed anything recently to correspond with the receiving of those e-mails?

I don't have that script on my box...
 

wardmundy

Nerd Uno
Joined
Oct 12, 2007
Messages
19,207
Reaction score
5,234
Are you using the code from here???
 
Last edited by a moderator:

wardmundy

Nerd Uno
Joined
Oct 12, 2007
Messages
19,207
Reaction score
5,234
I haven't used it yet, but I know the old way works...

HTML:
#!/bin/bash

fqdn="ionvillage.dyndns.org"
localnet="192.168.0.0"
externip=`ping -c 1 $fqdn | cut -f 2 -d "(" | cut -f 1 -d ")" -s | grep -m 1 ^`

if [ -e /tmp/$externip ] ; then
   echo No IP Update Required ;
else
   echo IP Update Required ;
   touch /tmp/$externip ;
   echo "externip=$externip" > /etc/asterisk/sip_custom.conf
   echo "localnet=$localnet/255.255.255.0" >> /etc/asterisk/sip_custom.conf
   asterisk -rx "dialplan reload" ;
fi
 

vcallaway

Guru
Joined
May 6, 2008
Messages
170
Reaction score
2
I get this email when the server does not have internet connection.

Telnet in and type "curl -s http://myip.dnsomatic.com/" and make sure that both curl is installed and that you get an ip address back.
 

Members online

No members online now.

Forum statistics

Threads
25,827
Messages
167,874
Members
19,254
Latest member
hezbay
Get 3CX - Absolutely Free!

Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.
Top