Blacklist Callers From File

Home » Asterisk Users » Blacklist Callers From File
Asterisk Users 6 Comments

If you are happy with the way blacklisting with the Asterisk database works, how about a shell script that loads all of the entries in your blacklist file?

Other alternatives involve modifying your dial plan. If you are comfortable with that then you can consider alternatives like an AGI that reads your text file and sets a channel variable.

If you need more ‘immediacy’ maybe storing the blacklist in a ‘real’
database is more appropriate — especially if you want the users to maintain their own black list via a web page.

6 thoughts on - Blacklist Callers From File

  • Here is a quick and dirty bash script to do it that I wrote you.

    #!/bin/bash if ( asterisk -rx “database deltree blacklist”)
    echo “Blacklist Cleared”
    err “ERROR Failed to clear Blacklist, Exiting.”
    exit 1;

    while IFS=, read TN REASON
    do if ( asterisk -rx “database put blacklist \”${TN}\” \”${REASON}\””)
    echo “Inserted $TN $REASON to Blacklist”
    err “ERROR Insert Failed on $TN.”
    exit 1;

    done < blacklist.csv unset IFS It reads from the file blacklist.csv in the same directory with the format of NUMBER,”DESCRIPTION/REASON”

  • I’m a home user (not business), but I implemented a blacklist function too after a harassing call to my wife.

    Using the Asterisk DB functions, I have a caller ID look-up function before my IVR-tree starts, a simple if then. Lookup caller ID in blocked-caller DB, if found then I kick them to a short dialplan the plays a message telling them they’ve been blocked, then lets them record an “appeal”
    message. If a user put someone in the Blocked DB, by accident, I’d want to have a way for someone to report it to me via phone.

    To add a number to the Blocked DB, the caller internal user is allowed a hot-key on the phone to block the caller in-call, or with screening they are given an option in the prompt. When a new number is added to the Blocked DB, the call recording for the session is retained to be able to remember why it was blocked. The Blocked DB will automatically unblock an entry after a year, to account for number changes, but the entry can be extended before it expires via an email response.

    I did a similar thing for Telemarketers, the phone numbers also automatically unblock, but only after a month, as most of the worst offenders I’ve come across don’t stay with a phone number longer than a few weeks to avoid enforcement by the FTC. For that I also have a hot key on the phones for “Telemarketer Goodbye”, which adds to the DB while playing a
    “Remove me from your list and stop bothering me” message. This gives my users a bit of power as they don’t have to be polite, or think of an evasion to the sales person, they just need to push a button and hang up the phone when they realize is a sales call.

    I didn’t want to publish code for this since it’s a bit long winded, but it should give you some ideas of things you want to consider.



  • Sorry, Gmail took my conversation with John off-list somehow, I’ll repost what I had set here (sorry, it’s going to be a bit big now), and I will be more careful in the future:

    First part:

    I’m using Grandstream phones, so I have XML soft-keys, what hardware do you have and how good are you with dialplans? You’ll have to do some re-work unless you have an exact match for my setup. I’m doing my best to give you the functional code, I have to change a bunch of stuff to not leak the security things I do in my code and some of the way more involved stuff I
    do. this should at least get you running I think. Portions between “CODE:
    /CODE” tags are the functional dialplan code, I tried to comment a lot here to explain what’s happening, but I may have broken something in the process. Also, be aware that I generate custom sound files, so if you try to run the code and it throws an error about sound-file does not exists, that’s why.

    For this, I am using the internal DB in Asterisk (

    The first part is easy, but also very important – filter your Caller-ID
    data (in computers, we NEVER trust data from a source we don’t generate), CODE:

    exten => 15555551234,1,Set(CalledNumber=${15555551234}) ;
    Primary incoming phone line, number changed to protect my phone number same => Set(CallEventTime=${STRFTIME(${EPOCH},,%m-%d-%Y_%H.%M.%S)}
    same => n,Set(CALLERID(name)=${FILTER(A-Z0-9\s,${CALLERID(name)})})
    same => n,Set(CALLERID(num)=${FILTER(0-9,${CALLERID(num)})})
    same => n,Verbose(Incomming call to main phone number)
    same => n,Goto(IVR-Menu,IVRStart,1)

    /CODE – Then you hit phone numbers that don’t have caller ID with the special three-tone application that sounds like a phone company message, CODE:

    exten => IVRStart,1,Zapateller(nocallerid) ; Before answering we do the sound for no-caller ID calls same => n,Zapateller(answer,nocallerid) ; This is where the phone call gets answered, we hit no-caller ID calls again here same => n,Wait(1)
    same => n,Playback(call-recording-warning) ; Legally required message: “For quality control purposes, this call may be monitored.”
    same => n,Wait(2)
    same => n,Monitor(wav,Inbound_to_${CalledNumber}_from_${CALLERID(num)}_${CallEventTime})
    ; Yes, I record every call in an out. same => n,GotoIf($[“${CalledNumber}” != “15555551234”]?BlacklistFilter) ;
    Number changed to protect my toll-free costs!!!

    /CODE – I had a problem with political phone calls to my 855 number this past spring, so I added this little routine to prompt the caller for a single random digit to be entered to prove they could interact, that stopped all the calls from getting through. CODE:

    ; special human test for toll-free same => n,Set(TOLLFREERAND=${RAND(0,9)})
    same => n,Read(HUMANTEST,thank-you&human-test&${TOLLFREERAND}&now,1,,1,5)
    ; Says “Thank you. If you’re a human, please press the number: {RANDOM}, now.”, like a captcha. same => n,GotoIf($[“${HUMANTEST}” = “${TOLLFREERAND}”]?BlacklistFilter) ;
    If the random number matches what they entered they move on in the dialplan same => n,Playback(do-not-call-warning)
    ; I have a message that states when this number was added to the FTC
    do-not-call list,
    ; requests removal of the number from the current list, and warns that the call and information was recorded
    ; and will be turned over to the FTC if the calls continue. same => n,Hangup() ; Yes, if a human can’t be bothered to press one button when calling a number I pay the minutes for, I won’t be bothered to talk to them.

    /CODE – now we can get into the stuff that handles the blacklists and telemarketers. CODE:

    ; return to regular program same => n(BlacklistFilter), Verbose(Checking caller ID against blacklist)
    same => n,GotoIf($[DB_EXISTS(blacklist\${CALLERID(num)})]?:TelemarketerFilter)
    ; If the caller ID is not in the blacklist DB family, it goes to the telemarketer check same => n,VoiceMail(Blacklist@OtherAccounts,s)
    same => n,Hangup()
    same => n(TelemarketerFilter), Verbose(Checking caller ID against telemarketer list)
    same => n,GotoIf($[DB_EXISTS(telemarketer\${CALLERID(num)})]?:IVRInteractiveStart)
    ; If the caller ID is not in the blacklist DB family, it goes to the telemarketer check same => n,VoiceMail(Telemarketer@OtherAccounts,s)
    same => n,Hangup()

    /CODE: Then if the caller ID is not on either list, they go into the start of the IVR. Everything up until now takes about 8 seconds, I limit
    “appeal” voicemails to 30 seconds by account limit, so that if one managed to come in through my toll-free number, it wouldn’t finish the first minute of billing. CODE:

    same => n(IVRInteractiveStart),Verbose(Dialplan continues here)
    ; …redacted…
    same => n,Hangup()


    You’ll have to tell me more about your phones to know what you are doing with called user prompts. You can enter the numbers into the DBs manually from the console using the DB commands: https://wiki.

    I hope this makes sense…

    Second part:

  • Hi Kevin,

    Looks like your gmail did the same thing mine did, took the conversation off-list (unless you meant to do that).

    If as you mention you’re running a mix of POTS and SIP, I’d recommend sticking with the transfer call method (set the hook/trigger in the features.conf file). That way if anyone picks up ANY phone in your house they have a consistent procedure for dealing with those calls. It will add to the confidence of your users and will score points with wife not having to learn and remember several procedures – a label with the hint would go a long way here.

    That’s what I’ll be doing for my aging parents, who feel helpless to the effect of the bothersome calls, and don’t want a complicated solution to the issue.


  • Example: callers with CallerID 0123456789, 9876543210 and 7410258963 are sent to tt-monkeys. Callers from area code 555 are also blocked.

    In “extensions.conf” file add

    #include “blacklist.conf”

    In “blacklist.conf”

    exten => s/0123456789,1,playback(tt-monkeys)

    exten => s/9876543210,1,playback(tt-monkeys)

    exten => s/7410258963,1,playback(tt-monkeys)

    exten => s/_555XXXXXXX,1,playback(tt-monkeys)