So I know this is a fairly simple question, but I can't seem to find a good answer to it. I have a DID which is getting hit fairly hard (or at least for me, about 1-3 calls per day) with telemarketers from non 1-8XX numbers. However there are only a handful of numbers that call day in and day out. So naturally I would just like to block them so my phone doesn't ring and I don't get billed for a call.

I'm looking to send a SIP/404 or some other non-answer response-- not send them to voicemail or another recording. I don't really care about logging or anything like that, I just would like to be able to add (or remove) a number fairly easily so I can cut down on unnecessary calls.

Everything I find seems to be either too complex or too simple (only one blocked number) and I can't find a good medium. Hopefully someone else will have better insight! I am using Asterisk 1.6. Thank you!


I've done something similar and like the other submitter, I used a database. Instead of using the built int DB functionality, I used cmd MySQL. This was so I could write a web page that interacts with the same DB AND my DBA could do cross DB selects between this "Directory" table and the MySQL CDR.

In my example, I setup a table with 4 fields. id (auto_increment int), number (the phone number), name (The name of user), ban (an int that defaults to 0). The example does 2 things, 1- it looks up name from the DB and sets that as the caller id (Since our POTS line doesn't communicate that) 2- It looks up to see if ban is set to 1, and blocks the call.

exten => s,1,Answer
exten => s,n,NoOp("Caller ID IS: ${CALLERID(number)}")
exten => s,n,MYSQL(Connect connid HOST USER PASSWORD database)
exten => s,n,MYSQL(Query resultid ${connid} SELECT name,ban FROM directory WHERE number='${CALLERID(number)}')
exten => s,n,MYSQL(Fetch fetchid ${resultid} name ban)
exten => s,n,NoOp("We found: ${name} ${ban}")
exten => s,n,MYSQL(Clear ${resultid})
exten => s,n,MYSQL(Disconnect ${connid});
exten => s,n,Set(CALLERID(name)=${name})
exten => s,10,NoOp()
exten => s,n,GotoIf($["${ban}" = "1"]?wedontlikeyou,s,1)
exten => s,n,Goto(mainmenu,s,1)

After this you'd have your [mainmenu] and [wedontlikeyou]. The later is your ban, it could be a simple hangup, error message, what ever you want. My preference is Congestion() since that tends to make people think the phone number is out of service.


How are your programming skills? I'm not sure about all the details, but I guess from your question:

  • You have a US incoming number delivered to you via SIP protocol
  • This is a number you pay incoming call costs for (1-800 ?)
  • You have a number of caller-id numbers you want to block
  • This number is big enough (more than 10, I guess) to not write one or two lines of code in extensions.conf for each number

Have a look at the code in the link kaptk2 mentioned.

And then have a look at the DB() functions of asterisk: Asterisk function DB(). You would do a lookup of each incoming caller-id as database key and you can maintain database keys from the asterisk commandline.

Short example of DB management:

greenblatt*CLI> database put blockcaller 18005551212 1
Updated database successfully
greenblatt*CLI> database get blockcaller 18005551212
Value: 1

You could access the value with something like:

exten => s,1,GotoIf($[${DB(blockcaller/${CALLERID(num)}))} = 1]?blocked:ok

The best way to 'kill' the call would be to use the Hangup() function. Or if you insist on a 404 code: send it to a non-existing extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.