how to set random caller ids on Asterisk based system

How to set random caller ids on Asterisk based system

Here we will use the 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 the service providers. In general, they used 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 display as below:

For eg: Consider the DID Range as 01142002001 to 01142002031

Option 1: Using Dialplan

Those who use Asterisk setup should use the below dialplan.

exten => _9XXXXXXXXXX,1,Set(Callerid(num)=${RAND(01142002001,01142002031)})
exten => _9XXXXXXXXXX,n,Dial(SIP/obcalltrunk/${EXTEN:1})
exten =>__9XXXXXXXXXX,n,Hangup()

NOTE: For VICIdial and GoAutodial use the below dialplan.

exten => _9XXXXXXXXXX,1,AGI(agi://
exten => _9XXXXXXXXXX,2,Set(Callerid(num)=${RAND(01142002001,01142002031)})
exten => _9XXXXXXXXXX,3,Dial(SIP/obcalltrunk/${EXTEN:1},,Tto)
exten => _9XXXXXXXXXX,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/cidsrandom-list.txt’);
$cid = array_rand($numbers, 1);
$newCID = trim($numbers[$cid]);
$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: Its Time now to write a dialplan with AGI function.

For plain asterisk

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

For VICIdial/GoAutodial

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

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

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

Comments are closed.