how to set random caller ids on Asterisk based system

How to set random caller ids on Asterisk based system

Here we will use rand function of asterisk to set random caller IDs on VICidial and GoAutodial. This is relevant to people using multiple Caller IDs for marketing or re-marketing purposes. Sometimes, the DID, phone numbers are blocked by spam marking by some applications like true caller as it is used to cold call.

Please note that your Carrier provider should allow you to use the caller IDs that you will be using as random. In india, generally this is limited to service providers. If you need more DID, you need to contact to the service providers. As general, they use to provide 30 DID per PRI. However this can be increased after requesting service providers.

Before implementing, we need to choose a Range (Minimum and Maximum).  Minimum is the low DID value and Maximum is the higher DID you have. Now you have to set your dialplan as below:

For eg: Consider the DID Range as 50005001 to 50005030

Option 1: Using Dialplan

Those who use plain Asterisk setup should use the below dialplan.

exten => _9X.,1,Set(Callerid(num)=${RAND(50005001,50005030)})
exten => _9X.,2,Dial(SIP/VOIPTRUNK/${EXTEN:1})
exten => _9X.,3,Hangup()

NOTE: For VICIdial and GoAutodial use the below dialplan.

exten => _9X.,1,AGI(agi://
exten => _9X.,2,Set(Callerid(num)=${RAND(50005001,50005030)})
exten => _9X.,3,Dial(SIP/VOIPTRUNK/${EXTEN:1},,Tto)
exten => _9X.,4,Hangup()

Option 2: Using PHP AGI Script

This method is useful when you have numbers which are not in range, or numbers are placed in a text file. A random number will be used and set as caller ID while the server dials out.

Note: You must have PHPagi installed in your pbx server under agi directory. This somes preinstalled if you use the ISO of VICIdial or GoAutodial to install your server. Else you can download the PHPagi using this link.

Step 1: Create a file named randomcid.php under /var/lib/asterisk/agi-bin. You can use any ftp application to create the file with ease.

Step 2: Copy the below script and paste in randomcid.php

include ‘phpagi-2.20/phpagi.php’;
$agi = new AGI();
$numbers = file(‘/var/lib/asterisk/agi-bin/cids-list.txt’);
$cid = array_rand($numbers, 1);
//return trim($numbers[$cid[0]]);
$newCID = trim($numbers[$cid]);
//echo $newCID;
$agi->set_variable(“CALLERID(num)”, $newCID);


Step 3: Create a file named cids-list.txt under /var lib/asterisk/agi-bin and fill your numbers in a single column. here is an example.


Step 4: Time now to write a dialplan with AGI function.

For plain asterisk

exten => _9X.,1,AGI(randomcid.php)
exten => _9X.,2,Dial(SIP/VOIPTRUNK/${EXTEN:1})
exten => _9X.,3,Hangup

For VICIdial/GoAutodial

exten => _9X.,1,AGI(agi://
exten => _9X.,2,AGI(randomcid.php)
exten => _9X.,3,Dial(SIP/VOIPTRUNK/${EXTEN:1},,tTo)
exten => _9X.,4,Hangup

Step 5: Now all done. Just make a test call.

For any query or issue, feel free to discuss on

One thought on “how to set random caller ids on Asterisk based system

Comments are closed.