An IP address change will drop all external calls in progress anyhow, so an Asterisk restart is not a big deal.
First, if you have any control over the changes, try to reduce their frequency. For example, if caused by a DSL modem losing sync, try to get the noisy pair fixed. If not possible, set up your modem and router so a sync loss does not result in an address change.
If you are in a country where dynamic IP addresses are changed daily, rig a script that forces a change at 3 AM, so they don’t happen during business hours.
If you have a connection where frequent changes are unavoidable e.g. via an LTE router, consider putting your PBX in the cloud. If the office internet has just been hit with an IP change, it can send calls to mobile phones, have the caller wait in a queue, or take a message.
If you are absolutely stuck on a system with frequent IP changes, you need a script that quickly detects the change and updates Asterisk.
updateip.tgz (2.1 KB)
Dynamic DNS is avoided here; it involves a third party, is slow and also a potential point of failure. However, if you have external extensions, you can have it update a DDNS name as well.
To use this perl script, edit /etc/hosts, adding a line like
1.2.3.4 public-ip
(replace 1.2.3.4 with your actual public IPv4 address).
Use the name ‘public-ip’ (without the quotes) as External Address in Asterisk SIP settings.
Run the script in the background. Any actions taken are logged to updateip.log in the current directory.
Then, when the script detects a change, it will update /etc/hosts and either let dnsmgr find it, or restart Asterisk immediately. An address change is typically detected in 6 seconds (15 seconds max) and Asterisk is ready to go again within a minute.
If you use dnsmgr to handle the change, extensions do not lose connectivity, but your trunks will not reregister until the old registration expires. Alternatively, uncomment the line to restart Asterisk immediately, and set up your extensions with e.g. 60 second registration expiry.
Let me know of any bugs, or any improvements you make.