从源辞典

查阅生活点点滴滴

Asterisk and /etc/hosts

October 11th, 2013 by congyuan

During recent network down because of fire at Singtel Bukit Panjang exchange, my home’s Asterisk server was also down. I search over internet and found it was a long time “bug” for Asterisk since many years ago and it was still not fixed.

The problem:

If the Asterisk server has SIP trunk to outside SIP providers, during the Internet outage, the SIP phones on LAN failed to register with the Asterisk server which is on the same network.

The reason:

The Asterisk uses synchronized DNS lookup. During Internet outrage, DNS times out. Asterisk will not respond to the local phones for SIP registration while waiting for DNS response.

The solution:

1. Use IP address for SIP trunk registration. However, for some SIP providers like pFingo, it will fail.
2. Add static hostname to IP address mapping to /etc/hosts

I go with solution 2.

Now I got another problem to fix. Usually the SIP providers will not change their IP address. To make it more sophisticate, I add the following script to my cron job list to check if the IP address changes and update accordingly.

#!/bin/sh

hosts=( "sip.pfingo.com" "sipproxy1.starhub.com" )
hostfile="/etc/hosts"

for host in "${hosts[@]}"
do
newip=$(dig +short $host)
oldip=$(sed -n 's/\([0-9.]\) '$host'$/\1/p' $hostfile)

if [ ${#newip} -gt 15 ]; then
echo "DNS timeout : $host"
continue
fi

if [ "$newip" != "$oldip" ]; then
sed -i 's/'$oldip'/'$newip'/' $hostfile
echo "$host : $oldip -> $newip"
fi

done

Posted in asterisk | No Comments »