Use System() to check external SIP peer availability

Since ChanIsAvail() can only be used to check SIP devices availability, I believe another method is needed when assessing trunks reachability.
Therefore I believe one could use the System() application to, for example, ping the external SIP server and use it’s feedback to this conclusion.

I would like to use this as a method to implement a PSTN fallback mechanism.

However I can’t even dump the System() result to a text file.

Is this syntax correct?

exten => 110,1,System(‘ping -w 1 -c 1 > l.txt’)

Could I use Asterisk to execute an external bash script and read it’s result to an internal variable?

Why not branching on dialstatus (unavai…in this case) ?

My problem is a little more complex since I only use Asterisk to communicate with SIP Servers that use password-based authentication.
I sometimes use ITSPs with IP-based auth, in which cases I pass the call to an OpenSER server running on the same machine. In this way, Asterisk could only test it’s connection to another port on the same machine (always sucessful in normal circumstances).

With this system() application i could use the same test whether I was using one type or the other.

However i’m going to check the output of the test when failing to communicate with the OpenSER-associated external server.

Hi there, I used a smilar approach to do fall back to ISDN when my psovider’s SIP-Server is unreachable, I posted the sollution on our website, it’s rather simple:

exten => _0.,1,System(‘ping -w 1 -c 1 > /dev/null’)
exten => _0.,2,Dial(SIP/${EXTEN}@1und1,Tr)
exten => _0.,102,Goto(ISDNout,${EXTEN},1)

In * 1.2 you have to activate priorityjumping in your extensions.conf to make this work:


Well, your solution is perfect except when the remote servers don’t respond to pings…apparently voipbuster is one of them, as are also some of my other regular iTSPs. :\

I’ll try to devise a method for these.

Thank you very much anyway!

I’ve created this script which can be used with the system command - it will check the output of “sip show peers” to see if your gateway is online.

Substitute x.x.x.x for your gateway IP address

exten => _0.,1,System(’/usr/scripts/checkgateway x.x.x.x’)
exten => _0.,2,Dial(${EXTEN}@x.x.x.x) -> gateway is OK
exten => _0.,102,Dial(ZAP/g1/{$EXTEN}) -> gateway unavail, use ISDN



gate=asterisk -rx "sip show peers" | grep $1 | grep OK
gatecheck=echo -n $gate

if [ “$gatecheck” == “” ]; then
# echo Gateway $1 unreachable
exit 1
# echo Gateway $1 reachable
exit 0

Hope this is of use… we are using it to fall back to our T1s if our VoIP is offline.

As per the previous message, In * 1.2 you have to activate priorityjumping in your extensions.conf to make this work:



Wow, that’s awesome coding in there!
I also devised this alternative:

exten => s,1,System(‘sipsak -v -s sip:nobody@${ARG3} | grep -e “SIP/” > /dev/null’)
exten => s,n,Dial(SIP/${ARG1}@${ARG2})
exten => s,n,Hangup()
exten => s,101,Playback(/var/lib/asterisk/sounds/cannot-complete-network-error)
exten => s,n,Playback(/var/lib/asterisk/sounds/call-forward

and then use with:

exten => _X!,1,Macro(SIPFallback,00351${EXTEN:3},Voipbuster,
exten => _X!,n,Macro(SIPFallback,00351${EXTEN:3},OtherOperator,
exten => _X!,n,Dial(PSTNtrunk…)

however you have to install the “sipsak” app in your machine.

I found my method useful since i keep all SIP server data in a database, and therefore, the asterisk code can be easily generated from it.