{"id":681,"date":"2014-03-27T16:48:07","date_gmt":"2014-03-27T10:48:07","guid":{"rendered":"http:\/\/alexeyka.zantsev.com\/?p=681"},"modified":"2014-03-27T17:28:54","modified_gmt":"2014-03-27T11:28:54","slug":"asterisk-round-robin-dial-out-through-trunks","status":"publish","type":"post","link":"https:\/\/alexeyka.zantsev.com\/?p=681","title":{"rendered":"Asterisk: round-robin dial out through trunks"},"content":{"rendered":"<p>Imagine that we have 3 trunks for dial out and we must balance outgoing calls in random order. This could be done with the RAND function. Module <kbd>func_rand.so<\/kbd> must be compiled and loaded.<\/p>\n<p>sip.conf:<\/p>\n<pre>\r\n[trunk1](trunk_preset)\r\nhost=10.10.10.11\r\n\r\n[trunk2](trunk_preset)\r\nhost=10.10.10.12\r\n\r\n[trunk3](trunk_preset)\r\nhost=10.10.10.13\r\n<\/pre>\n<p>extensions.conf:<\/p>\n<pre>\r\nexten => _79XXXXXXXXX,1,NoCDR()\r\n    same => n,Dial(SIP\/trunk${RAND(1,3)}\/${EXTEN})\r\n    same => n(hang),Hangup()\r\n<\/pre>\n<p>You may say &#8211; well, OK, but what if my trunks are named not like [trunk1], [trunk2], [trunkN] and I can not substitute the last symbol in Dial application?<br \/>\nIn this case we can write a list of trunk names (which you configured already in sip.conf) in txt file, for example rrtrunks.txt:<\/p>\n<pre>\r\nteliasonera-out\r\ndeutschetelekom-out\r\nlevel3-out\r\n<\/pre>\n<p>Ensure that a &#8216;sort&#8217; command is available in your *NIX system. Usually it&#8217;s a part of coreutils. Older versions don&#8217;t suit us, because they don&#8217;t have the &#8216;-R&#8217; flag which means sort <em>randomly<\/em>. Newer versions have.<\/p>\n<p>Then load <kbd>func_shell.so<\/kbd> and use it in your dialplan. It can execute a shell command and use its output in dialplan.<\/p>\n<pre>\r\nexten => _79XXXXXXXXX,1,NoCDR()\r\n    same => n,Dial(SIP\/${SHELL(sort -R \/etc\/asterisk\/rrtrunks.txt | head -1)}\/${EXTEN},20)\r\n    same => n,Hangup()\r\n<\/pre>\n<p>It means that we sort randomly our list of trunks and get the first one each time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Imagine that we have 3 trunks for dial out and we must balance outgoing calls in random order. This could be done with the RAND function. Module func_rand.so must be compiled and loaded. sip.conf: [trunk1](trunk_preset) host=10.10.10.11 [trunk2](trunk_preset) host=10.10.10.12 [trunk3](trunk_preset) host=10.10.10.13 extensions.conf: exten => _79XXXXXXXXX,1,NoCDR() same => n,Dial(SIP\/trunk${RAND(1,3)}\/${EXTEN}) same => n(hang),Hangup() You may say &#8211; well, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[43,178,179,176,183,177,180,182,181],"class_list":["post-681","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-asterisk","tag-function","tag-functions","tag-func_rand","tag-func_shell","tag-rand","tag-random","tag-shell","tag-sort"],"_links":{"self":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts\/681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=681"}],"version-history":[{"count":9,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts\/681\/revisions"}],"predecessor-version":[{"id":690,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=\/wp\/v2\/posts\/681\/revisions\/690"}],"wp:attachment":[{"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alexeyka.zantsev.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}