FreePBX Trunk Failover Script

I am using a trunk provider that has redundant servers but I can only be registered to one at a time. They suggested that I create a script to monitor the trunk and have it switch automatically but I am running into some issues getting FreePBX to cooperate.

So far this is what I have

trunk=$(/usr/sbin/asterisk -x “pjsip show registrations” | sed -n 5p | awk ‘{print $2" "$3}’)
mysqlDL=$(mysql -u root asterisk -e “SELECT disabled FROM trunks WHERE name=‘ExampleDL’” | awk ‘{print $1}’ | sed -n ‘1!p’:wink:
mysqlNY=$(mysql -u root asterisk -e “SELECT disabled FROM trunks WHERE name=‘ExampleNY’” | awk ‘{print $1}’ | sed -n ‘1!p’:wink:

if [[ $trunk != “ExampleDL Registered” && $mysqlDL = “off” ]]; then
mysql -u root asterisk -e “UPDATE trunks SET disabled=‘off’ WHERE name=‘ExampleNY’”;
mysql -u root asterisk -e “UPDATE trunks SET disabled=‘on’ WHERE name=‘ExampleDL’”;
sed -i ‘s/ExampleDL/ExampleNY/g’ /etc/asterisk/pjsip.registration.conf
sed -i ‘s/’ /etc/asterisk/pjsip.registration.conf
fwconsole reload
logger -s “ExampleDL Down On $(date)” 2>> /var/log/trunkstatus.log

The mysql and sed commands work as intended and modify the database and config files. The changes are reflected in the FreePBX gui but when I get to ‘fwconsole reload’ the changes are reverted. Are there more spots that I need to modify to make these changes from the backend? Anybody have any other suggestions or ideas?

Any help is greatly appreciated as I have hit the brick wall.


You want the “fwconsole trunks …” command followed by ‘fwconsole r’:

1 Like

Thought for sure you nailed it. I manually ran the command to enable the disabled trunk/disable the enabled trunk. Ran ‘fwconsole r’ and instead of switching trunks it reverted the trunk enable/disable back to what it was before I ran the commands.

Any other suggestions?

Working as expected for me. When I set a trunk to --enable, it shows as Disabled=off and stays that way after reload.

I’ll try a reboot after office hours to see if anything changes.

Thanks for the quick suggestions.

I rebooted and tried different combinations, also recreated my trunks in FreePBX. It doesn’t matter if my trunk shows disabled or not using fwconsole only the trunk that was enabled before messing with the trunks sends out registration or even shows with ‘pjsip show registrations’. I am on asterisk 13.12.1 and FreePBX 13.0.151. Out of interest what version are you running. Any other thoughts?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.