How do I tell an Asterisk dialplan to use any available trunk when dialing out using the dialout-trunk macro

netrxinc used Ask the Experts™
I am trying to get my extensions_custom.conf file configured to make an outbound call on any available SIP trunk using [macro-dialout-trunk].

The following line works:
exten=> 2001,1,Macro(dialout-trunk,2,915551212)

I would llke to replace the ,2, with something that reflects any open trunk.  Currently I have 2 trunks, but I will soon be getting several more.

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
To my knowledge, there is not a variable that gives you "any available trunk."

However, you can loop through them if you have named them numerically, which it appears you have.

Try something like this for your macro:

exten=> 2001,1,Macro(dialout-trunk,915551212)

exten => s,1,Set(DIALSTART=${EPOCH})
exten => s,n(init),Set(TRUNK=0)
exten => s,n(dialagain), Set(TRUNK=$[${TRUNK}+1])
exten => s,n,Dial(SIP/${TRUNK}/${ARG1})
exten => s,n,Goto(s-${DIALSTATUS},1)

exten => s-CANCEL,1,Hangup
exten => s-NOANSWER,1,Set(DTIME=$[${EPOCH} - ${DIALSTART}])
exten => s-NOANSWER,n,GotoIf($["${DTIME}" = "0"]?here)
exten => s-NOANSWER,n,Hangup
exten => s-NOANSWER,n(here),Verbose(1,Need failover for "${ARG1}")
exten => s-BUSY,1,Busy
exten => s-CHANUNAVAIL,1,Verbose(1,You touched me in my naughty place. Trunk ${TRUNK} FAILED. Trying again.)
exten => s-CHANUNAVAIL,n,Goto(dialagain)
exten => s-CONGESTION,1,Verbose(1,Congested? Take some sudafed, or try trunk # ${TRUNK})
exten => s-CONGESTION,n,Goto(dialagain)
exten => _s-.,1,Congestion
exten => s-,1,Congestion

Open in new window


Thanks for the help.  I'm using Asterisk with FreePBX, so I don't want to modify the [macro-dialout-trunk] macro.  What I did was call it [macro-sip-dialout-trunk] and added it to my extensions_custom.conf file.  I reloaded Asterisk and invoke the macro and it hungup. I've attached the appropriate lines from extensions_custom.conf and an error listing from the log.

Appreciate your assistance.


The 1st argument passed to [macro-dialout-trunk] is not identical to the first parameter used in the Asterisk Dial command. Looking at the source of [macro-dialout-trunk] on my FreePBX box, it looks like you would need to change the Dial command in your new macro to this:
exten => s,n,Dial(${OUT_${TRUNK}}/${ARG1})

Or perhaps a better alternative would be to call the FreePBX macro from your macro  - i.e. replace your Dial command with this:
exten => s,n,Macro(dialout-trunk,${TRUNK},${ARG1})

Taking a wider look at your question, I wonder why you consider it necessary to cycle through different trunks and what you mean by an "open trunk". SIP trunks can normally carry more than 1 call at a time. Also, FreePBX does already have functionality in "Outbound Routes" to attempt to call using one trunk and, if that fails, to attempt the same call on a different one. Is it just that you want an inbound call from a particular DDI to redial immediately as an outbound call on another? If so, please look at this previous question:


Thanks for your help.  My main problem is that I've been using this software for about a week :)  I rethought what I was doing and took a completely different approach, and am now using the Recording Panel in FreePBX to set an extension to Call Forward to an outside line, thereby letting the existing code and macros handle everything...AND IT WORKS!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial