How to batch-import a phone number blacklist into Asterisk/FreePBX

Here’s a simple shell script sample on how to import a list of phone numbers into FreePBX’s blacklist module. The blacklist module is available for download in the FreePBX “Module Admin” menu. Unless you’re based in Switzerland, the call-center blacklist I’m using in the script below is probably not relevant to you – you’ll have to find one for your country.
I tried to come up with a highly performant socat/socket based approach using asterisk.ctl to import the numbers into the database, but unfortunately that didn’t work on the Raspberry Pi. That’s why I had to resort to calling the Asterisk binary for every number in the blacklist. It’s slow, but it works. Supply a -d parameter if you want to clear the existing blacklist prior to the import operation.

#!/bin/bash
cli="/usr/sbin/asterisk -rx"
dump=/tmp/blacklist.backup
blacklist=/tmp/latest_cc_blacklist.txt

wget -O${blacklist} https://trick77.com/tools/latest_cc_blacklist.txt

echo "Dumping old blacklist to $dump"
$cli "database show blacklist" > $dump

if [ "$1" == "-d" ]; then
  echo "Removing all existing blacklist entries"
  $cli "database deltree blacklist"
fi

cat $blacklist | while read line; do
  [[ $line = \#* ]] && continue
  number=${line%;*}
  desc=${line#*;}
  echo "database put blacklist $number \"$desc\""
  $cli "database put blacklist $number \"$desc\""
done

This is what it looks like in FreePBX’s blacklist module after the batch-import script is run:

You probably want to update the blacklist every once in a while, so make sure to add a cron.d configuration for the shell script:

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
18 2 2 * * root nice -n10 /usr/local/bin/blacklist_update.sh
# EOF

7 thoughts on “How to batch-import a phone number blacklist into Asterisk/FreePBX

  1. Hi,
    Thanks for this great idea !
    Unfortunatly your web site become too much restrictive with SSL for freepbx build, impossible to download the black list. OpenSSL seems to be to old on the centos used by FreePBX. Is it a way to download it in http ?
    Best regards
    Stephane

  2. … Blacklist is cool But since those guys are *really* annoying I have created something a bit different.

    When they call, someone – or should I say: Something, actually takes the call.

    I have a recording on an extension which plays a pretty loud shiphorn.

    I know that it’s working because the amount of calls is dropping pretty fast according to my log, from 10-20 per week to 1-2 per month(!).

    And fun thing: There were several numbers calling and calling – even when they already were on the blacklist.

    … since activating the horn… one call – and that’s been it.

    You may argue that I’m not very nice. Well, yes. But neither are they!

    Well, let’s see…

  3. Hi.
    I have FREE PBX , I want clear black list log,but I don’t know enter this code, where Enter this code?
    please your help.

Comments are closed.