Moderator note: This post contains scripts, or links to scripts, that are untested by the Pi-hole team. We encourage users to be creative, but we are unable to support any potential problems caused by running these scripts.
My aim was to not use sqlite, but stick to cron and pihole
#!/bin/bash
#
# this script blocks and unblocks domains from a regex list
# it is called from croni. The /etc/crontab might look like this:
# 2 3 * * 2,4 root /etc/pihole/scheduled-actions -b
# 29 17 * * 2,4 root /etc/pihole/scheduled-actions -d
#
##### Beginning of configuration
# LIST - a list of domain regular expressions
# add the domain regexes in here, one per line, with single quote marks around them
declare -a LIST=(
'*.twitter.com'
'twitter.com'
)
# PHC - where to find the pihole command
PHC='/usr/local/bin/pihole'
##### end of configuration
print_usage() {
>&2 echo "$0 {-b|-d}"
>&2 echo " use -d to delete (unblock) and -b to block"
}
print_config_err() {
>&2 echo "File '$PHC' is not executable or found"
>&2 echo "$0 has not been configured well."
>&2 echo "Help it by doing these two things:"
>&2 echo " 1. find pihole by executing this code:"
>&2 echo " sudo find / -name pihole -type f -executable"
>&2 echo " 2. use that answer to edit the configuration contained in $0"
}
if [[ -f "$PHC" && -x $(realpath "$PHC") ]]
then
echo "Starting pihole to modify the regex blacklist"
else
print_config_err
exit 1
fi
PHARGS=''
while getopts 'bd' flag; do
case "${flag}" in
d) PHARGS=('--regex --delmode') ;;
b) PHARGS=('--regex') ;;
*) print_usage
exit 1 ;;
esac
done
for wc in "${LIST[@]}"; do
$PHC ${PHARGS[@]} \"$wc\"
done
$PHC restartdns